How to Run a Python Script in a Web Page

How to run a Python script in a web page

In order for your code to show, you need several things:

Firstly, there needs to be a server that handles HTTP requests. At the moment you are just opening a file with Firefox on your local hard drive. A server like Apache or something similar is required.

Secondly, presuming that you now have a server that serves the files, you will also need something that interprets the code as Python code for the server. For Python users the go to solution is nowadays mod_wsgi. But for simpler cases you could stick with CGI (more info here), but if you want to produce web pages easily, you should go with a existing Python web framework like Django.

Setting this up can be quite the hassle, so be prepared.

How can I run a Python script in HTML?

It probably would depend on what you want to do. I personally use CGI and it might be simpler if your inputs from the web page are simple, and it takes less time to learn. Here are some resources for it:

  • cgi — Common Gateway Interface support
  • Python - CGI Programming

However, you may still have to do some configuring to allow it to run the program instead of displaying it.

Here's a tutorial on that: Apache Tutorial: Dynamic Content with CGI

How to run a python script from clicking a button on a HTML webpage?

You need to setup a web server. Flask can be used for that purpose. So you can create the following script:

from datetime import datetime
from flask import Flask, redirect, request
app = Flask(__name__)

@app.route('/')
def index():
with open('index.html') as fh:
return fh.read()

@app.route('/update')
def update_file():
with open('data.txt', 'w') as fh:
fh.write(datetime.now().strftime('%H:%M:%S'))
return redirect(request.referrer)

The @app.route('/') provides your landing page which is stored in index.html:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example</title>
</head>

<body>
<form action="/update">
<input type="submit">
</form>
</body>
</html>

This HTML page contains a submit button which refers to /update. In the web server script above this route is registered with the update_file function. Here you can implement your logic to react to the button press (in the example it writes the current time to data.txt on the server). Since the script operates on the server and your browser operates on the client this only works if that is actually the same machine, i.e. if your using it locally as an interface to your programs. If you want to separate server and client you'll need to introduce a way for transferring the data.

Now you can run the web server as follows:

$ FLASK_APP=script.py flask run
[...]
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Now you can navigate to the address indicates above and you'll be on the landing page. Pressing the button updates the file:

$ tree .
.
├── data.txt <-- This file gets updated.
├── index.html
├── __pycache__
│   └── script.cpython-38.pyc
└── script.py

Run a Python Script from the Web

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



Related Topics



Leave a reply



Submit