Table of contents
1.
Introduction
2.
HTML Helpers
3.
Types of Helpers
3.1.
Built-in Helpers
3.2.
Custom Helpers
3.2.1.
TAG
3.2.2.
MENU
3.2.3.
BEAUTIFY
4.
Frequently Asked Questions
4.1.
What is the use of web2py?
4.2.
What is the web2py framework?
4.3.
Is web2py a good framework?
4.4.
Does web2py support Python 3?
4.5.
Which is better, web2py or Django?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Custom helpers 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 framework that is both Python-programmable and designed for  Agile development of secure database-driven online applications. As a full-stack framework, web2py has every element required to create entirely working web applications. Web2py framework applications utilize models, Controllers, and Views. To make the code more understandable and without limiting how web2py should use Python, it contains a slightly modified version of the Python syntax in the Views. 

web2py intro image

The helper class comes under views and enables the developers to construct HTML programmatically. In the following article, we will dive deeper into this helper class and discuss specifically the custom helpers.  

HTML Helpers

Developers can use the helper class in web2py to create HTML programmatically. It is comparable to HTML tags, often called "HTML helpers."

Example:

[(A('Home', _href = URL('default', 'home')), False, None, []), ...]
You can also try this code with Online Python Compiler
Run Code

In this case, A is the helper that corresponds to the HTML anchor tag, or a>. Programmatically, the HTML anchor tag (a>) is created.

There are two categories of HTML helpers: positional parameters and named arguments.

Positional arguments are treated as objects in the space between the open and close HTML elements.

Assigning an underscore to a named argument causes it to be treated as an HTML tag.

Types of Helpers

Helpers are of two types:

  1. Built-in Helpers
  2. Custom helpers

Built-in Helpers

Web2py uses a lot of built-in tools. The following is a list of some of the built-in HTML aids.

Custom Helpers

These helpers are used to modify the tags per the specifications. Web2py utilizes the following unique helpers

TAG

Web2py uses TAG as the universal tag generator. It facilitates the creation of unique XML tags. The following generic syntax is used:

{{ = TAG.name('a', 'b', _c = 'd')}}
You can also try this code with Online Python Compiler
Run Code

The XML code is generated as :

<name c = "d">ab</name>

The functions TAG.name and TAG['name'] return a temporary auxiliary class for the object TAG.

MENU

To create a tree-like structure representing the menu, the helper creates a list of the list items or the values of the menu items. The menu is organized as a list of response.menu items.

Example:

print MENU([['One', False, 'link1'], ['Two', False, 'link2']])
You can also try this code with Online Python Compiler
Run Code

The output is will be−

<ul class = "web2py-menu web2py-menu-vertical">
   <li><a href = "link1">One</a></li>
   <li><a href = "link2">Two</a></li>
</ul>

BEAUTIFY

It aids in creating representations of composite objects, such as dictionaries and lists.

Example :

{{ = BEAUTIFY({"a": ["hello", XML("world")], "b": (1, 2)})}}
You can also try this code with Online Python Compiler
Run Code

It provides a representation of the function Object() { [native code] } parameter in the form of an XML object serializable to XML. The representation in this scenario would be:

{"a": ["hello", XML("world")], "b": (1, 2)}


The output will be rendered as −

<table>
   <tr>
      <td>a</td>
      <td>:</td>
      <td>hello<br />world</td>
   </tr>
   
   <tr>
      <td>b</td>
      <td>:</td>
      <td>1<br />2</td>
   </tr>
</table>

Frequently Asked Questions

What is the use of web2py?

Web developers can use Python to program dynamic web content thanks to web2py. Although a web developer can still create a form from scratch if necessary, web2py is intended to help decrease tiresome web development activities, such as making web forms from scratch.

What is the web2py framework?

web2py, which is written in Python and programmable in Python, is described as a free, open-source online framework for agile development that involves database-driven web applications. It is a full-stack framework with every part a developer might require to create a fully functional web application.

Is web2py a good framework?

It is a trustworthy and stable Python web framework with rock-solid APIs. Web2py allows for highly secure web applications. Because the batteries are already in place, developers do not have to worry about creating standard web components. It runs its web application faster because of the Rocket WSGI.

Does web2py support Python 3?

Web2py functions on Python 2.7 and Python 3 along with CPython (the C implementation) and PyPy (Python written in Python).

Which is better, web2py or Django?

Due to its smaller size, short learning curve, and lack of project-level configuration files, web2py differs from Django. Compared to PHP-based frameworks and Java-based frameworks, web2py has a significantly more precise syntax. This makes applications easier to comprehend, maintain, and create.

Conclusion

In this article, we have extensively discussed various Custom helpers in web2py. We began with a brief introduction to web2py followed by its helper class and examples. 

After reading about the Custom helpers in web2py, refer to web2py web framework, What is web2py web frameworkweb2py introductionweb2py installationCreating a New Web2Py ApplicationWeb2Py - Prepare the ToolWeb2Py - Troubleshooting Use Cases, and Web2Py - Capacity Planning Use Cases, Application development using PythonIntroduction to Python- Coding Ninjas for a deeper understanding of web2py development and other related topics.

Thank you image
Live masterclass