为我的 python 脚本创建一个 WEB UI
Creating a WEB UI for my python scripts
所以,我有一个网站,我想在其中实时显示我的机器学习程序的输出。每个输出都基于用户的输入。
我已经成功地创建了我的 python 脚本,它给了我想要的输出,但直到现在我只是 运行 在我的本地机器上运行我的脚本。
我正在使用 matplotlib、numpy、sci-kit 和与它们类似的包,我需要创建一个网络 UI,它可以 运行 来自 [=] 的 python 脚本49=] 按钮点击然后显示生成的 matplotlib 图。
我一直在寻找答案,这是我的发现
现在我对所有这些技巧都有些了解了。但我找不到一些完美的例子来帮助我解决我的情况。我想让你知道的几件事是,我有一个准备好的 HTML 文件,还有 CSS。我正在为我的远程服务器使用 Google 控制台 VM 实例。
所以,总而言之,我只需要解决以下问题。
- 用户从我的 html 页面中选择了一个选项。
- 远程服务器收到请求,启动必要的python脚本
- Python脚本returnsmatplotlib形式的数据,文本和数字。
- 我的 HTML 页面获取数据,解析它们并显示输出。
- 你好,所以你需要一些 python 网络框架,因为你已经
提到的烧瓶可能是个不错的选择。
- 要将您的应用程序部署到生产环境中,您还需要一个网络
服务器 运行 在您的远程服务器上。
- 之后,您可以设置您的网络服务器以与您的
烧瓶 运行 申请。
您的烧瓶端点可能如下所示:
@app.route('/evaluate', methods = ['GET', 'POST'])
def index():
if request.method == 'POST':
# Computed ML output
output = compute_data(data)
# Format to graph data
graph_data = transform_to_graph(output)
# Pass data to the template
return render_template('graph-view.html', graph_data=graph_data)
return render_template('main.html')
其中 main.html 将是带有按钮和用户输入的页面:
<!DOCTYPE html>
<html>
<head>
<title>User Input</title>
</head>
<body>
<h1>Provide with input</h1>
<form method="post" action="/evaluate">
<input type="text" name="data">
<input type="submit" value="Evaluate">
</form>
</body>
</html>
对于 Flask 基础知识,这可能是非常简单明了的教程:
https://www.codementor.io/overiq/basics-of-flask-fzvh8ueed。
搜索其他来源,您将很快进入。对于部署,您还需要应用程序服务器 运行 您的 Flask 应用程序,因为 Flask 本身还没有准备好生产。我可以推荐 Gunicorn,这是显示使用 Nginx 代理 Web 服务器设置 Gunicorn 的众多教程之一:https://tutorials.technology/tutorials/71-How-to-setup-Flask-with-gunicorn-and-nginx-with-examples.html。请搜索此主题,那里有很多教程。
所以,我有一个网站,我想在其中实时显示我的机器学习程序的输出。每个输出都基于用户的输入。
我已经成功地创建了我的 python 脚本,它给了我想要的输出,但直到现在我只是 运行 在我的本地机器上运行我的脚本。
我正在使用 matplotlib、numpy、sci-kit 和与它们类似的包,我需要创建一个网络 UI,它可以 运行 来自 [=] 的 python 脚本49=] 按钮点击然后显示生成的 matplotlib 图。
我一直在寻找答案,这是我的发现
现在我对所有这些技巧都有些了解了。但我找不到一些完美的例子来帮助我解决我的情况。我想让你知道的几件事是,我有一个准备好的 HTML 文件,还有 CSS。我正在为我的远程服务器使用 Google 控制台 VM 实例。
所以,总而言之,我只需要解决以下问题。
- 用户从我的 html 页面中选择了一个选项。
- 远程服务器收到请求,启动必要的python脚本
- Python脚本returnsmatplotlib形式的数据,文本和数字。
- 我的 HTML 页面获取数据,解析它们并显示输出。
- 你好,所以你需要一些 python 网络框架,因为你已经 提到的烧瓶可能是个不错的选择。
- 要将您的应用程序部署到生产环境中,您还需要一个网络 服务器 运行 在您的远程服务器上。
- 之后,您可以设置您的网络服务器以与您的 烧瓶 运行 申请。
您的烧瓶端点可能如下所示:
@app.route('/evaluate', methods = ['GET', 'POST'])
def index():
if request.method == 'POST':
# Computed ML output
output = compute_data(data)
# Format to graph data
graph_data = transform_to_graph(output)
# Pass data to the template
return render_template('graph-view.html', graph_data=graph_data)
return render_template('main.html')
其中 main.html 将是带有按钮和用户输入的页面:
<!DOCTYPE html>
<html>
<head>
<title>User Input</title>
</head>
<body>
<h1>Provide with input</h1>
<form method="post" action="/evaluate">
<input type="text" name="data">
<input type="submit" value="Evaluate">
</form>
</body>
</html>
对于 Flask 基础知识,这可能是非常简单明了的教程: https://www.codementor.io/overiq/basics-of-flask-fzvh8ueed。 搜索其他来源,您将很快进入。对于部署,您还需要应用程序服务器 运行 您的 Flask 应用程序,因为 Flask 本身还没有准备好生产。我可以推荐 Gunicorn,这是显示使用 Nginx 代理 Web 服务器设置 Gunicorn 的众多教程之一:https://tutorials.technology/tutorials/71-How-to-setup-Flask-with-gunicorn-and-nginx-with-examples.html。请搜索此主题,那里有很多教程。