Table of contents
1.
Introduction
2.
Third Party Modules
2.1.
Using the site-packages Directory
2.2.
Using the Module Folder of Application
3.
Execution Environment
4.
Cooperation
5.
Logging
6.
Frequently Asked Questions
6.1.
What is DAL in Python?
6.2.
What is SQLite used for?
6.3.
What is pyDAL?
6.4.
What is pylon framework?
6.5.
What is Falcon API?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Third Party Modules In Web2py

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Web2py is a Python-based, accessible, and open-source web framework enabling agile development of database-driven online applications. It is a full-stack framework that includes all the elements a developer needs to create a fully functional web application.

web2py cover photo

In this blog, we will discuss Third Party Modules, Execution Environment, Cooperation, and Logging in Web2py.

Third Party Modules

Because web2py is developed in Python, it can import and utilize any Python module, including third-party modules. All it needs to do is be capable of finding them. Modules may be installed in the official Python site-packages directory and then imported from anywhere in your code, just like with any other Python program.

There are mainly two ways of including the third-party modules in Web2py:-

1.) Using the site-packages directory.

2.) Using the module folder of Application.

Using the site-packages Directory

The site-packages directory contains modules that are, as the name implies, site-level packages. Applications that need site-packages are not portable unless these modules are installed separately.

Let’s take an example to understand how to include third-party modules using the site-packages directory.

Consider the "pandas" package as an example. You can use the pip command to install it from the command line.

pip install pandas
You can also try this code with Online Python Compiler
Run Code


and then you can import it into any model/controller/view with:

import pandas
You can also try this code with Online Python Compiler
Run Code


A site-packages folder may be found in the top-level folder of both the web2py source distribution and the Windows binary distribution. The Mac binary release includes a site-packages folder:

web2py.app/Contents/Resources/site packages
You can also try this code with Online Python Compiler
Run Code


Let’s look at the advantage and disadvantages of using the site-package directory.

Advantage and disadvantages of using the site-package directory

Using the Module Folder of Application

Web2py provides another method for importing modules without changing the global sys.path: by storing them in an application's "modules" subdirectory. One advantage is that the module will be automatically duplicated and distributed with the application.

For example, Once a module "mymodule.py" is placed in an app "modules/" folder, it may be imported from anywhere inside a web2py application (without the need to change sys.path with):

import mymodule
You can also try this code with Online Python Compiler
Run Code

Execution Environment

Model and controller files for web2py are not Python modules since the import statement in Python cannot be used to import them. This is because web2py global objects (request, response, session, cache, and T) and helper functions are pre-populated in the context where models and controllers are intended to be run. Python is a statically (lexically) scoped language, whereas the web2py environment is dynamically generated, hence this is required.

Man doing programming.

Web2py includes the exec_environment function, which allows you to access models and controllers directly. After loading the file into the web2py execution environment that was created, exec_environment returns a Storage object that contains the environment. Additionally, the Storage object functions as a namespace mechanism. Using exec_environment, any Python file created to run in the execution environment may be loaded. exec_environment has the following uses:-

  • Accessing data (models) from other applications.
  • Accessing global objects from different models or controllers.
  • Executing controller functions from other controllers.
  • Helper libraries for the entire site are being loaded.

Cooperation

There are many ways applications can cooperate, some of the ways of cooperation are given below.

Some of the ways of cooperation

The syntax used to import modules from other applications is as follows:-

from applications.otherapp.modules import mymodule
You can also try this code with Online Python Compiler
Run Code


or 

import applications.otherapp.modules.othermodule
You can also try this code with Online Python Compiler
Run Code

Logging

Python offers logging APIs. Web2py provides a method to set it up so that apps may use it.

For example, you may include a logger in your application in a model:-

import logging
logger = logging.getLogger("web2py.app.ninjasapp")
logger.setLevel(logging.DEBUG)
You can also try this code with Online Python Compiler
Run Code


Additionally, you may use it to log messages of different importance.

logger.debug("Show me the details: %s", details)
logger.info("Show me the info: %s", details)
logger.warn("Show me the warning: %s", details)
logger.error("Show me the error: %s", details)
You can also try this code with Online Python Compiler
Run Code

Frequently Asked Questions

What is DAL in Python?

Database Abstraction Layer (DAL), a feature of web2py, is an API that converts Python objects into database objects, including queries, tables, and records.

What is SQLite used for?

Desktop applications like version control systems, financial analysis tools, media categorization and editing suites, CAD packages, record-keeping programs, and others frequently use SQLite as the on-disk file format.

What is pyDAL?

An entirely Python database abstraction layer is called pyDAL. For the database back end, the pyDAL module dynamically creates the SQL for the database back end in the chosen language.

What is pylon framework?

Python-based Pylons Framework is an open-source Web application framework. The Web Server Gateway Interface standard is heavily utilized to encourage reusability and divide functionality into independent modules.

What is Falcon API?

Falcon is a lightning-quick, lightweight Python web API framework for creating reliable app backends and microservices. The framework performs admirably with both gevent/meinheld and asyncio (ASGI) (WSGI).

Conclusion

In this article, we have extensively discussed Third Party Modules in Web2py and execution, cooperation, and logging concepts. I hope you enjoyed reading this article on Third Party Modules in Web2py. If you want to learn more, check out our articles on What Is Web2Py?Why To Use Web2py?Postbacks and Internationalization in web2py, and  XML in Web2py.

Do upvote our blog to help other ninjas grow.

Happy Coding!

thank you image
Live masterclass