简体   繁体   中英

Creating a basic web page to trigger a Python script

I have a Python script that loops through a list of SQL queries read from an Excel file, executes them, and stores output results to a .csv file.

Currently I trigger this from the command line locally. I would like to create a very basic Web page, something that basically has a button to click to execute the Python script for now. If I can get the output file stored there somehow as well, even better.

The idea being I would start with it locally, but then move the web page somewhere where my team could access it and do the same thing.

I don't need much functionality at all for this web page obviously but this stuff is new to me so not quite sure where to start. Any ideas?

Thanks

I guess Flask would be a decent choice for a simple web app.

folder structure:

├── app.py
├── static
│   ├── index.html

app.py ( EDIT added index.html route handling, duh doy)

from flask import Flask
app = Flask(__name__, static_url_path='', template_folder='static')

@app.route('/')
def index():
    return app.send_static('index.html')

@app.route('/execute')
def execute_sql_logic():
    # Do stuff here
    return 'SQL executed', 200

if __name__ == '__main__':
    app.run()

You'll need to export FLASK_APP=app.py in your working directory

index.html

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <a href='/execute'><button>execute SQL</button></a> </body> </html> 

Simplest example I could think of.
Now when you'll run app.py and navigate to localhost:5000 (Port defaults to 5000 if I remember correctly) you'll see the HTML page with the execute SQL button. Clicking on it will send a GET to localhost:5000/execute which in turn will call your function.
Deploying on a server is beyond the scope of this answer unfortunately.

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