Я пытаюсь вернуть несколько JSON объектов-графиков в запрос AJAX в моем приложении Flask, но получаю сообщение об ошибке
TypeError: The view function did not return a valid response tuple. The tuple must have the form (body, status, headers), (body, status), or (body, headers).
I ' я не уверен, как преобразовать графически JSON объекты в кортежи для отправки обратно на запрос AJAX. Это мой Python код:
def create_bar_plot(query, ycol, title):
df = pd.DataFrame(query, columns=[ycol,'count'])
data = [
go.Bar(
x=df['count'],
y=df[ycol],
orientation='h'
)
]
layout=go.Layout(title=title, xaxis={'title':'count'}, yaxis={'type':'category'})
figure=go.Figure(data=data,layout=layout)
graphJSON = json.dumps(figure, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON
@app.route('/change_visualization', methods=['GET', 'POST'])
@login_required
def change_visualization():
"""
Change year
"""
year = request.args['selected']
data = []
first_query = db.engine.execute("""SELECT machine, COUNT(*) as `count` FROM table1 WHERE id LIKE %s GROUP BY machine""", (year + '%',))
first_result = create_bar_plot(run_info_query, "machine", "First Result")
data.append(first_result)
second_query = db.engine.execute("""SELECT Type, COUNT(*) as 'count' FROM table2 WHERE id LIKE %s GROUP BY Type;""", (year + '%',))
second_result = create_bar_plot(project_type_query, "Type", "Second Result")
data.append(second_result)
third_query = db.engine.execute("""SELECT Type, COUNT(*) as 'count' FROM table3 WHERE id LIKE %s GROUP BY Type;""", (year + '%',))
third_result = create_bar_plot(sample_type_query, "Type", "Third Result")
data.append(third_result)
return tuple(data)
И AJAX:
<script>
$('#year').on('change',function(){
$.ajax({
url: "/change_visualization",
type: "GET",
contentType: 'application/json;charset=UTF-8',
data: {
'selected': document.getElementById('year').value
},
dataType:"json",
success: function (data) {
Plotly.newPlot('bargraph1', data.first_result,{});
Plotly.newPlot('bargraph2', data.second_result,{});
Plotly.newPlot('bargraph3', data.third_result,{});
}
});
})
</script>
Любая помощь будет оценена