我面临类型错误:视图函数未返回有效响应。该函数要么返回 None ,要么在没有 return 语句的情况下结束



嗨,我正试图使用flask创建一个水平条形图,但遇到以下错误。TypeError:视图函数未返回有效的响应。函数要么返回None,要么结束时没有返回语句。请帮忙。提前谢谢。

app.py从烧瓶导入烧瓶,重定向,url_fo,render_template,请求,flash,会话进口熊猫作为pdapp=烧瓶(名称(app.debug=真

@app.route('/', methods=['GET','POST'])
def index():
return render_template('index.html')
@app.route('/data', methods=['GET','POST'])
def dropdown():
if request.method == 'POST':
file = request.form['upload-file']
data = pd.read_excel(file)
data = pd.DataFrame(data)
colours=data['Question'].unique().tolist()
return render_template('test.html', colours=colours)
@app.route("/data/submitted", methods=['GET', 'POST'])
def charts():
#select = request.form.get('colour')
if request.method == 'POST':
to_filter = request.form.get['colours']
# filter the data here
plot_data = data[data['Themes'].str.contains(to_filter)]
plot_data['flag'] = 1
plot_data2 = plot_data.groupby(['Themes'])['flag'].sum().reset_index()
#colours1 = data1['Question'].unique().tolist()
labels = plot_data2['Themes'].tolist()
values = plot_data2['flag'].tolist()
bar_labels = labels
bar_values = values
return render_template("sample_chart1.html", labels=bar_labels,data=bar_values)
if __name__ == "__main__":
app.run(debug=True)

sample_chart1.html

<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<head>
<meta charset="utf-8" />
<title>Charts.js</title>
<!-- import plugin script -->
</head>
<h1>Chart</h1>
<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
<!-- bar chart canvas element -->
<canvas id="chart" width="300" height="200"></canvas>
<p id="caption">Distribution of Topics.</p>
<script>
// create the chart using the chart canvas
var chartData = {
labels : [{% for item in labels %}
"{{item}}",
{% endfor %}],
datasets : [{
data : [{% for item in values %}
{{item}},
{% endfor %}],
spanGaps: false
}]
}
// get chart canvas
var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, {
type: 'horizontal bar',
data: chartData,
});
</script>

它正在发出一个GET请求,该请求不传递if request.method == 'POST'由于它没有通过,所以对于不是POST 的请求没有返回

最新更新