views:

189

answers:

6

How to create simple web site with python?

I mean really simple, f.ex, you see text "Hello World", and there are button "submit", which (onClick) will show ajax box "submit successful".

I want to start develop some stuff with Python, and I don't know where to start ;)

+3  A: 

I think you should start with some kind of Python web framework. For me Web2Py is both easy and powerful. Of course you can create your pages using CGI: no framework required, but for more complicated sites it is not practical.

Michał Niklas
+3  A: 

I was hoping more elaborate answers would be given to this question, since it's a sensitive subject. Python web developing is split across lots of frameworks, each with its ups and downs and every developer using a different one. This is quite unfortunate. What you should know:

  • Use WSGI. Don't use anything else, WSGI is the latest standard in Python web developing;
  • Don't develop directly on top of WSGI, unless you really have to (not even for a hello world app);
  • Use a framework that best suits your needs:
    • I played with Werkzeug (which is not really a platform, they call it a toolkit) because it's really simple yet powerful. It lets you work on the WSGI level (also helps you understand how WSGI works) while providing really useful features and helpers. On the Werkzeug website you will also find useful tutorials and things like that.
    • Probably the most popular framework is Django. Never used it, but maybe there's a reason for why it is so popular.

In conclusion, use whatever is closest to your heart.

Felix
Why are you saying "Don't use anything else but WSGI?" if he's asking this question, chances are he's going to be using a test server for awhile before he actually deploys anything, which means he doesn't need to worry about such things (at least not yet).
Luke
@Luke yes, he does. WSGI defines the way your application handles requests, so you have to consider it from the very first line of code. And that "test server" has to support wsgi in some way (for example, for apache, there's mod_wsgi). Also, Werkzeug (and I think Python itself, too) comes with a builtin WSGI based web server, so you can use `run_wsgi(my_app)` for example as a test server.
Felix
+3  A: 

I'd cast my vote for Django. Not just because it's what I use, but also because it has a great tutorial: http://docs.djangoproject.com/en/dev/intro/tutorial01/

Luke
+2  A: 

As Felix suggested, definitely use WSGI (mod_wsgi) as your gateway interface. It is the modern way of doing business and the other major contendor, mod_python, is no longer being maintained.

Django is a great choice if you want a full-fledged production-quality framework but it also comes at the cost of having a lot of overhead and a pretty steep learning curve.

My suggestion is: Tornado!

I have found that Tornado makes it very easy to get up and running quickly. To illustrate here is the "Hello, World" from the Tornado documentation:

import tornado.httpserver
import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start() 

In my opinion that speaks for itself.

Edit: It's important to note that you don't have to use the web server that comes with Tornado. It plugs very easily into WSGI to run with whatever server you already have going.

Best of luck in your search!

jathanism
But Django comes with an on-line book at http://www.djangobook.com/en/2.0/ :)
extraneon
Yes, it does. But you kind of have to read the book to get the full effect. I love Django and I use it heavily, but I think it's overkill for a lot of things.
jathanism
+1  A: 

You can write a web site with Python in which the web server is implemented in Python, or in which Python is called from some other web server. If you do not already have a web server set up, the first option is easier. The Python library includes a fully functional web server, all you have to is add a couple of methods to respond to requests.

For a complete example of a web site using this simple technique, see Making a simple web server in Python

This technique may or may not serve you well for developing commercial, production web sites, but it's the simplest way from P(ython) to W(ebsite).

Larry Lustig
That's a lot more work than this guy wants to do I bet. He wants a webpage, not a webserver.
bradlis7
@bradlis7: See the link. It's considerably less work implementing a web site this way than trying to install and configure Apache, or anything else, to access Python. It is the fastest way to a web site with Python.
Larry Lustig
I would think that creating a CGI is just as well.
bradlis7
Writing the CGI script in Python is about the same amount of effort as the suggestion I linked. However, if you use that suggestion you can avoid downloading and installing a web server and configuring the web server to use Python for CGI. You would be done with the web site while the other person is still installing a web server.
Larry Lustig
+1  A: 

Why don't you try out the Google AppEngine stuff? They give you a local environment (that runs on your local system) for developing the application. They have nice, easy intro material for getting the site up and running - your "hello, world" example will be trivial to implement.

From there on, you can either go with some other framework (using what you have learnt, as the vanilla AppEngine stuff is pretty standard for simple python web frameworks) or carry on with the other stuff Google provides (like hosting your app for you...)

Daren Thomas