#1
  1. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14

    Your favourite Python CGI tricks


    I'm starting a project in my spare time to "port" a web development framework I wrote in Perl to Python, before I start two new contracts sometime soon. Whilst porting my existing ideas, I'm also interested in ideas others have developed over the years to make deploying web sites easier.

    At the moment my favourite trick is to keep a separate module, subs.py, containins all the frequently re-used functions and code. Each script is then a matter of calling those functions, grabbing data from a form or a database (and I have a series of mysql wrapper functions because typing query handling code is dull dull dull), building an object with the resultant content, then spitting it into a function that prints the html including a template. I've found it makes writing scripts really quick, because I can focus on the logic and put the boring stuff to the back of my mind.

    I'm also looking into Apache's mod_rewrite functionality to allow me to use URLs like www.site.com/users/tom/settings rather than long, ugly query strings like users.py?id=tom&op=settings.

    What are your tricks? I'll post my new subs.py when it's done.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    It sounds like an interesting in this idea telex, maybe you could explain further? if your looking for any help your welcome to email me

    Anyway like you, whenever i have to write a CGI app in Python I find it helpful to make a module to contain all the functions i need in the app.. though i usually name for the app/page i.e page.html.header() would call the header function for the script and etc.

    Also where possable i created a template page with some special tags in like:

    <template>Error message or some default text</template>

    Then read the template file in and use a simple regex to output content into the webpage.. not really much of a "trick" but hey

    Have fun,
    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    netyan, I used to use my own homebrew templating system in Perl until I discovered HTML::Template. I've come across htmltmpl for Python which does a similar job - you should have a look, it's a great way to simplify separating code from content.

    http://htmltmpl.sourceforge.net/python.html

    Right now I'm trying to work out mod_rewrite more than anything else though.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Looks cool, maybe i'll give it a go when i next do some Python CGI , it seems like a bit of an overkill for simple stuff and since the templating i'm doing right now is just read -> replace -> print

    I usually don't use 3rd party modules if i can do the it myself.. i prefer to use my own code rather than somone else's, if only for the fun factor! But thanks for the recomendation, i'll keep it in mind

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12
    wow telex!

    M$ spent so many years developing .net and I found it quite interesting that they used that same idea exactly - seperating html from the "code behind" the html . I'm very surprised (and very very impressed) to see it in python

    Cheers for the link
    Eli
  10. #6
  11. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    Well of course the main difference between languages like Perl & Python and languages like PHP & ASP is that in the latter two, the code is embedded in the HTML, whereas in Perl and Python the HTML is embedded in the code. I find both solutions annoying though, because I like to develop web site systems that I can then use to deploy many different web sites, just changing certain forms and the HTML template. It also allows companies or groups to work together easily, assinging graphics, HTML work and coding to different groups without worrying about putting it all together.

IMN logo majestic logo threadwatch logo seochat tools logo