Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Model-View-Controller
2.1.
Model
2.2.
View 
2.3.
Controller 
3.
Workflow
4.
Application of Web2py
5.
Create a New Application
6.
API 
6.1.
Accessing the API from Python modules
6.2.
request
7.
Response
8.
Frequently Asked Questions
8.1.
What is web2py framework?
8.2.
Who is the author of Web2py?
8.3.
What is the default port of Web2py?
8.4.
In which language was Web2py written?
8.5.
What are the features of Web2py?
9.
Conclusion
Last Updated: Mar 27, 2024

Applications and API in web2py

Author Anju Jaiswal
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this article, we deep dive into understanding the application of web2py and its API. For database-driven web applications, Web2py is a free, open-source web framework for agile development that is written in Python and programmable in Python.  

Application and API in web2py

It is a full-stack framework with every part a developer could need to create a fully functional web application.

Model-View-Controller

In contrast to conventional patterns, the web2py framework runs web application of web2py using the Model-View-Controller pattern.

Model

The Model is a component of the application of web2py that contains the data logic. Data from the database is retrieved and stored using the objects in the Model.

View 

The application's view component aids in providing users with the data display. Data is pulled from the Model for the data display.

Controller 

The component of the application of web2py that manages user interaction is called Controller. Controllers can read information from views, manage user input, and send information to a particular model.

web2py appliance

A built-in feature of web2py allows you to control cookies and sessions. The session is simultaneously stored after a transaction is committed (in terms of SQL).

Web2py can execute the tasks at predetermined intervals following the conclusion of specific actions. CRON can be used to accomplish this.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Workflow

Look at the workflow illustration provided below:

web2py workflow
  • The web server handles the HTTP requests concurrently in a separate thread.
  • The dispatcher receives the HTTP request header.
  • The dispatcher controls application request management and maps the PATH INFO in the function call's URL.
  • The URL contains a representation of each function call.
  • Large files are streamed to the client, and all requests for files found in the static folder are handled directly.
  • Any request not for a static file is translated into action.
  • The session object is retrieved if the request header for the app contains a session cookie; otherwise, a session id is created.
  • The client is not given that value if an action returns a value as a string.
  • If the action returns an iterable, it is used to loop through the data and stream it to the client.

Application of Web2py

The components of applications created with web2py are as follows:

  • Models describe how data are represented as database tables and their connections.
  • Controllers outline the workflow and application logic.
  • Views specify how HTML and JavaScript should be used to present data to the user.
  • Languages provide instructions on how to convert application strings into each of the supported languages.
  • Static file processing is unnecessary (e.g., images, CSS stylesheets, etc.).
  • Documents titled "ABOUT" and "LICENSE" are self-explanatory.
  • The application generates error reports, which are stored in errors.
  • Sessions record data specific to each user.
  • Databases hold additional table data as well as SQLite databases.
  • Applications' cached items are kept in the cache.
  • Modules are optional extra Python modules.
  • The controllers have access to private files but not the developer.
  • Models have access to uploaded files, but the developer is not directly involved (e.g., files uploaded by application users).
  • The directory tests store test scripts, fixtures, and mocks.

Static files, languages, and models can access the interface of the web administration [design]. The corresponding menu items in the administration interface also provide access to ABOUT, README, and errors. The administration interface does not have access to sessions, cache, modules, or private files.

Even though the user never needs to access the filesystem directly, everything is neatly organized in a clear directory structure that is replicated for every web2py application that is installed:

__init__.py  ABOUT        LICENSE    models    views
controllers  private      models test cron
cache        errors       upload     sessions  static

The empty file "__init __.py" is necessary for Python (and web2py) to import the modules in the modules directory.

Remember that the admin application merely offers an interface for web2py applications on the server file system. You don't have to use the browser admin interface; you can create and develop web2py applications from the command line or your preferred text editor/IDE. The above directory structure can be duplicated manually under "applications/newapp/," for example, to create a new web2py application (or untar the welcome.w2p file into your new application directory). Application files can be created and edited from the command line without using the web admin interface.

Create a New Application

Web2py applications can be created from the admin interface. Give your new application a name under "New simple application" and click "Create." As an example, we'll use the name "Arista EOS Tool" for the application of web2py.

create new application

It will create the new application of web2py with default models, controllers, and views.

create new application

You can view the recently created application of web2py by directly launching the web2py application from your browser at https://web-server>/Arista EOS Tool/default/index. Your application can also be launched from the admin interface. Click "Open  Link in New Tab" in the "right-click" menu of your web2py application."

create new application

This is the default website that web2py has created for your program.

create new application

 You can check the Ubuntu server to see which folders and files web2py created for your new application. The "/home/www-data/web2py/applications" folder contains both the standard applications and your application.

API 

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

Global Objects − request, response, session, cache.

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

For example, A, B, FIELDSET, FORM, etc.

Accessing the API from Python modules

Your models or controller may import Python modules. These are typically Python files that you keep in your app's modules directory. Perhaps some of the web2py API will be required. The process involves importing them:

from gluon import ...

If web2py is included in the sys.pat file, any Python module, even one not imported web2py application, can import the web2py API.

request

The request object represents the popular web2py class known as gluon.storage. Storage is a Python dict class extension. Although it resembles a dictionary in many ways, attribute access is also possible for the item values:

request.vars                                                                                         
request table

Response

Another instance of the Storage class is response. It has the following in it:                                                    

response table

Frequently Asked Questions

What is web2py framework?

An open-source web application framework called Web2py. It enables the creation and design of dynamic web applications.

Who is the author of Web2py?

Massimo Di Pierro is the author of Web2py.

What is the default port of Web2py?

Web2py default port is 8000.

In which language was Web2py written?

Web2py was written in Python language.

What are the features of Web2py?

Web2py features are: easy to learn, portable, standard library that supports many tasks.

Conclusion

In this blog, we have briefly introduced web2py, its workflow, the essential components used to compose the Application of web2py, and how you can create a new application. We dive into API requests and responses to global objects with their instances.

Here are a few key websites that will aid in your exploration of Web2py

 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem DesignMachine learning and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!!

Thank You

Previous article
Libraries in Web2py
Next article
Accessing the API from Python Modules in web2py
Live masterclass