views:

175

answers:

3

I'm just starting out with Python and have practiced so far in the IDLE interface. Now I'd like to configure Python with MAMP so I can start creating really basic webapps — using Python inside HTML, or well, vice-versa. (I'm assuming HTML is allowed in Python, just like PHP? If not, are there any modules/template engines for that?)

What modules do I need to install to run .py from my localhost? Googling a bit, it seems there're various methods — mod_python, FastCGI etc.. which one should I use and how to install it with MAMP Pro 1.8.2?

Many thanks

+2  A: 

Hi there:

I think probably the easiest way for you to get started is to work with something like Django. It's a top-to-bottom web development stack which provides you with everything you need to develop and run a backend server. Things can be very simple in that world, no need to mess around with mod_python or FastCGI unless you really have the need.

It's also nice because it conforms to WSGI, which is a Python standard which allows you to plug together unrelated bits of reusable code to add specific functionality to your web app when needed (say for example on-the-fly gzip compression, or OpenID authentication). Once you have outgrown the default Django stack, or want to change something specific you can go down this road if you want.

Those are a few pointers to get you started. You could also look at other alternative frameworks such as TurboGears or paste if you wanted but Django is a great way to get something up and running quickly. Anyway, I'm sure you'll enjoy the experience: WSGI makes it a real joy knocking up web apps with the wealth of Python code you'll find on the web.

[edit: you may find it helpful to browse some of the may Django related questions here on stack-overflow if you run into problems]

jkp
But, erm, I'm an absolute beginner so I thought I'd start with something really basic and then move to Django. Are you saying Django is easier to use than writing HTML manually in Python? I must check it out then! :)
Nimbuz
Absolutely: go follow the tutorial I linked: it will get you running something out of the box in minutes. Much much easier than having to understand the stack from top-to-bottom. And it's written in a way that gives you a lot of room to grow once you need it.
jkp
Oo! It doesn't support Python 3 yet. :(
Nimbuz
Nothing much in the web arena supports Python 3 yet. :-( Even WSGI itself hasn't been properly finalised for the new version. Personally I think it is indeed worth doing some low-level work before considering which framework, if any, you would like to work with. Whatever you do, ensure it goes through WSGI for portability.
bobince
I would also recommend Django - even if you're not overly familiar with Python Django makes everything very easy and it would still be a hands-on way to improve your Python. If however, you don't want to do that, look into FastCGI, I've done some cgi python scripting in the past and it worked out well for extremely small scale stuff.
hora
Ohk, I was learning Python 3 all this while, well, have to familiarize myself with 2.x too then! Thanks. :)
Nimbuz
A: 

Django is definitely not the easiest way.

check out pylons. http://pylonshq.com/

also check sqlalchemy for sql related stuff. Very cool library.

On the other hand, you can always start with something very simple like mako for templating. http://www.makotemplates.org/

prime_number
+2  A: 

You asked whether HTML is allowed within Python, which indicates that you still think too much in PHP terms about it. Contrary to PHP, Python was not designed to create dynamic web-pages. Instead, it was designed as a stand-alone, general-purpose programming language. Therefore you will not be able to put HTML into Python. There are some templating libraries which allow you to go the other way around, somewhat, but that's a completely different issue.

With things like Django or TurboGears or all the other web-frameworks, you essentially set up a small, stand-alone web-server (which comes bundled with the framework so you don't have to do anything), tell the server which function should handle what URL and then write those functions. In the simplest case, each URL you specify has its own function.

That 'handler function' (or 'view function' in Django terminology) receives a request object in which interesting info about the just-received request is contained. It then does whatever processing is required (a DB query for example). Finally, it produces some output, which is returned to the client. A typical way to get the output is to have some data passed to a template where it is rendered together with some HTML.

So, the HTML is separated in a template (in the typical case) and is not in the Python code.

About Python 3: I think you will find that the vast majority of all Python development going on in the world is still with Python 2.*. As others have pointed out here, Python 3 is just coming out, most of the good stuff is not available for it yet, and you shouldn't be bothered about that.

My advise: Grab yourself Python 2.6 and Django 1.1 and dive in. It's fun.

Juergen Brendel