Web2py Core - Web2Py

What is Web2py core?

Command Line Options

We have knowledge how to start the web2py server by means of GUI widget in the earlier section.

This widget can be avoided by starting the server from command line prompt.

python web2py.py -a 'your password' -i -p 8000

Whenever web2py server starts, it creates a file "parameters_8000.py" where all the passwords are stored in a hashed form.

For additional security purpose, the following command line can be used −

For the above scenario, web2py reuses the hashed passwords stored in "parameters_8000.py".

In case, if the file "parameters_8000.py" is deleted accidently or due to some other reasons, the web-based administrative interface is disabled in web2py.

URL Mapping / Dispatching

The functioning of web2py is based on model-view-controller, which maps the URL in a specific form −

It routes till the function “f()” referred to inside the controller d.py is under the application named “a”. If the controller isn't always gift within the utility then web2py uses a default controller named “default.py”.

If the feature, as given inside the URL isn't always present, then the default feature called init() is used. The working of the URL is shown schematically in the picture below.


The extension .html is optional for the URL. The extension determines the extension of View that renders the output of the function defined in the controller. The same content is served in multiple formats namely html, xml, json, rss etc.

The request is passed, based on the functions, which accept the arguments and gives the appropriate output to the user. It is the controller, which interacts with model and view of the application for giving the output as per the user’s need.

web2py – Workflow

The workflow of web2py is discussed below −

  • The web server manages each and every HTTP requests simultaneously in its own thread.
  • The HTTP request header is parsed and passed to the dispatcher.
  • The Dispatcher manages the application requests and maps the PATH_INFO in the URL of the function call. Every function call is represented in the URL.
  • All the requests for files included in the static folder are managed directly, and large file are streamed to the client.
  • Requests for anything but a static file are mapped into an action.
  • If the request header contains a session cookie for the app, the session object is retrieved; or else, a session id is created.
  • If the action returns a value as string, this is returned to the client.
  • If the action returns an iterable, it is used to loop and stream the data to the client.

Conditional fashions

inside the preceding chapter, we saw the capability of the Controllers. web2py uses fashions, perspectives, and controllers in each of its software. therefore, it's also essential to understand the functionality of the version.

not like some other MVC software, models in web2py are dealt with as conditional. models in subfolders are done, primarily based on its controller’s usage. this may be established with the following instance −

don't forget the URL −

In this situation, ‘a’ is the call of the utility, ‘d’ is the controller’s name and f() is the function associated with the controller. The list of models, so that it will be completed are as follows −


web2py consists of libraries, which might be exposed to the all of the programs as the objects. those objects are defined inside the center files underneath the directory named “gluon”.

among the modules like DAL template don't have any dependencies and can be implemented outside the framework of web2py. It additionally maintains the unit checks that is considered as desirable exercise.


web2py programs are proven underneath in a diagrammatic form.


The applications evolved in web2py are composed of the subsequent parts −

  • models − Represents data and database tables.
  • Controllers − Describes the software common sense and workflow.
  • views − enables rendering the show of the data.
  • Languages − describe a way to translate strings inside the application into numerous supported languages.
  • Static documents − Do no longer require processing (e.g. images, CSS style sheets and many others).
  • approximately and README − information of the venture.
  • errors − shops mistakes reports generated via the utility.
  • periods − stores information associated with each precise consumer.
  • Databases − save SQLite databases and additional table facts.
  • Cache − save cached software items.
  • Modules − Modules are other elective Python modules.
  • private − protected files are accessed through the controllers however now not immediately through the developer.
  • Uploads − files are accessed by using the models but now not immediately by the developer.


In web2py, models, controllers and views are executed in an surroundings where positive objects are imported for the developers.

global items − request, reaction, consultation, cache.

Helpers − web2py consists of helper class, which can be used to build HTML programmatically. It corresponds to HTML tags, termed as “HTML helpers”.

as an example, A, B, FIELDSET, shape, and so forth.


A session may be described as a server-facet storage of facts, that's continued throughout the consumer's interaction at some stage in the internet software.

consultation in web2py is the instance of storage elegance.

for instance, a variable can be saved in consultation as

This price can be retrieved as

The value of the variable may be retrieved as long as the code is performed in the same session by the equal user.

one of the crucial methods in web2py for session is “forget” −

It instructs web2py not to save the session.

Running Tasks in Background

An HTTP request arrives to the web server, which handles each request in its own thread, in parallel. The task, which is active, takes place in the foreground while the others are kept in background. Managing the background tasks is also one of the main features of web2py.

Time-consuming tasks are preferably kept in the background. Some of the mechanisms are listed as follows, which manage the background tasks −

  • CRON
  • Queues
  • Scheduler


In web2py, CRON gives the ability to run the task within the specified intervals of the time. Each application includes a CRON file, which defines its functionalities.


The built-in scheduler helps in running the tasks in background by setting the priority. It provides a mechanism for creating, scheduling and modifying the tasks.

The scheduled events are listed in models with the file name “scheduler.py”.

Building an Application

We had an overview of creating models and controllers in web2py. Here, we will focus on the creation of the application named “Contacts”. The application needs to maintain a list of companies, and a list of people who work at those companies.

Creation of Model

Here, identification of the tables for the data dictionary is the model. The model for the contacts application will be created under the “models” folders. The file is stored in models/db_contacts.py.

Once the above file is created, the tables can be accessed with the help of URL

Creation of Controller

The Controller will include some functions for listing, editing and deleting the contacts.

The creation of the view along with its output will be discussed in the next chapter.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Web2Py Topics