Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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.

like image 759
Jonathan Avatar asked Dec 10 '10 19:12

Jonathan


People also ask

How do I make a simple GUI in Python?

Tkinter ProgrammingImport the Tkinter module. Create the GUI application main window. Add one or more of the above-mentioned widgets to the GUI application. Enter the main event loop to take action against each event triggered by the user.

Can We Do web scripting using Python?

Since JavaScript is the main programming language for making a web browser work, and a web page interactive, Python is usually used for data science, machine learning, and artificial intelligence.


2 Answers

Have you considered Flask? Like Tornado, it is both a "micro-framework" and a simple web server, so it has everything you need right out of the box. http://flask.pocoo.org/

This example (right off the homepage) pretty much sums up how simple the code can be:

from flask import Flask app = Flask(__name__)  @app.route("/") def hello():     return "Hello World!"  if __name__ == "__main__":     app.run() 
like image 125
AndrewF Avatar answered Sep 29 '22 18:09

AndrewF


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.

Sorting

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.

Instructions:
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

like image 43
Bradley Kreider Avatar answered Sep 29 '22 19:09

Bradley Kreider