top | item 3972327

Python Web Programming

152 points| plessthanpt05 | 14 years ago |docs.python.org | reply

54 comments

order
[+] DizzyDoo|14 years ago|reply
A good article, but I don't want the Flask web framework (http://flask.pocoo.org/) to go unnoticed. I struggled along with Django for a number of months, and since I was just beginning web development I felt like I was fighting the large framework, even to build the simplest app. Upon discovering Flask I found it refreshingly simple, and when I moved on to more complicated applications I found it scaled beyond 'toy' apps. It really is worth a look for any Python developer.
[+] teilo|14 years ago|reply
I've used both Flask and Django for apps that are both simple and complex. What I have discovered, is that if I use Flask for a complex app, I need to organize my code in clean components that end up looking like Django. Yes, I can do this more-or-less in a freeform manner with Flask, and even use the "one file for everything" method, but for a complex app, I wouldn't want to. It's too hard to manage.

So, in the end, I end up mimicking the basic structure of a Django project: Models, Views, Forms, Utilities, etc. Yes, different ORM, different Form library, WSGI middleware instead of context processors, etc., but all-in-all, no less complex than had I used Django. It's really to the point that I find it hardly any faster to code in Flask for all but the most basic sites.

I still use Flask for super simple projects that require no Admin interface, such as Personalized URL sites for ad campaigns. So it certainly has it's place.

[+] tjr|14 years ago|reply
Anyone have experience with Flask vs. CherryPy? I started using CherryPy a few years ago and really liked it, but recently Flask [which looks conceptually similar to CherryPy] seems to be (amongst minimal Python web frameworks) the more hip and popular choice.
[+] LeonidasXIV|14 years ago|reply
At the time of writing, there was no Flask and Werkzeug wasn't really popular by then. An updated version would mention Flask of course.
[+] davepeck|14 years ago|reply
It's worth pointing to Graham Dumpleton's blog [1] -- he seems to be the Python world's web hosting ninja. See the great slide deck [2] from his recent talk on hosting python web apps [3] -- the deck nicely captures the recent state of the world.

Apache2 + mod_wsgi in Daemon mode is probably the most common approach (especially for VPSs), although I know several people who prefer nginx + gunicorn + supervisor. Both are sane and stable options at this point. (The python web frameworks themselves all speak WSGI and there are about seventeen million of 'em to choose from.)

[1] http://blog.dscpl.com.au/

[2] http://modwsgi.googlecode.com/files/mod_wsgi-pycon-sydney-20...

[3] http://www.youtube.com/watch?v=PWIvm-uloMg

[+] streptomycin|14 years ago|reply
Seems to me that a better title for this article would be "A history of different ways to use Python in the web" rather than "HOWTO Use Python in the web", since most of the article is spent describing old things that should not be used for new projects.
[+] LeonidasXIV|14 years ago|reply
Author here: While I agree that part of this guide are indeed outdated and could be worth an update (which I haven't had the time to since some years) I still think the general basics are useful. I saw many people get into web programming without having a slightest clue how HTTP works, sometimes even after coming from PHP.
[+] brendn|14 years ago|reply
Interesting that there's no mention of Tornado [1]. (Disclosure: as a co-author of O'Reilly's Introduction to Tornado, I'm biased in its favor.) I haven't played around with Brubeck [2] yet, but I also hear many great things about it.

Needless to say, there are some great Python web servers and frameworks out there that aren't listed on that page.

[1] http://www.tornadoweb.org/

[2] http://brubeck.io/

Edit: Added links

[+] kzahel|14 years ago|reply
I'm also confused. Tornado is definitely a more compelling web framework. Who doesn't want the ability to have tons and tons and tons of long-lived connections?
[+] Kilimanjaro|14 years ago|reply
Bottle.py is the simplest way to start playing with web programming in python. Mix it with Jinja and there you have it, good enough for any personal project. Then you make your way up with Flask and Django.

* I wish there was a one-file jinja copycat to make it easier to just drop bottle and jinja and start hacking.

[+] derelk|14 years ago|reply
I agree Bottle is simple and has some nice features, but I'm a little wary of the decorator-based "micro"frameworks. In my experience it can lead to strange code structure and it's more difficult to translate into the class-based handlers of larger frameworks.

Moreover, I don't think the barrier of entry to use Tornado, for example, is significantly higher than Bottle, and you can use it for any sized project. I realize that is both a server and framework, but truthfully I guess I'm also wary of WSGI on the whole and wouldn't recommend anyone even start down that rabbit hole in this day and age.

[+] domodomo|14 years ago|reply
This reminds me, whatever happened to Turbogears? Are many people using it? I never really hear people mention using it compared to Django.

I thought long and hard about the "Should I learn Django or Turbogears" question 3 or four years ago, and went with Django. Back then, Django was not yet the "default" answer.

[+] OptimusSubprime|14 years ago|reply
The TG, Pylons, and Zope guys all joined forces under the Pyramid banner a couple of years ago. There are still people developing in each of those three projects (and I think it's largely still maintained), but the definite trend is to Pyramid itself.
[+] lmm|14 years ago|reply
I'm still using it; don't know if it's even maintained any more, but for my app it works really well.
[+] mistercow|14 years ago|reply
It's a real shame that support for Python Server Pages is as rare as it is in the wild, and that we don't see that kind of capability common in other server side languages. I really think that the ability to mix HTML and server code together painlessly is one of the main advantages that PHP still has today. It's the reason I still use it for new projects even though I dislike almost everything about the language itself.

If I could just say <?coffeescript like that, my programmer-happiness rating would quadruple instantly.

[+] pconf|14 years ago|reply
I'd love to see Python Server Pages as well but not implemented anything like it is in PHP. Mixing View and MVC Controller methods/classes in View pages is simply not a sustainable model. Following the PHP example here would seriously taint Python's potential for webapps. Java's JSP, OTOH, is the reference model for that.

One big plus for Python Server Pages is the ability to pre-compile into bytecode. True Python doesn't have near the level of compile-time checking that Java does, but it that could be written into a framework...

[+] F_J_H|14 years ago|reply
What would be extremely useful is an equivalent of Michael Hartl's "Ruby on Rails Tutorial" (http://ruby.railstutorial.org/ruby-on-rails-tutorial-book) for one of the more popular Python web frameworks...

If someone knows of something similar, please let me know.

[+] n2dasun|14 years ago|reply
I know nothing about python web programming, but I'm hoping to get deep into it very soon. I wanted to point out the framework that I'm planning to use, because I hadn't seen it mentioned in this thread yet.

http://www.web2py.com/

[+] phzbOx|14 years ago|reply
Why is there a link to python's documentation on HN frontpage? I mean, it's not like it's a new release or, wait for it, was anything new.

Here are some others:

   - Python/C API Reference Manual http://docs.python.org/py3k/c-api/
   - Higher-order functions and operations on callable objects http://docs.python.org/library/functools.html
   - Google http://www.google.com
[+] jonathansizz|14 years ago|reply
This link might help you all out: http://docs.python.org/release/3.1.5/index.html or are we going to eventually have every single Python doc page posted to HN?

It there's no Python news, that doesn't mean you should post random tutorials instead. This is getting tiresome. Flagged.

[+] slurgfest|14 years ago|reply
If it's not useful to post random tutorials, how much less useful is it to post links to documentation for obscure Python versions which hardly anybody uses? Do you even use Python for anything?
[+] mattdeboard|14 years ago|reply
Great timing on this. I'm training a very smart & academically accomplished new dev who's spent the last several years pursuing his PhD in CS, but has no idea about web development (I don't consider that a vice).

Fortunately he's one of the awesome, awesome people I can just point at an article and he'll be off and running.

[+] RossM|14 years ago|reply
> Most HTTP servers are written in C or C++, so they cannot execute Python code directly – a bridge is needed between the server and the program.

Are there any HTTP servers written in Python (rather than modules)? I'm struggling to think of any.

[+] LeonidasXIV|14 years ago|reply
Yes, the HTTP server which is part of the Python stdlib: SimpleHTTPServer in 2.x and http.server in 3.x.
[+] seanwoods|14 years ago|reply
CherryPy, Tornado, maybe others...
[+] SpiderX|14 years ago|reply
Aspen, Rocket, Tornado - granted a lot of these have C modules for speed, but they are Python. There are several others.
[+] jefe78|14 years ago|reply
Why is something like this being posted? It's been in the docs for ages. Seems like link bait to all the folks drooling over Python lately(of which I am).
[+] arethuza|14 years ago|reply
I don't know why it was posted, but as a recent convert to Python (and using CherryPy for web development) I'm interested to see what else is out there in the Python ecosystem.
[+] Estragon|14 years ago|reply
I've been using python since 1997, and I found the link interesting.