我正在创建一个 Web 应用程序,允许用户输入绘图的坐标,然后查看生成的绘图。
我有一个函数可以根据用户输入创建此图,然后将其保存到我的“静态”文件夹中,然后在点击提交后将其发布到下一个 html 上。
这在本地工作得很好,但是当我部署我的应用程序时返回内部服务器错误。似乎这是写入静态文件夹然后在应用程序运行时从中提取的问题。
Flask 中的绘图功能
@app.route("/plot", methods = ['GET', 'POST'])
def plot():
form_data = request.form
xcord = request.form.get('xcord')
ycord = request.form.get('ycord')
xcord = int(xcord)
ycord = int(ycord)
goal = Goal(xcord, ycord)
percent = goal.is_goal()
fig = goal.shot_chart()
fig.savefig('static/myplot.png', pad_inches=0, dpi=300)
return render_template("plot.html", title = 'plot', fig = fig, form_data = form_data, xcord=xcord, ycord=ycord, percent=percent)
Plot.html
<h1>Shot Predictor</h1>
<h3>Enter the x and y coordinates of your shot and see the % chance of a goal!</h3>
<body>
<p>
<img src= "static/myplot.png" class = "soccer" height="504" width="864">
<table class="center">
<tr>
<td>Percent chance of a goal: {{ percent }}</td>
</tr>
</table>
</p>
</body>
我尝试更改文件路径,也咨询了其他线程,但无济于事。我是网络开发的业余爱好者。
将
DEBUG
设置为True
,以便您可以看到错误,这可能与创建新文件的服务器权限有关,您还可以检查是否在静态文件夹中生成了绘图。
我建议您尝试在
fig.savefig()
中提供完整路径,例如 fig.savefig('/path/to/your/project/static/myplot.png', pad_inches=0, dpi=300)
。