Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a Python Script from the Web

I've been stumbling along with the same problem for almost a year now. I always find a way to work around it, but I'm tired of finding work arounds.

What I need is to create a button on a Web Page (preferable HTML, not PHP or ASP) that runs a python script on the server. I'd also like the ability to have this button send information from a form to the script.

I need to do this on a local host and through a web service hosted on the Amazon Cloud. I won't be able to install anything extra on the Amazon Cloud service, such as PHP or CGI.

I'd really like an easy solution, I'm an expert with python and I can write webpages that whistle, but I just can't find a simple solution to this problem.

My ideal solution would be something like the mail to tag:

<a href="mailto:[email protected]?Subject=Hello%20again">Send Mail</a>

Except:

<a href="myscript.py?Subject=1234">Run Script</a>

Now I highly doubt a solution like that exists, but well I can dream right.

The script I am trying to run:

  1. Returns a Unique ID from the user
  2. Sends the ID to a GIS program that creates a map based on the ID (the ID selects the area of the map)
  3. The map is then exported to a PNG, wrote into an HTML document and then displayed for the user in a new tab.

EDIT ---------------------------

Thanks to @Ketouem answer I was able to find a great solution to my issue. I'll post some of the code here so that others can benefit. Make sure you download the Bottle Module for python, its great.

# 01 - Import System Modules
from bottle import get, post, request, Bottle, run, template

# 02 - Script Variables
app = Bottle()

# 03 - Build Temporary Webpage
@app.route('/SLR')
def login_form():
    return '''<form method="POST" action="/SLR">
                Parcel Fabric ID: <input name="UID" type="text" /><br />
                Save Location: <input name="SaveLocation" type="text" value="D:/Python27/BottleTest/SLR_TestOutputs"/><br />
                Air Photo On: <input name="AirPhoto" type="checkbox"/><br />                
                Open on Completion: <input name="Open" type="checkbox"/><br />
                Scale: <input name="Scale" type="text" value="10000"/><br />
                <input type="submit" />
              </form>'''

# 04 - Return to GIS App
@app.route('/SLR', method='POST')
def PHPH_SLR_Script():
    # I won't bother adding the GIS Section of the code, but at this point it send the variables to a program that makes a map. This map then saves as an XML and opens up in a new tab.

 # 04 - Create and Run Page
run(app, host='localhost', port=8080)
like image 322
Cody Brown Avatar asked Feb 15 '13 16:02

Cody Brown


People also ask

Can you run Python scripts in a browser?

Brython is both a Python compiler and an interpreter written in JavaScript. As a result, you can compile and run Python code in the browser. A good example of this feature is demonstrated by the online editor available on the Brython website. With the online editor, Python is running in the browser.

Can you run a Python script from HTML?

PyScript is an open source web framework that allows you to create frontend web applications using Python. With PyScript, you can either embed Python code in HTML, or link to a Python file and the code will execute in the browser — without running Python in the backend.


1 Answers

You could use Bottle : http://bottlepy.org/docs/dev/index.html which is a light web framework

like image 80
Ketouem Avatar answered Oct 12 '22 16:10

Ketouem