Web2py - Framework Overview


Advertisements

web2py is a full-stack web framework that can be used by a developer to completely develop a web application. It includes SQL database integration and multi-threaded web server for designing a program.

Web Interface for Designing a User’s Program

Once the command is executed as per the operating system, web2py displays a startup window and then displays a GUI widget that asks the user to choose −

  • a one-time administrator password,
  • the IP address of the network interface to be used for the web server,
  • and a port number from which to serve requests.

The administrator includes all the authority for addition and editing any new web application.

By default, web2py runs its web server on 127.0.0.1:8000 (port 8000 on localhost) but a user can run it on any available IP address and port as per the requirement.

The web2py GUI widget will be displayed as shown below.

GUI Widget

The password is used in the administrative interface for any changes in the new module.

After the user has set the administration password, web2py starts up the web browser at the page with the following URL − http://127.0.0.1:8000/

The welcome page of the framework will be displayed as shown below.

Framework

Designing a Basic Program in web2py

After starting the web2py application, with the above-mentioned URL, we can use the administrative interface for creating a new module, for example, “helloWorld”.

The administrative interface will ask for the password for authentication purpose as the administrator holds all the authority for addition and editing any new web application.

Web2py Applications

The snapshot given above includes the page details, which lists all the installed web2py applications and allows the administrator to manage them. By default, the web2py framework comes with three applications. They are −

  • An admin application, which the user is implementing currently.

  • An examples application, with the online interactive documentation and an instance of the web2py official website.

  • A welcome application. It includes the basic template for any other web2py application. It is also known as the scaffolding application. The application also welcomes a user at the startup.

Let the name of the new application be “helloWorld”.

Once, a new application is created, the user is redirected to a page comprising of view, model and controllers of the respective application.

Edit Application

The user can look at the newly created application by mentioning the following URL − http://127.0.0.1:8000/helloWorld

By default, a user can view the following screen on hitting the above-mentioned URL.

For printing the message of the given web application “helloWorld”, the change is made in the default.py controller.

Web Application

The function named “index” is the default function for returning the value and displaying the necessary output. As mentioned above, the string “Hello World- Welcome to my first web application” is used as the return value, which displays the output in the screen.

The output is displayed as follows −

Output

Postbacks

The mechanism of validating the input of form is very common and is not considered as such a good programming practice. The input is validated each time, which is a burden for validation.

A better pattern in web2py is to submit forms to the same action, which generates them. This mechanism is called as “postback” which is the main feature of web2py. In short, self-submission is achieved in postback.

def first():
   if request.vars.visitor_name: #if visitor name exists
      session.visitor_name = request.vars.visitor_name
      redirect(URL('second'))#postback is implemented
   return dict()

CRUD Application

web2py includes applications, which perform the functions of Create, retrieve, update and delete. The CRUD cycle describes the elemental functions of a database, which is persistent.

All the application logic is written in the models, which are retrieved by the controllers and displayed to the users with the help of view.

appadmin

For PHP, the application server includes listing of all the databases under phpmyadmin. In a similar way, web2py provides an interface for managing, creating and deleting tables or databases, which is termed as “appadmin.”

Before implementing the logic behind the tables, it is necessary to create database and its associated tables.

The URL to access appadmin

http://127.0.0.1:8000/applicationname/appadmin

On hitting the URL, the user will get the list of tables associated for the given application.

List Of Tables

This interface is not intended to be public. It is designed to get an easy access to the database. It consists of two files namely − a controller “appadmin.py” and a view “appadmin.html”.

It can paginate up to 100 records at a time. The usage of “appadmin” is discussed in subsequent chapters.

Advertisements