微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我面临TypeError:视图函数未返回有效响应该函数返回None或不返回return语句而结束

如何解决我面临TypeError:视图函数未返回有效响应该函数返回None或不返回return语句而结束

嗨,我正在尝试使用烧瓶创建水平条形图,并遇到以下错误。 TypeError:视图函数未返回有效响应。该函数返回None或不返回return语句而结束。请帮忙。预先感谢。

app.py 从flask导入Flask,重定向,url_for,render_template,请求,flash,会话 将熊猫作为pd导入 app = Flask(名称) app.debug = True

@app.route('/',methods=['GET','POST'])
def index():
            return render_template('index.html')

@app.route('/data','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",'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}},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的请求也不会返回

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。