How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?

38,762

Solution 1

This page on the python site has a good description and example of what you need to do to run a python CGI script. Start out with the simplest case first. Just make a short script that prints html.

#!/usr/bin/python                   #on windows change to your path to the python exe

print "Content-Type: text/html"     # HTML is following
print                               # blank line, end of headers
print "<TITLE>CGI script output</TITLE>"
print "<H1>This is my first CGI script</H1>"
print "Hello, world!"

When you try this the first time, the hardest part is usually figuring out where to put the script and how to make the web server recognize and run it. If you are using an apache web sever, take a look at these configuration steps.

Once you have this simple script working, you will just need to add an html form and button tag and use the action property to point it to scripts you want to run.

Solution 2

This simple approach requires nothing except Python standard library. Create this directory structure:

.
|-- cgi-bin
|   `-- script.py
|-- index.html
`-- server.py

You put your scripts in "cgi-bin" directory, "index.html" contains links to scripts in "cgi-bin" directory (so you don't have to type them manually, although you could), and "server.py" contains this:

import CGIHTTPServer
CGIHTTPServer.test()

To run your server, just run "server.py". That's it, no Apache, no other dependencies.

HTH...

Solution 3

All you need is to have it in a directory that is set to execute CGI scripts, then you need to add

#!/usr/bin/env python

to the top, and it should execute the script as a normal python script if you link to it. (like you would a HTML or PHP file)

This, of course, assumes your webserver is set up to execute CGI at all, and that you have a linux system.

There are other, alternative solutions if you want something a bit more fancy, like mod_python and web frameworks like Pylons, but I think the CGI solution would suit your use case better.

[EDIT]

On Windows if you have Apache, you SHOULD be able to use the same method, but replacing the stuff behind the #! with the path to your python.exe, like so:

#!C:\Python25\python.exe -u

The -u is apparently needed to put Python into unbuffered mode according to this link, which also has more indepth info: http://www.imladris.com/Scripts/PythonForWindows.html

Solution 4

You should look into CherryPy as your HTTP request handler which can be configured to run on top of a very light NGINX web server (or Apache if you prefer).

You can then either import the other scripts into the service you have running on CherryPy. This would give you a little bit more control for monitoring the process and catching errors if they occur.

The other option is to simply tell the operating system to trigger the command using os.system(). Check here for an example of different approaches to using os.system().

Solution 5

A simple cgi script (or set of scripts) is all you need to get started. The other answers have covered how to do this so I won't repeat it; instead, I will stress that using plain text will get you a long way. Just output the header (print("Content-type: text/plain\n") plus print adds its own newline to give you the needed blank line) and then run your normal program.

This way, any normal output from your script gets sent to the browser and you don't have to worry about HTML, escaping, frameworks, anything. "Do the simplest thing that could possibly work."

This is especially appropriate for non-interactive private administrative tasks like you describe, and lets you use identical programs from a shell with a minimum of fuss. Your driver, the page with the buttons, can be a static HTML file with single-button forms. Or even a list of links.

To advance from there, look at the logging module (for example, sending INFO messages to the browser but not the command line, or easily categorizing messages by using different loggers, by configuring your handlers), and then start to consider template engines and frameworks.

Don't output your own HTML and skip to using one of the many existing libraries—it'll save a ton of headache even spending a bit of extra time to learn the library. Or at the very least encapsulate your output by effectively writing your own mini-engine.

Share:
38,762

Related videos on Youtube

Dustin Wyatt
Author by

Dustin Wyatt

Updated on July 22, 2020

Comments

  • Dustin Wyatt
    Dustin Wyatt almost 4 years

    I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities.

    I'd like to create a web page served from one of my systems that would merely present a few buttons which would allow me to initiate these scripts remotely.

    The problem is that I don't know where to start investigating how to do this. Let's say I have a script called:

    file_arranger.py

    What do I need to do to have a webpage execute that script? This isn't meant for public consumption, so anything lightweight would be great. For bonus points, what do I need to look into to provide the web user with the output from such scripts?

    edit: The first answer made me realize I forgot to include that this is a Win2k3 system.

    • Dustin Wyatt
      Dustin Wyatt over 6 years
      I was just looking through my profile here on SO and I noticed this question I asked nearly a decade ago. For any other newbies who come along and need some encouragement: I'm now a software developer with a wide range of knowledge and skills across all sorts of areas of software development. I could easily implement the system I asked about in this question and tack on all sorts of cool and useful functionality. Keep learning! It can be done!
  • krizajb
    krizajb over 10 years
    What test.py? You only mentioned script.py, and server.py. Could you be more specific or is this already obsolete?
  • hello_there_andy
    hello_there_andy over 7 years
    Hi @JaseAnderson, I've read some of the page you linked, but it seems amazingly complex compared to say: stackoverflow.com/questions/25336688/…, where you'd just need to add some ajax statement in the hmtl file to run a python script. Am I missing something?