I am new to python web frameworks. I am using web.py because I like how raw it is. I am wondering though, how one can produce pages and scripts efficiently when being restricted to sending output through the return keyword? It seems like for any python module you can only send one thing to the browser, even if it is a large string. What am I missing about the python way of server-side scripting? If it helps, I am coming from a PHP perspective. I am used to being able to say print "foo" and foo will appear. Now I am only allowed to print once per module. If you could point me in the direciton of a python approach to scripting vs the php approach I'd much appreciate it! I'm awful confused at this point how python can be so efficient while being so limited.

For example, here is a basic program I wrote:

 import web
 urls = ('/','index')
 class index:
 def GET(self):
     return "foo" ///Where this is the only place on the file I can output anything
 (and the rest of the file you should be familiar with)

The point is that it appears to me that the only place you could output anything is in the one return line? Now I understand that if the URI arguments change, you can map to a different class and thus output something differently, but even then this seems limited?

Or ultimately, is the point of the web framework to use 'templates' as they are called in web.py? Thanks for the help.

I'm not (yet) a specialist of web frameworks in python, but yes, one of the purposes of templating systems is to mix html code and python code. The main idea is to write html files containing python expressions and blocks of code with a special markup syntax. This file is called the template. The templating system provides a render() method which evaluates the template in a certain python context, and you can output the resulting html.

If you want a powerful and simple templating system, try Mako which benefits from the experience of many other python templating engines developped in the last 15 years. Among other things, mako templates can be compiled into python modules for performance. They also implement an inheritance system between templates see here. I think this templating system can very well be used with web.py or other frameworks.

Another nice feature of mako templates is that they can be used for completely different purposes. For example I use them to produce restructured text files, post-processed by rstex and latex to write scientific papers.

This article has been dead for over six months. Start a new discussion instead.