Table of contents
1.
Introduction 🎯
2.
Accessing the API from the Python Module 🌐
3.
Sharing the Global Scope with Modules Using the Current Object 🔖
4.
Warning! Do Not Use the Current Object in the Global Scope in a Module 🔖
5.
Frequently Asked Questions ❓
5.1.
Why should we use Web2py?
5.2.
What are the protocols that Web2py supports?
5.3.
How can we delete the records from the database through Web2py?
5.4.
Why is Web2py different from Django?
5.5.
What is an HTTP response?
6.
Conclusion ✉️
Last Updated: Mar 27, 2024
Easy

Accessing the API from Python Modules in web2py

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 🎯

Web2py is an open-source web application framework written in Python. Web2py helps developers to program dynamic websites using Python. Web2py is designed in such a way that it reduces all those web development tasks which are time taking. 

Accessing the API from Python modules in web2py

The base of Web2py's design is Ruby Rails and Django. In this blog, we will see accessing the API from Python modules in web2py. Accessing the API from Python modules in web2py is essential because it handles all the data transferred in the whole system.

Accessing the API from the Python Module 🌐

Web2py framework is designed based on Django, so it also works on MVC(Model View Control) method. Our models or controllers may need to import modules of Python. Python modules are Python files that we store in the modules directory of our framework. Depending on the requirements, the modules may use some web2py API. To use those APIs, we need to import them. For accessing the API from Python modules in web2py, we use the following command:

from gluon import …
You can also try this code with Online Python Compiler
Run Code

Any python module can import the API of Web2py as long as Web2py is available in the "sys. path."

Sharing the Global Scope with Modules Using the Current Object 🔖

"though. Web2py" is a caveat(warning) that defines global objects like request, response, cache, session, T) that only exists when an HTTP request is present or seems to be present. That's why modules can access those objects only when any application calls them. This is why we store them in a container known as current, a local thread object. To clear the concept, let's understand this with an example:

Let's create a module  "/codingninjas/modules/myweb.py" for accessing the API from Python modules in web2py  that contains:

from gluon import current
	def ip():
	return current.request.client”
You can also try this code with Online Python Compiler
Run Code

Now from the controller of "codingninjas" we can import

import codingninjas
	def index():
	return myweb.ip() + "is our ip " 
You can also try this code with Online Python Compiler
Run Code

"import codingninjas" firstly check for the module in the "current" application's module folder. After that, it will check in the "sys. path folders." That's why app-level modules always get preference over Python modules, allowing the different applications to ship with other module versions without any issue.

Since "current.request" is a different object in different threads, there would be no conflict even if many users can call the same action index simultaneously. We should never forget that we can't call "current.request" outside of a function.

"import codingninjas" is a short form of "applications.appname.modules import myweb". It can be possible to import modules from different applications using this syntax.

While accessing the API from Python modules in web2py, When the changes are complete, Web2py doesn't reload the modules to give programmers flexibility, but we can reload the modules by a function named "track_changes." Below is the declaration of the "track_changes" function.

from gluon.custom_import import track_changes; track_changes(True)
You can also try this code with Online Python Compiler
Run Code

After importing this function, the function will reload the modules whenever there is any change in our Python source file. 

Function "track_changes" only governs changes in those modules stored in the application. The modules which import the current container can access: 

  • current.request
  • current.response
  • current.session
  • current.cache
  • current.T

Apart from that, it can also access those variables stored in the "current container." For example:

ninja = Auth(cns)
from gluon import current
	current.ninja = ninja
	current.cns = cns”
You can also try this code with Online Python Compiler
Run Code

Now all the imported modules can access "current.ninja".

"Current" and "import" methods increase the reusability of modules of the application.

Warning! Do Not Use the Current Object in the Global Scope in a Module 🔖

"from gluon import current..." is good to use "current.request" and any other thread-local objects, but we should not assign these functions to global variables in the process of accessing the API from Python modules in web2py, such as

request = current.request # It is wrong and dangerous.
You can also try this code with Online Python Compiler
Run Code

We should also not use "current" in the class declaration:

class CodingNinjas:
request = current.request # It is wrong and dangerous.
You can also try this code with Online Python Compiler
Run Code

 

The above request is wrong because Web2py should extract the local thread object at runtime. In comparison, We declare Global variables only when we import the model for the first time. 

So, Instead of assigning the local objects to global variables, we should set them inside the function as given below:

from gluon import current…
def codingninjas():
	cns = current.cns" # Suppose we have assigned current.cns = cns in the model cns.py
You can also try this code with Online Python Compiler
Run Code

Another warning is that we should not use "cache" to enhance the functions in the module because it will not show the expected behavior. So to cache a function "cns" in a module, we should use "lazy_cache." For example

from gluon.cache import lazy_cache
@lazy_cache('key', time_expire=30, cache_model='CodingNinjas')
def cns(coding, ninjas, blogs): ....
You can also try this code with Online Python Compiler
Run Code

Frequently Asked Questions

Why should we use Web2py?

Web2py is an easy-to-use framework for web development and doesn't require any installation.

What are the protocols that Web2py supports?

XML, XMLRPC, JSON, JSON RPC, AND RPC, RSS, CSV, and SOAP are the protocols which Web2py supports.

How can we delete the records from the database through Web2py?

Users can delete the record saved in the server by the following command:

“curd.delete(db.table,id)”

Why is Web2py different from Django?

Web2py is designed based on Django, so we can say that Web2py is more advanced than Django and is more compact and easy to learn.

What is an HTTP response?

The HTTP response is a transfer of an HTML page by the server to the client against the request made by the client.

Conclusion ✉️

In this blog, we learned accessing the API from Python modules in web2py using functions, warnings about what we should or should not do while assigning any global object, and how to share international things with modules using the current objects.

Also read,

 

Refer to our guided paths on Coding Ninjas Studio to learn more about C programming, DSA, JavaScript, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass