简体   繁体   English

创建一个基本网页以触发Python脚本

[英]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. 我有一个Python脚本,该脚本遍历从Excel文件读取的SQL查询列表,执行它们,并将输出结果存储到.csv文件。

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. 我想创建一个非常基本的Web页面,该页面基本上具有一个按钮,可以单击以立即执行Python脚本。 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. 我想Flask对于简单的Web应用程序来说将是一个不错的选择。

folder structure: 文件夹结构:

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

app.py ( EDIT added index.html route handling, duh doy) app.py( 编辑添加了index.html路由处理,不是吗)

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 您需要在工作目录中导出FLASK_APP = app.py

index.html 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. 现在,当您运行app.py并导航到localhost:5000 (如果我没有记错的话,端口默认为5000),您将看到带有execute SQL按钮的HTML页面。 Clicking on it will send a GET to localhost:5000/execute which in turn will call your function. 单击它会发送一个GET到localhost:5000/execute ,这将依次调用您的函数。
Deploying on a server is beyond the scope of this answer unfortunately. 不幸的是,在服务器上进行部署超出了此答案的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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