0
如何创建单独的函数(在controller.py中的索引函数内),这样我就可以创建两个独立的绘图,并具有独立加载的两个独立文件?Flask中的两个函数独立绘制数据
换句话说,我希望用户采取以下步骤:
- 上传的培训文件
- 单击“绘图”按钮,绘制
- 现在上传的测试文件
- 点击'Plot'按钮进行绘图。
这两个图都应该出现在同一个HTML模板上。这两个文件将使用相同的绘图功能(或现在)。理想情况下,我想使用不同的功能。
这里是输入HTML的样子:
绘制函数如下:
def compute_mean_std(filename=None):
# data = np.loadtxt(os.path.join('uploads', filename))
df = pd.read_csv(os.path.join('uploads', filename))
sns.set_style('darkgrid')
fig, ax = plt.subplots(3, sharex=True)
ax[0].plot(df.index, df.x, color="red")
ax[1].plot(df.index, df.y, color="blue")
ax[2].plot(df.index, df.z, color="teal")
ax[0].legend(numpoints=1, loc=1)
ax[1].legend(loc=1)
ax[2].legend(loc=1)
# Check static folder:
if not os.path.isdir('static'):
os.mkdir('static')
else:
for plotfilename in glob.glob(os.path.join('static', '*.png')):
os.remove(plotfilename)
plotfile = os.path.join('static', str(time.time()) + '.png')
plt.savefig(plotfile)
return plotfile
当控制器具有下面的代码:
from compute import compute_mean_std as compute_function
@app.route('/', methods=['GET', 'POST'])
def index():
form = Average(request.form)
filename = None # default
if request.method == 'POST':
# Save uploaded file on server if it exists and is valid
if request.files:
file = request.files[form.filename.name]
if file and allowed_file(file.filename):
# Make a valid version of filename for any file ystem
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'],
filename))
result = compute_function(filename)
else:
result = None
return render_template("view.html", form=form, result=result)
现在都绘制相同的情节。
它看起来像JavaScript/Ajax – furas
的工作也许他们绘制相同的,因为你画samr形式的两倍,但同时应该有两个不同的字段names.Show HTML。 – furas
@furas是对的。当ajax调用或POST响应时,你必须区分'训练'和'测试' –