【发布时间】:2018-04-10 11:18:18
【问题描述】:
我正在尝试创建一个有 2 个按钮的 GUI。开始按钮将触发我的 python 脚本并显示移动图。当我按下停止按钮时,图形应该退出并且 python 应该停止。
当我点击开始按钮时,我得到了想要的结果。但它进入无限循环并且停止按钮不会退出代码。单击停止按钮不会将代码带到装饰器并且我的函数 stop() 没有执行。
下面是sn-p:
from flask import Flask, request, flash, url_for, redirect, render_template, session
from flask import render_template
import numpy as np
import matplotlib.pyplot as plt
import io
import threading
import base64
from IPython import get_ipython
from sys import exit
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/plot' , methods = ['GET', 'POST'])
def build_plot():
img = io.BytesIO()
global running
x = np.linspace(-180, 180,1000)
y = np.sin(x)
global j
fig, ax = plt.subplots()
plt.plot(x[0:200], y[0:200])
#plt.plot(x,y)
for i in range(200, len(y)):
plt.scatter(x[i], y[i])
plt.pause(0.0005)
return(get_ipython().run_line_magic('matplotlib', 'inline'))
@app.route('/stop' , methods = ['GET', 'POST'])
def stop():
#stop_ind='N'
#exit()
return render_template('home.html')
if __name__ == '__main__':
app.debug = True
app.run()
我想退出的函数没有被调用。
下面是我的 HTML:
<!DOCTYPE html>
<html>
<title>HOME PAGE..</title>
<body>
<h1>Home Page..!!</h1>
<form action="/plot" method="POST">
<input type="number" name="Number" ><br>
<br>
<input type="submit" value="Start" >
</form>
<form action="/stop" method="POST">
<br>
<input type="submit" value="Stop" >
</form>
</body>
</html>
</form>
</body>
</html>
【问题讨论】:
标签: python-3.x flask