views:

152

answers:

4

If I want to write a webpage in Python, where do I begin?

  • Do I save my file as "index.py"?
  • Can I mix Python and HTML? what does that look like?

EDIT:

I want to learn how the Python process works on the web. I want to know if I can put Python into an existing webpage and having it render the Python code. I'm not asking about frameworks.

EDIT2:

It seems that I'm going to try Google App Engine. Thanks to all those who responded.

+4  A: 

There are many frameworks you can use. Look for "getting started pages" in the following frameworks:

They all look a bit differently, so choose what you like.

viraptor
A: 

well you dont "NEED" Django, but it does makes it easier.. it's a good way to start.. i've never developped webs in python and django was really helpful

where do I begin?

Like anything else in programming: RTFD and STFW

Do I save my file as "index.py"?

In Django nop, you'll need to create some files (views, models and templates) but you can create a "template" for your index.html

Can I mix Python and HTML? what does that look like?

Yes we can!.. but it looks uglier than a monkey sucking lemons:

html = "<html><body>"
html += "<p>hello world</p>"
...
return HttpResponse(html)

(i really dont like mixing html to any language)

Good Luck

EDITED if you rather not use frameworks.. read this, i think this is what you want http://wiki.python.org/moin/CgiScripts

pleasedontbelong
What if I'm not using Django? I wanna try a Python on a GoDaddy.com linux server.
Moshe
i dont have the answer to that.. =) i tried to develop without using frameworks in python and it was too complicated for me =P its not as easy as php
pleasedontbelong
A: 

You can do a pure cgi solution, if you're not looking for frameworks:

http://www.cs.virginia.edu/~lab2q/index.html

Looks pretty good, but I haven't researched extensively.

For any real application, you're better off using a framework like django.

babbitt
+9  A: 

At its most basic level, a Python script works like any CGI program. A Web request is sent to the server. The server sees that the URL is mapped to a Python script, so it runs your program, passing along information about the request (HTTP request headers, etc.). Your script receives those parameters and returns HTTP response headers and, in most cases, a valid HTML page, unless your script is doing a redirect or something rather than an HTML response. All this is done by simply sending the headers and HTML to standard output, e.g. using print or the like, in the appropriate format. Then your script ends.

Because HTTP is a stateless protocol (that is, it does not inherently have any concept of a "user" or a "session"), you have to keep track of users' state data by sending them cookies. If you have multiple scripts, you have to arrange to save any information that needs to be passed from one script to another, using cookies or hidden fields in a form or special URLs. (You can also save some of that information server-side and simply provide a session cookie.)

Generally you do not embed Python code in HTML as you would with PHP or ASP. Instead your Python script would use a template, possibly reading it from a file, and substitute the necessary values into it.

All this quickly becomes a major pain to do yourself as your application gains complexity (I do recommend it as a learning experience, however -- every Web developer should know how HTTP and CGI work). This is where the frameworks that others are mentioning come in, by doing much of that for you, providing abstractions that make HTTP seem like it has user sessions, templating mini-languages, and so forth.

kindall
+1, good answer, I wish I could +1 you again for the "every Web developer should know how HTTP and CGI work" part.
Mark
This explains the concept, even though I am going with te App Engine in the end.
Moshe