Separation of Code in CGI App

R

Rob Cowie

Hi,

I need to create a planner/calendar system using python cgi scripts. It
is my first CGI app (beyond a few tutorial examples).

I wish to separate the code according to function. For instance, the
code to handle the DB connection, SQL querying, HTML form variable
retrieval, rendering of HTML via some sort of template system etc. Each
will be a class instantiated when the CGI app is called.

To do this, do I just create each of these classes in one, monolithic
..cgi file? or is it possible to separate these out into different
scripts and have a 'controller' script call them and pass the relevant
parameters/data? I guess I could also store the python code in text
files and use the 'execfile()' function, although it seems to me that
would be an uneccasary performance hit.

If that is possible, is that the right way to go? If not, suggestions
would be gratefully recieved!
 
S

Sybren Stuvel

Rob Cowie enlightened us with:
I need to create a planner/calendar system using python cgi scripts.
It is my first CGI app (beyond a few tutorial examples).

Are you sure you want to use CGI? I'd go for mod_python instead if at
all possible, since that'll give you a much better performance.
To do this, do I just create each of these classes in one,
monolithic .cgi file?

Please don't. It's much better to put things into modules, and import
them when needed.
or is it possible to separate these out into different scripts and
have a 'controller' script call them and pass the relevant
parameters/data? I guess I could also store the python code in text
files and use the 'execfile()' function, although it seems to me
that would be an uneccasary performance hit.

Why that difficult? What's wrong with just doing "from htmlgenerator
import pagexy"?
If that is possible, is that the right way to go? If not,
suggestions would be gratefully recieved!

Check out http://www.unrealtower.org/mycheetah

Sybren
 
R

Rob Cowie

So, do I separate out whatever code I want to into .py modules, then
have the cgi script import them, and utilise them as required?

If so, do I need to put these modules somewhere described in the System
Path? Can I modify the PATH at runtime to point to my modules? How?

Cheers for being patient!
 
P

Paul McNett

Rob said:
So, do I separate out whatever code I want to into .py modules, then
have the cgi script import them, and utilise them as required?
Bingo!


If so, do I need to put these modules somewhere described in the System
Path?

Put them in the same directory, and you don't have to worry any more
about it. You can get more advanced if you want by putting your modules
into a "package", which is just a directory with a __init__.py file -
see the docs at python.org.

Can I modify the PATH at runtime to point to my modules? How?

If you want to have them in a different directory, and the directory
isn't already in sys.path, then just issue, from your main script,
before your import statements:

import sys
sys.path.insert(0, "/path/to/your/modules")

Cheers for being patient!

Welcome to Python! Being new to it, you may want to consider having
someone review your code before putting it on the public internet,
simply because there are lots of opportunities for inadvertantly
providing backdoors (Python is powerful). This is especially true if you
let your users type into a text box, or if you do dynamic database
queries, or both.

Also, have you run through the tutorial on python.org?
 
R

Rob Cowie

Thanks,

I have run through the tutorial - very good. One day, I'd like to try
and organise a similar tutorial involving CGI programming. There are
several very basic examples, but they leave plenty of unanswered
questions such as how to best structure a cgi app, apache permissions,
etc

So to be clear, if a .py module is in the same directory as my python
cgi script, I can import it thus 'import XXX.py'?
 
P

Paul McNett

Rob said:
So to be clear, if a .py module is in the same directory as my python
cgi script, I can import it thus 'import XXX.py'?

Not quite. If you had a XXX.py, XXX.pyc, and/or XXX.pyo in the same
directory as your Python cgi script, you'd import it by issuing:

import XXX
 
B

Bruno Desthuilliers

Rob Cowie a écrit :
Hi,

I need to create a planner/calendar system using python cgi scripts. It
is my first CGI app (beyond a few tutorial examples).
(snip)

You may want to have a look at the cgi_app mini-framework:
-> http://thraxil.org/code/cgi_app/

It's a port of a Perl framework, and it has some unpythonic flavour in
some points (but that can be easily corrected), and it can work with CGI
and mod_python. It's quite easy to get started with, and the source
being really small (about 1kloc IIRC), it's quite easy to customize to
your own needs (I rolled my own mod_python-only/SimpleTAL-only version
in half a day...).

You also have the choice between 2 template systems, one that's dirty as
can be IMHO (htmltmpl) and one that is quite nice (SimpleTAL). The only
wart with SimpleTAL is that it requires every input to be unicode, but
that's easy to solve also (I wrote a wrapper class that handle this
quite transparently - it's still Q&D but works well so far, I can
publish it if anyone is interested).

My 2 cents
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top