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

时间:2017-12-16 17:53:55

标签: python web-applications

我有一个Python脚本,它循环读取从Excel文件读取的SQL查询列表,执行它们,并将输出结果存储到.csv文件。

目前我从本地命令行触发此操作。我想创建一个非常基本的Web页面,基本上有一个按钮可以点击执行Python脚本。如果我可以以某种方式将输出文件存储在那里,甚至更好。

我的想法是从本地开始,然后将网页移动到我的团队可以访问它的地方并执行相同的操作。

对于这个网页我显然不需要太多功能,但这些东西对我来说是新的,所以不太清楚从哪里开始。有什么想法吗?

由于

1 个答案:

答案 0 :(得分:2)

我猜Flask对于简单的网络应用来说是一个不错的选择。

文件夹结构:

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

app.py(编辑添加了index.html路由处理,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()

您需要在工作目录中导出FLASK_APP = app.py

的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>

我能想到的最简单的例子 现在,当您运行app.py并导航到localhost:5000时(如果我没记错,端口默认为5000),您将看到带有execute SQL按钮的HTML页面。单击它将向localhost:5000/execute发送GET,然后def decode(n): A = [0]*36 #initialize array of 36 zeros for i in range(0,12): #for(int i = 0; i < 12; i++) in C# r = n % 3 n = n // 3 #integer division, just use / in C# if n is an int A[3*i+r] = 1 return A #test: n = 1*3**0 + 2*3**1 + 1*3**2 + 2*3**3 + 1*3**5 A = decode(n) print(A) 将调用您的函数 不幸的是,在服务器上部署超出了这个答案的范围。