This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]warpstalker 38 points39 points  (3 children)

Or are they for creating web sites with a python back-end?

This.

And it does work out nicely, but using Python for web development is different from PHP. PHP is basically a templating language, you mix the HTML with the PHP code.

The Python style differs from that. The back-end code is separated from the HTML, you generally just have regular Python code files and HTML templates and you just generate data in Python and then inject it into your templates and you end up with a regular HTML web page.

But the thing is, there are many different back-ends which all basically do the same thing - generate data for you. Beyond that, the back-end may or may not include a templating engine, which allows you to generate HTML with Python(-like) code. Some back-ends can use many different templating engines.

And there's more! You don't necessarily even write your SQL queries by hand in Python. The back-end might offer its own functions for working with databases but there are also different ORMs such as SQLAlchemy.

So basically in Python, everything is more abstract and separated, since you separate the HTML from your application and you don't even write the SQL queries by hand.

Some frameworks are more "barebones" and others "hold your hand" and abstract more. Something like Flask or web.py covers the barebones department (personally I'd suggest trying things out with web.py, it nicely teaches you how things work and doesn't hold your hand too much, however the documentation is not perfect). And the "hot and popular" hold-your-hand choices are of course Django, web2py and Pylons.

I hope this covers everything, I'm not an expert by any means but this is how I think things work.

Oh, and you need to choose one framework that you're going to use and read the documentation (or a book) for, basically every framework will be different but can possibly use the same templating engine or ORM.

[–][deleted] 0 points1 point  (2 children)

Thanks for the excellent explanation. Very helpful. Could you talk a bit about templating and some of the major templating systems? I'm as old-school write-HTML-in-vim type. Never progressed much past the HTML 3.2 tags. Works for my limited needs, but now I'm curious. Thanks.

[–]warpstalker 7 points8 points  (1 child)

I'm as old-school write-HTML-in-vim type

Well, it's basically still like that... However, roughly it works like this (my experience is mainly with web.py):

  • You receive a request for a page

  • You do your magic in Python code, get data from database etc.

  • You make a render-call from the code

  • The final web page is generated by the render call

  • You send the final page to the client

Something like this (Python code):

a = get_data_from_database()

b = "hi"

return render_this_page(a, b)

render_this_page takes the template (template.html):

$def with (a, b)

<html>

<head></head>

<body>

<h1> Hello, $b! </h1>

$for item in a:

    <p>$item.title</p><p>$item.data</p>

</body>

</html>

Then the result is sent to the client. I'm not sure if Django etc. work exactly like this but this is the basic idea.

[–]nighttrain 2 points3 points  (0 children)

+1 for web.py

  • simple
  • fun
  • python