Python: How to create simple web pages without a huge framework? [closed]
Asked Answered
P

8

8

I would like to know if there is a way to create web pages without a huge framework in python.

I think of something like PHP/Apache, which comes just as a language and not with to much overhead (but I don't like PHP...). In PHP there is no ORM, no template engine, etc. But it is very very easy to just print a Hello World to the browser.

I know about Django and really like it, but it is a bit too big for simple web portals (5-10 pages).

I really like something simple, without installing too much.

Pardoes answered 2/4, 2012 at 15:38 Comment(3)
What do you expect the framework to do? You only said what it is supposed not to do. If it shouldn't do anything at all, how about just writing HTML pages with a good editor?Shue
The same that PHP does, getting POST/GET values, processing, echo`ing.Pardoes
Please update your question, and try to be as specific as possible. Otherwise, this question is "post your favourite lightweight Python web framework", which isn't a good fit for SO.Shue
I
6

Have you looked up Flask?

It's a much more minimalistic framework, and very easy to set up and get started.

Impassive answered 2/4, 2012 at 15:39 Comment(3)
Bottle is a similar minimalist framework that I like using: bottlepy.org/docs/devBurl
Yes, I know of bottle, I believe flask even derived from it.Impassive
yeah! That's it! Thank you. That's exactly what I searched. Simple.Pardoes
C
4

I've used Flask (and bottle.py) in the past, but these days I actually prefer Pyramid, from the Pylons folks.

Pyramid is capable of being a large, full-fledged framework, is designed for flexibility, and has no shortage of plugins and extensions available adding additional functionality -- but it also is capable of small, single-file projects; see this tutorial for an example.

Going with Pyramid will give you room to grow if your needs expand over time, while still retaining the ability to start small.

Crissman answered 2/4, 2012 at 15:47 Comment(3)
yeah, pyramid seems cool, but flask is really the better solution for my 'problem', because I would switch to Django for bigger things.Pardoes
@DavidHalter That only works if you know up-front if your project is going to be a "bigger thing". If it starts small and then grows organically, having the only path be a complete rewrite kinda' sucks. (Also, as a former Django developer myself, I'm a little unclear on why you'd want to switch to something with a godawfully horriffic ORM [at least compared to SQLAlchemy, the standard by which all others should be measured], a painfully slow templating engine, a less pluggable URL routing mechanism, and so forth... but that's a completely separate conversation).Crissman
Oh, it seems like I have to checkout Pyramids as well. I hate the Django ORM for its inabillity to handle composite keys. And the template engine is really really slow, you're right (it's much better with PyPy). Pyramid sounds really good. I have to give it a try.Pardoes
E
2

Good old CGI is the quickest way to get you started. On most configurations, you just need to drop a python script in 'cgi-bin' and make it executable, no need to install anything. Google for "cgi python", there are lots of tutorials, e.g. this one looks pretty decent.

Ending answered 2/4, 2012 at 15:44 Comment(0)
C
1

I'm not sure what's wrong with django flatpages for your purposes.

Another alternative would be to replace the django template system with something more powerful, like jinja, so you can write your tag soup and do processing there, with minimal logic in the view.

In practice (given that you already know django), that is likely to be easier than using a microframework (which requires more of the programmer, in exchange for being completely unopinionated about anything).

Classify answered 2/4, 2012 at 15:41 Comment(0)
S
0

mod_python perhaps?

Sanctus answered 2/4, 2012 at 15:39 Comment(2)
How does mod_python let you do that? Please explain what that is, and provide an example if possible, so people don't have to click through.Wilmington
anybody who knows about Apache will understand at least approximately from the name what mod_python does.Sanctus
H
0

I would recommend CherryPy

import cherrypy
class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

cherrypy.quickstart(HelloWorld())
Hasa answered 2/4, 2012 at 15:42 Comment(0)
M
0

Sure, you can go really lean with the CGI or wsgiref route. However, you get what you pay for, and I prefer Flask or WerkZeug for all the pain they prevent.

From wsgiref python docs:

from wsgiref.simple_server import make_server

def hello_world_app(environ, start_response):
    status = '200 OK' # HTTP Status
    headers = [('Content-type', 'text/plain')] # HTTP Headers
    start_response(status, headers)
    return ["Hello World"]

httpd = make_server('', 8000, hello_world_app)
print "Serving on port 8000..."

# Serve until process is killed
httpd.serve_forever()
Mercier answered 2/4, 2012 at 15:45 Comment(0)
H
0

Python works well using CGI.

that's the simplest thing you can do: it only needs apache and a working python environment, and is the closest to a standard php setup.

remember that, when using CGI, your python script is responsible for outputting the necessary HTTP headers (sys.stdout.write('Content-Type: text/html\n\n')), but there is a CGI module which is part of the python standard library which greatly helps dealing with the raw stuffs (post/get arguments parsing, header retrieval, header generation).

Halfsole answered 2/4, 2012 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.