简体   繁体   中英

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

I currently have a python script that updates certain CSV files when ran (it web scrapes and updates the information of a CSV file). In my HTML page (index.html), I have a script tag inside index.html that reads the CSV file and displays it as a table on the webpage. However, what I now need to do is update the CSV file by pressing an HTML button on the webpage. This will update the CSV file so when I run the button to run the JS script, it will have updated values from the file.

I searched and found it very hard to understand what they meant by using flask and Django (I don't know anything about setting up servers). I don't want to set up a Django webpage because I want to work with my current pure HTML webpage I wrote.

I would appreciate it if the answer is up to date with the current standard solutions for running python scripts in HTML.

Please ask if you need more information. Thanks.

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, ie 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

You can't run python in html. You can just use javascript. but you can pass the information as json and then use javascript if you don't want to change the language or using python frameworks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM