views:

63

answers:

3

Hi, I'm just starting out learning python (for webapp usage) but I'm confused as to the best way for using it with html files and dynamic data. I know php has the tags which are great - does python have something like this or equivalent, if not how should I be doing it?

Thanks

+4  A: 

There are a few php style python options out there. Mod_python used to ship with one, and spyce had an alternate implementation, but that modality is out of favor with pythonistas. Instead, use a templating language. Genshi and jinja2 are both popular, but there are lots to choose from.

Since you are new to web programming with python, you would probably be best off choosing an entire framework get started. Django, turbogears, and cherrypy are a few to check out. These frameworks will all include all the tools you need to make a modern website, including a templating language.

easel
+6  A: 

PHP inline tags actually tend to encourage a bad practice - namely, mixing your control logic (PHP code) with your templates (html). Most Python frameworks tend to steer away from such a mix and instead encourage separation between the two - typically using some kind of templating system.

Many frameworks have their own template systems (for instance, the Django templating system is fairly popular). Others don't and let you choose a separate template engine (like Cheetah).

Amber
I'd +2 if I could for referencing the fact that python frameworks tend to explicitly react against mixed HTML and control tags. As an aside, Django does allow alternate templating systems (with even greater ease than ever in version 1.2 http://docs.djangoproject.com/en/1.2/ref/templates/api/#using-an-alternative-template-language).
Jarret Hardie
I'd also +2 if I could for giving a good, solid reason other than 'fashion' for why 'Pythonistas' do not like PHP style templates.
Omnifarious
+1  A: 

Yes, Python does have a roughly equivalent feature: Python Server Pages (a.k.a. psp) using mod_python. It enables you to do things like:

<html>
 <% import time %>
 Hello world, the time is: <%=time.strftime("%Y-%m-%d, %H:%M:%S")%>
</html>

However, in general this practice is not considered Pythonic. I would start with a lightweight Python web framework. web2py is a nice one to use when you are starting out. bottle, flask & cherrypy are very easy to work with out of the box. Django, TurboGears & others are are more complex, but provide more features.

Tim McNamara