Web2Py for Everybody

../web2py/carnet-reingart.jpg

Author: Mariano Reingart

Software programmer and teacher. Free software enthusiast, fond of Python, PostgreSQL and Web2Py.

Blog: http://reingart.blogspot.com

Company: http://www.sistemasagiles.com.ar

Introduction to Web2py

Web2py is a web framework that is very easy to use and to learn. It was designed for educational purposes, it includes the latest technologies integrated in a straightforward way (MVC, ORM, templates, javascript, ajax, css, and so on.) making it a fully functional solution to interactive develop web 2.0 applications (both design and programming is done inside the web browser!).

In our humble opinion, web2py makes web development faster, easier and more efficient, allowing focus on business logic rather than trivial or esoteric technical issues. Broadly speaking, its main features are:

  • Straightforward installation, almost zero-configuration (both self-contained and mod_wsgi)
  • Intuitive and with a very low learning curve, suitable to be taught in the classroom for beginners levels
  • Its Database Abstraction Layer (DAL) allows to define tables without complex class (which could be extended later with virtual fields, similar to an ORM) and its query language in Python is very close to SQL, giving it a great declarative power and flexibility.
  • Solid all-inclusive structure, including ajax, menus, forms, caching, EAG, web services (JSON, XML_RPC, AMF, SOAP), scheduled tasks, etc. Its integrated clean and safe design prevents common pitfalls associated with web development
  • Highly pythonic: models, views and simple, clear, explicit and dynamic controllers, with a template language programmable with Python, HTML helpers, bidirectional URL mapping for advanced patterns.
  • With no hassles associated with the command line, it includes an integrated development environment and management tools entirely online with a code editor and html web ticket system error, file uploading, etc.

In this first article we will see the main features and installation, then the intention is to show the capabilities of the tool: the model, controllers, views, authentication, forms, CRUD, ajax, etc.

Installing Web2Py

Web2py comes packaged for various operating systems, so installation is very simple, and "batteries included" philosophy of means that in most cases you do not have to download or install other dependencies (libraries or packages)

Windows

For the Windows operating system, we will find a compressed package with everything you need, just follow the instructions to setup web2py:

  • Download all-in-one package web2py_win.zip
  • Unzip
  • Run (double click) web2py.exe

Mac

Installation for Mac is very similar to Windows, with a compressed package web2py_osx.zip. You just have to unzip it and run web2py.app to launch the program.

GNU/Linux

At the moment there are no packages for different distributions of GNU / Linux, since in most cases you can simply run from source because Python and the main units are usually pre-installed in these environments.

To use web2py from source, follow this steps:

  • Install the dependencies (python and connectors to the database)
  • Download the source code web2py_src.zip
  • Uncompress
  • Run python web2py.py

For Ubuntu (or Debian), open a console and run:

sudo apt-get install python-psycopg2
wget http://www.web2py.com/examples/static/web2py_src.zip
unzip web2py_src.zip
cd web2py
python web2py.py

Quick Tour

Here's a quick snapshot of main features of web2py.

Note: The links works only if web2py is running on the local machine at port 8000 (default configuration).

Important: The Web pages languages are displayed according to browser settings (available: English, Spanish, Portuguese, etc.).

Start

When you start web2py, it will show the splash screen while the program is loading:

http://www.web2py.com.ar/wiki/static/img/bienvenida.png

Then there will be a dialog of the built-in development web server. To start the server choose and enter an administrator password (eg. 'abc') and press start:

http://www.web2py.com.ar/wiki/static/img/server.png

Welcome

When the server starts, web2py will launch a browser with a default welcome page :

http://www.web2py.com.ar/wiki/static/img/welcome.png

This page is the default application, a "skeleton" that is used when we create Web2Py applications.

Basically we have several links to the administrative interface, documentation, interactive examples and a short description about the page you are viewing:

  • You visited the URL .../default/index
  • Which called the function index() located in the file .../controllers/default.py
  • The output of the file is a dictionary that has been rendered by the view .../views/default/index.html

Administrative Interface

Once we have web2py running and we can see the home page, we can begin to create and edit our web applications by going to the administrative interface:

http://www.web2py.com.ar/wiki/static/img/admin0.png

On that page, you must enter the password chosen in the previous step. An index with the installed applications will be shown:

http://www.web2py.com.ar/wiki/static/img/admin1.png

Here we can create new applications, upload or download pre-made applications, edit source code, html files, upload static files, translate messages, review the error log, etc. All these topics we will be addressed in the following articles.

In this case, we will enter the default welcome (welcome) application, by clicking the EDIT link (edit)

http://www.web2py.com.ar/wiki/static/img/admin2.png

And once there, we can modify the main controller source code (default.py) by clicking on the edit link (edit):

http://www.web2py.com.ar/wiki/static/img/admin3.png

The links above allow us to quickly edit the related html templates and test the exposed functionality.

We can see that the code of this hello world program is very simple, we have the index (which is run by default when entering the application), which establishes the flashing message "Welcome to web2py" and returns a dictionary with a variable message = 'Hello World' to be used to generate the webpage:

def index():
  """
  acción ejemplo usando el operador de internacionalización T y mensaje flash
  renderizado por views/default/index.html o views/generic.html
  """
  response.flash = T('Welcome to web2py')
  return dict(message=T('Hello World'))

Note that this is all the code that is used in the function to generate the web page. It is not mandatory to run management scripts, modify configuration files, map URLs with regular expressions and/or to import several modules. Web2Py take care of all these issues for us.

We will end here by now, since the idea of this article was to show a brief introduction to the tool. In later articles we will continue with more advanced topics.

For users who wish to continue experimenting with this tool, we recommend following the interactive examples at: http://www.web2py.com.ar/examples/default/examples, where small code snipes are used to show the main features of the framework.

Summary:

In this article we have introduced web2py, a powerful tool for coding Web sites quickly and easily. The idea is to go deeper and to expand each topic in more detail in later articles.

In our view, is a very good choice to start with web development without losing focus on performing advanced applications in the future.

As an advice, we recommend to subscribe to the Google user group, where you can consult and review the news and updates, since web2py moves at a fast pace, including many new features in each version.

Resources:

PDF version | reSt version

Help PET: Donate

blog comments powered by Disqus

Last change: Thu Sep 9 17:11:52 2010.  -  This magazine is under a Creative Commons license.