Web2py

Last updated
web2py Enterprise Web Framework
Original author(s) Massimo Di Pierro
Initial releaseSeptember 27, 2007;16 years ago (2007-09-27)
Stable release
2.27.1 [1]   OOjs UI icon edit-ltr-progressive.svg / 16 November 2023;8 months ago (16 November 2023)
Repository Web2py Repository
Written in Python
Platform Cross-platform
Type Web application framework
License GNU Lesser General Public License version 3 (LGPLv3)
Website www.web2py.com

Web2py is an open-source web application framework written in the Python programming language. Web2py allows web developers to program dynamic web content using Python. Web2py is designed to help reduce tedious web development tasks, such as developing web forms from scratch, although a web developer may build a form from scratch if required. [2]

Contents

Web2py was originally designed as a teaching tool with emphasis on ease of use and deployment. Therefore, it does not have any project-level configuration files. The design of web2py was inspired by the Ruby on Rails and Django frameworks. Like these frameworks, web2py focuses on rapid development, favors convention over configuration approach and follows a model–view–controller (MVC) architectural pattern.

Overview

Web2py is a full-stack framework in that it has built-in components for all major functions, including:

Web2py encourages sound software engineering practices such as

Web2py uses the WSGI protocol, the Python-oriented protocol for communication between web server and web applications. It also provides handlers for CGI and the FastCGI protocols, and it includes the multi-threaded, SSL-enabled Rocket [6] wsgiserver.

Distinctive features

Web-based integrated development environment (IDE)

All development, debugging, testing, maintenance and remote database administration can (optionally) be performed without third-party tools, via a web interface, itself a web2py application. Internationalization (adding languages and writing translations) can also be performed from this IDE. Each application has an automatically generated database administrative interface, similar to Django. The web IDE also includes web-based testing.

Applications can also be created from the command line or developed with other IDEs. [7] Further debugging options: [8]

The Hello World program with web2py in its simplest form (simple web page [14] with no template) looks like:

defhello():return'Hello World'

Web2py includes pure Python-based template language, with no indentation requirements and a server-side Document Object Model (DOM). The template system works without web2py. [15] Joomla 1.x templates can be converted to web2py layouts. [16]

Web2py also includes two markup libraries: the markdown2 text-to-HTML filter, which converts Markdown markup to HTML on the fly; and markmin which is inspired by markdown but supports tables, html5 video/audio and oembed protocol.

A controller without a view automatically uses a generic view that render the variables returned by the controller, enabling the development of an application's business logic before writing HTML. The "Hello World" example using a default template:

defhello():returndict(greeting='Hello World')

The dict() output of an action is automatically rendered in HTML if the page is request with a .html extension, in JSON if the page is requested with a .json extension, in XML if requested with .xml. It supports other protocols including jsonp, rss, ics, google maps, etc. and is extensible.

Here is a more complex code example which defines a table, and exposes a grid to logged in users:

db.define_table('thing',Field('name',notnull=True))@auth.requires_login()defhello():returndict(grid=SQLFORM.grid(db.thing))

Ticketing system

Each web2py application comes with a ticketing system:

Portable cron

Cron is a mechanism for creating and running recurrent tasks in background. It looks for an application-specific crontab file which is in standard crontab format. Three modes of operation are available:

Scheduler

Since version 2.3 the use of cron is discouraged since web2py comes with a master/worker scheduler. Jobs can be defined in models and are scheduled by creating an entry in the database. Users can start work processes who pickup and execute tasks in background. The schedule is better than cron because it allows to specify more parameters (start time, stop time, number of repetitions, number of trials in case of error) and do a better job at running within constant resource utilization.

Bytecode distribution

Web2py can compile web applications for distribution in bytecode compiled form, without source code. Unlike frameworks that use specialized template languages for their views, Web2py can also compile the view code into bytecode, since it is pure Python code.

Global Environment

Web2py is unique in the world of Python web frameworks because models and controllers are executed, not imported. They are not modules. They are executed in a single global environment which is initialized at each HTTP request. This design decision has pros and cons.

The major pro is the ease of development, specifically for rapid prototyping. Another pro is that all the objects defined within this environment are cleanly reset at each HTTP request and never shared across requests. This means the developer does not need to worry about changing the state of an object (for example the readable attribute of a database field) or worry about a change leaking to other concurrent requests or other applications. A third advantage is that web2py allows the coexistence of multiple applications under the same instance without conflicts even if they use different versions of the same modules or different modules with the same name.

The main disadvantage of the global environment is that model files and controller files are not modules and the order of execution matters (although it can be specified using conditional models). Naming conflict is more likely to occur than in normal Python modules. Some standard Python development tools may not understand objects defined in models and controllers. Moreover, developers must be aware that code in models is executed at every request and this may cause a performance penalty. Nothing in web2py prevents developers from using and importing normal Python modules (model-less approach) and for this purpose web2py provides a thread local object (current) to facilitate access to objects associated to the current request. Yet, in this case, the developer has to be aware of the same pitfalls that other frameworks incur: changing the state of an object defined in a module may affect other concurrent requests.

Another con is that, because models and controllers are not class-based, efficient code reuse becomes more difficult, particularly as the inability to inherit from a parent controller (e.g. the ApplicationController in Ruby on Rails) means that common controller functionality must be referenced repeatedly across all controller files.

Supported environments

Operating systems, Python versions & implementations, virtual machines, hardware

web2py runs on Windows, Windows CE phones, Mac, Unix/Linux, Google App Engine, Amazon EC2, and almost any web hosting via Python 2.7/3.5/3.6/pypy. [2]

The current binary version of web2py (for Windows or Mac) includes Python 2.7, but the source version can be run on 2.7 and 3.5+. Support for Python 2.6 has been dropped on 2017.

web2py since v1.64.0 runs unmodified on Java with Jython 2.5, without any known limitation. [17]

web2py code can run with IronPython on .NET. [18] Limitations:

The web2py binary will [19] run from a USB drive or a portable hard drive without dependencies, like Portable Python.

Web servers

Web2py can service requests via HTTP and HTTPS with its built-in Rocket server, [20] with Apache, [21] Lighttpd, [22] Cherokee, [23] Hiawatha, Nginx and almost any other web server through CGI, FastCGI, WSGI, mod_proxy, [24] [25] [26] and/or mod_python.

IDEs and debuggers

While a number of web2py developers use text editors such as Vim, Emacs or TextMate Web2py also has a built-in web-based IDE. Others prefer more specialized tools providing debugging, refactoring, etc.

Database handling

The database abstraction layer (DAL) of web2py dynamically and transparently generates SQL queries and runs on multiple compatible database backend without the need for database-specific SQL commands (though SQL commands can be issued explicitly).

SQLite is included in Python and is the default web2py database. A connection string change allows connection to Firebird, IBM Db2, Informix, Ingres, Microsoft SQL Server, MySQL, Oracle, PostgreSQL, and Google App Engine (GAE) with some caveats. Specialities:

The DAL is fast, at least comparable with SQLAlchemy and Storm. [31]

Web2py implements a DAL, not an ORM. An ORM maps database tables into classes representing logical abstractions from the database layer (e.g., a User class or a PurchaseOrder class), and maps records into instances of those classes. The DAL instead maps database tables and records into instances of classes representing sets and records instead of higher-level abstractions. It has very similar syntax to an ORM but it is faster, and can map almost any SQL expressions into DAL expressions. The DAL can be used independently of the rest of web2py. [32]

Here are some examples of DAL syntax:

db=DAL('postgresql://user:pass@localhost/db',pool_size=10)db.define_table('person',Field('name'),Field('image','upload'))db.person.insert(name='Martin',image=open('filename.png'))rows=db((db.person.name=='Martin')|db.person.name.contains('T')).select(orderby=db.person.name.lower())

The latest version of the DAL has support for 2D GIS functions with Spatialite and PostGIS. The current API are experimental because of a possible move to 3D APIs.

Automatic database migrations

web2py supports database migrations—change the definition of a table and web2py ALTERs the table accordingly. Migrations are automatic, but can be disabled for any table, and migration is typically disabled when an application is ready for live distribution. Migrations and migration attempts are logged, documenting the changes.

Limitations:

Licenses

Web2py code is released under GNU Lesser General Public License (LGPL) version 3 as of web2py version 1.91.1. [33]

Web2py code before version 1.91.1 was released under GNU GPL v2.0 with commercial exception.

Various third-party packages distributed with web2py have their own licenses, generally public domain, MIT or BSD-type licenses. Applications built with web2py are not covered by the LGPL license.

Web2py is copyrighted by Massimo DiPierro. The web2py trademark is owned by Massimo DiPierro.

Awards

In 2011 InfoWorld ranked web2py highest among the top six Python web frameworks, awarded web2py the Bossie award 2011 for best open source application development software. In 2012 web2py won the InfoWorld Technology of the Year award. [34] [35]

Publications

web2py Book

The base web2py documentation is The Official web2py Book, by Massimo DiPierro. The manual is a full web2py application and it's freely available online, [36] in PDF format or printed form.

Online documentation

Online documentation is linked from the web2py home page, with cookbook, videos, interactive examples, interactive API reference, epydoc s (complete library reference), FAQ, cheat sheet, online tools etc.

Videos

Printed

Background

Developers

The lead developer of web2py is Massimo DiPierro, an associate professor of Computer Science at DePaul University in Chicago. As of 2011, the web2py homepage lists over 70 "main contributors". [38]

Development source code

The web2py development source code is available from the main repository:

Third-party software included in web2py

History and naming

The source code for the first public version of web2py was released under GNU GPL v2.0 on 2007-09-27 by Massimo DiPierro as the Enterprise Web Framework (EWF). The name was changed twice due to name conflicts: EWF v1.7 was followed by Gluon v1.0, and Gluon v1.15 was followed by web2py v1.16. The license was changed to LGPLv3 as of web2py version 1.91.1 on 2010-12-21.

Applications built on Web2py

Notes

  1. "web2py Web Framework".
  2. 1 2 "What is web2py?". web2py.com. Web2py. Retrieved 2023-10-31.
  3. Web2py speaks multiple protocols since v1.63
  4. Using SOAP with web2py
  5. Writing Smart Web-based Forms
  6. Rocket Web Server
  7. Web2py online IDE with It's All Text! Firefox addon and Ulipad (open source Python IDE)
  8. How to debug Web2py applications?
  9. Wing IDE supports debugging for web2py
  10. Eclipse/PyDev supports debugging for web2py
  11. Using web2py on Eclipse
  12. With Winpdb one can do remote debugging over TCP/IP
  13. Encrypted communication in Winpdb
  14. Simplest web page with web2py: "Hello World" example
  15. How to use web2py templates without web2py
  16. Using Joomla templates with web2py
  17. Web2py runs fully on Java and J2EE using Jython
  18. Web2py runs with IronPython on .NET, with limitations
  19. MySQL with web2py Windows binary on a USB thumb-drive
  20. How to run the built-in SSL server
  21. Web2py with Apache and mod_ssl
  22. Web2py with Lighttpd and FastCGI
  23. Web2py with Cherokee
  24. Apache Module mod_proxy
  25. Web2py with mod_proxy
  26. Web2py with mod_proxy and mod_proxy_html
  27. Eric IDE Project
  28. Using Wing IDE with web2py
  29. Distributed transactions with PostgreSQL
  30. Distributed transactions with PostgreSQL — further details
  31. ORM Benchmark
  32. How to use web2py DAL without web2py
  33. web2py License Agreement
  34. Grehan, Rick. "Pillars of Python: Six Python Web frameworks compared". InfoWorld. Retrieved 2017-11-30.
  35. staff, InfoWorld Reviews. "InfoWorld's 2012 Technology of the Year Award winners". InfoWorld. Retrieved 2017-11-30.
  36. "web2py - The official manual online". web2py.com. Retrieved 2018-11-14.
  37. "web2py/web2py-book". GitHub. Retrieved 2018-11-14.
  38. List of main contributors to web2py

Related Research Articles

<span class="mw-page-title-main">PyQt</span> Python GUI library

PyQt is a Python binding of the cross-platform GUI toolkit Qt, implemented as a Python plug-in. PyQt is free software developed by the British firm Riverbank Computing. It is available under similar terms to Qt versions older than 4.5; this means a variety of licenses including GNU General Public License (GPL) and commercial license, but not the GNU Lesser General Public License (LGPL). PyQt supports Microsoft Windows as well as various kinds of UNIX, including Linux and MacOS.

The Web Server Gateway Interface is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language. The current version of WSGI, version 1.0.1, is specified in Python Enhancement Proposal (PEP) 3333.

<span class="mw-page-title-main">Dependency injection</span> Software programming technique

In software engineering, dependency injection is a programming technique in which an object or function receives other objects or functions that it requires, as opposed to creating them internally. Dependency injection aims to separate the concerns of constructing objects and using them, leading to loosely coupled programs. The pattern ensures that an object or function that wants to use a given service should not have to know how to construct those services. Instead, the receiving 'client' is provided with its dependencies by external code, which it is not aware of. Dependency injection makes implicit dependencies explicit and helps solve the following problems:

CherryPy is an object-oriented web application framework using the Python programming language. It is designed for rapid development of web applications by wrapping the HTTP protocol but stays at a low level and does not offer much more than what is defined in RFC 7231.

<span class="mw-page-title-main">Django (web framework)</span> Python web framework

Django is a free and open-source, Python-based web framework that runs on a web server. It follows the model–template–views (MTV) architectural pattern. It is maintained by the Django Software Foundation (DSF), an independent organization established in the US as a 501(c)(3) non-profit.

<span class="mw-page-title-main">Catalyst (software)</span> Open-source web application framework

Catalyst is an open-source web application framework written in Perl. It closely follows the model–view–controller (MVC) architecture and supports a number of experimental web patterns. It is written using Moose, a modern object system for Perl. Its design is heavily inspired by frameworks such as Ruby on Rails, Maypole, and Spring.

Python Paste, often simply called paste, is a set of utilities for web development in Python. Paste has been described as "a framework for web frameworks".

mod_wsgi is an Apache HTTP Server module by Graham Dumpleton that provides a WSGI compliant interface for hosting Python based web applications under Apache. As of version 4.5.3, mod_wsgi supports Python 2 and 3.

Google App Engine is a cloud computing platform used as a service for developing and hosting of web applications. Applications are sandboxed and run across multiple Google-managed servers. GAE supports automatic scaling for web applications, allocating more resources to the web application as the amount of requests increases. It was released as a preview in April 2008, and launched officially in September 2011.

The Wing Python IDE is a family of integrated development environments (IDEs) from Wingware created specifically for the Python programming language with support for editing, testing, debugging, inspecting/browsing, and error-checking Python code.

Tornado is a scalable, non-blocking web server and web application framework written in Python. It was developed for use by FriendFeed; the company was acquired by Facebook in 2009 and Tornado was open-sourced soon after.

<span class="mw-page-title-main">Plack (software)</span>

Plack is a Perl web application programming framework inspired by Rack for Ruby and WSGI for Python, and it is the project behind the PSGI specification used by other frameworks such as Catalyst and Dancer. Plack allows for testing of Perl web applications without a live web server.

<span class="mw-page-title-main">Flask (web framework)</span> Python web framework

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies and several common framework related tools.

<span class="mw-page-title-main">Gunicorn</span> Python web server

The Gunicorn "Green Unicorn" is a Python Web Server Gateway Interface (WSGI) HTTP server. It is a pre-fork worker model, ported from Ruby's Unicorn project. The Gunicorn server is broadly compatible with a number of web frameworks, simply implemented, light on server resources and fairly fast. It is often paired with Nginx, as the two have complementary features.

Pylons Project is an open-source organization that develops a set of web application technologies written in Python. Initially the project was a single web framework called Pylons, but after the merger with the repoze.bfg framework under the new name Pyramid, the Pylons Project now consists of multiple related web application technologies.

<span class="mw-page-title-main">Yesod (web framework)</span>

Yesod is a web framework based on the programming language Haskell for productive development of type-safe, representational state transfer (REST) model based, high performance web applications, developed by Michael Snoyman, et al. It is free and open-source software released under an MIT License.

PythonAnywhere is an online integrated development environment (IDE) and web hosting service based on the Python programming language. Founded by Giles Thomas and Robert Smithson in 2012, it provides in-browser access to server-based Python and Bash command-line interfaces, along with a code editor with syntax highlighting. Program files can be transferred to and from the service using the user's browser. Web applications hosted by the service can be written using any WSGI-based application framework.

<span class="mw-page-title-main">Jam.py (web framework)</span> Python web framework

Jam.py is free and open-source low-code/no-code "full stack" WSGI rapid application development framework for the JavaScript and Python programming language.

The Asynchronous Server Gateway Interface (ASGI) is a calling convention for web servers to forward requests to asynchronous-capable Python frameworks, and applications. It is built as a successor to the Web Server Gateway Interface (WSGI).