Jonathan Jonathan - 1 year ago 147
Python Question

How do I create a web interface to a simple python script?

I am learning python. I have created some scripts that I use to parse various websites that I run daily (as their stats are updated), and look at the output in the Python interpreter. I would like to create a website to display the results. What I want to do is run my script when I go to the site, and display a sortable table of the results.

I have looked at Django and am part way through the tutorial, but it seems like an awful lot of overhead for what should be a simple problem. I know that I could just write a Python script to output simple HTML, but is that really the best way? I would like to be able to sort the table by various columns.

I have years of programming experience (C, Java, etc.), but have very little web development experience.

Thanks in advance.

Answer Source

If you are creating non-interactive pages, you can easily setup any modern web server to execute your python script as a CGI. Instead of loading a static file, your web server will return the output of your python script.

This isn't very sophisticated, but if you are simply returning the output without needing browser submitted date, this is the easiest way (scaling under load is a different story).

You don't even need the "cgi" module from python, if you aren't receiving any data from the browser. Anything more complicated than this and you should use a web framework.

Examples and other methods

  • Simple Example: hardest part is webserver configuration
  • mod_python: Cut down on CGI overhead (otherwise, apache execs the python interpreter for each hit)
  • python module cgi: sending data to your python script from the browser.


Javascript side sorting: I've used this javascript library to add sortable tables. This is the easiest way to add sorting without requiring additional work or another HTTP GET.

Download this file
Add to your HTML
Add class="sortable" to any table you'd like to make sortable
Click on the headers to sort

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download