flask app convert sql query to python plotly.

Abdellah ALAOUI ISMAILI my.alaoui.ia at gmail.com
Sat Apr 2 14:50:43 EDT 2022


i would like to convert in my flask app an SQL query to an plotly pie chart using pandas. this is my code :

def query_tickets_status() :
    query_result = pd.read_sql ("""
            SELECT COUNT(*)count_status, status 
            FROM tickets 
            GROUP BY status""", con = mydc_db)
    return query_result

labels_statut = query_tickets_status['status']
values_statut = query_tickets_status['count_status']

fig = go.Figure(data=[go.Pie(labels=labels_statut, values=values_statut)])
graphSuppliers = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)

return render_template('admin/dashboard.html', graphSuppliers = graphSuppliers)
this is my template file.

<html>
 <body>
  <h1>Your Plotly Chart</h1>
  <div id='chart' class='chart'></div>
</body>
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
<script type='text/javascript'>
  var graphs = {{graphSuppliers | safe}};
  Plotly.plot('chart',graphs,{});
</script></html>
but I get this error :

TypeError: 'function' object is not subscriptable


More information about the Python-list mailing list