Say Hello

Here, as an example, we create a simple web app that displays the message "Hello from MyApp" to the user. We will call this application "myapp". We will also add a counter that counts how many times the same user visits the page.

You can create a new application simply by typing its name in the form on the top right of the site page in admin.

After you press [submit], the application is created as a copy of the built-in welcome application.

To run the new application, visit:

Now you have a copy of the welcome application.

To edit an application, click on the [EDIT] button for the newly created application.

The EDIT page tells you what is inside the application. Every web2py application consists of certain files, most of which fall into one of five categories:

• models: describe the data representation.

• controllers: describe the application logic and workflow.

• views: describe the data presentation.

• languages: describe how to translate the application presentation to other languages.

• modules: Python modules that belong to the application.

• static files: static images, CSS files [39, 40, 41], JavaScript files [42, 43], etc.

Everything is neatly organized following the Model-View-Controller design pattern. Each section in the [EDIT] page corresponds to a subfolder in the application folder.

Notice that section headings will toggle their content. Folder names under static files are also collapsible.

Each file listed in the section corresponds to a file physically located in the subfolder. Any operation performed on a file via the admin interface (create, edit, delete) can be performed directly from the shell using your favorite editor.

The application contains other types of files (database, session files, error files, etc.), but they are not listed on the [EDIT] page because they are not created or modified by the administrator. They are created and modified by the application itself.

The controllers contain the logic and workflow of the application. Every URL gets mapped into a call to one of the functions in the controllers (actions). There are two default controllers: "appadmin.py" and "default.py". appadmin provides the database administrative interface; we do not need it now. "default.py" is the controller that you need to edit, the one that is called by default when no controller is specified in the URL. Edit the "index" function as follows:

2 return "Hello from MyApp"

Here is what the online editor looks like:

«no »drt myipp/Ciirttrtiil*fiyd»ljijlt-py 1

«no »drt myipp/Ciirttrtiil*fiyd»ljijlt-py 1

Editing file "myapp/mntroflers/defaulLpy"

4apaaes Tint, second. Imitm, uarr, dnwiUu«!, call [ dm ]

Editing file "myapp/mntroflers/defaulLpy"

4apaaes Tint, second. Imitm, uarr, dnwiUu«!, call [ dm ]

:' mr'tKiivrtl fileha-.fr 1471174&«»3fUi?01f JW I.T.I vrvr<) rm: Tuc Auq IB H li SUOIH

:' mr'tKiivrtl fileha-.fr 1471174&«»3fUi?01f JW I.T.I vrvr<) rm: Tuc Auq IB H li SUOIH

def Ifutexnt »

rftiuf A "He3 la f rexs HyApp*

18

14

JO

13

24

Ljt 2. Oi X Tttii Lni.CMI

^Jc-ejjIc cdJsor

|J rertof t"' currently saved or ("iwi") to previous wnlaa.

PbwkkI by wctapy fT>!) treated bj- Muitmo Dl Pierre ?j a007, aooi. 3009

Ljt 2. Oi X Tttii Lni.CMI

^Jc-ejjIc cdJsor

|J rertof t"' currently saved or ("iwi") to previous wnlaa.

PbwkkI by wctapy fT>!) treated bj- Muitmo Dl Pierre ?j a007, aooi. 3009

Doa*

Save it and go back to the [EDIT] page. Click on the index link to visit the newly created page.

When you visit the URL

1 http://12 7.0.0.1:8000/myapp/default/index the index action in the default controller of the myapp application is called. It returns a string that the browser displays for us. It should look like this:

Now, edit the "index" function as follows:

2 return dict(message= "Hello from MyApp")

Also from the [EDIT] page, edit the view default/index (the new file associated with the action) and, in this file, write:

Now the action returns a dictionary defining a message. When an action returns a dictionary, web2py looks for a view with the name "[con-troller]/[function].[extension]" and executes it. Here [extension] is the requested extension. If no extension is specified, it defaults to "html", and that is what we will assume here. Under this assumption, the view is an HTML file that embeds Python code using special {{ }} tags. In particular, in the example, the {{=message}} instructs web2py to replace the tagged code with the value of the message returned by the action. Notice that message here is not a web2py keyword but is defined in the action. So far we have not used any web2py keywords.

If web2py does not find the requested view, it uses the "generic.html" view that comes with every application.

If an extension other than "html" is specified ("json" for example), and the view file "[controUer]/[function] json" is not found, web2py looks for the view "generic json". web2py comes with generic.html, generic.json, generic.xml, and generic.rss. These generic views can be modified for each application individually, and additional views can be added easily.

Read more on this topic in Chapter 9.

If you go back to [EDIT] and click on index, you will now see the following HTML page:

Was this article helpful?

0 0

Post a comment