How to display a bar graph using data from pandas sql

I know the answer may be obvious, but I’m really really new to Dash, to Pandas and to Creating DashBoard in general.
I am trying to work on a project where I can monitor sentiments from Twitter based on keywords streamed and saved to a an SQL database. For one of the columns, I need to display data in bar graph format. Even with the graph id in place, there is no update or sign that anything from the callback is been seen.

import dash
from dash.dependencies import Output, Event
import dash_core_components as dcc
import dash_html_components as html
import plotly
import random
import plotly.graph_objs as go
from collections import deque
import sqlite3
import pandas as pd

app = dash.Dash(__name__)

app.layout = html.Div( children = [ 
html.Div([
            html.H5('ANNx'),
            dcc.Graph( id='cx1' )
        ])
])

@app.callback(Output('cx1', 'figure'))
def update_bar( ):
    try:
        conn = sqlite3.connect('twitterx.db')
        c = conn.cursor()

        atiku = pd.read_sql("SELECT * FROM sentiment WHERE sentiment > 0.5 AND tweet LIKE '%atiku%' AND tweet NOT LIKE '%RT%' ORDER BY unix DESC LIMIT 1000", conn)

        buhari = pd.read_sql("SELECT * FROM sentiment WHERE sentiment > 0.5 AND tweet LIKE '%buhari%' AND tweet NOT LIKE '%RT%' ORDER BY unix DESC LIMIT 1000", conn)

        X = (len(atiku))
        Y = (len(buhari))

        datax = [ 
            go.bar (
                x=['ATIKU'],
                y= Y,
                type='bar',
                name= 'Atiku'
            ),

            go.bar (
                x =['BUHARI'],
                y = X,
                type = 'bar',
                name = 'Buhari'
            )
        ]

        return {'data': [datax],'layout' : go.Layout(xaxis=dict(range=[min(X),max(X)]),
                                                    yaxis=dict(range=[min(Y),max(Y)]),)}
    except Exception as e:         
        with open('errors.txt','a') as f:
            f.write(str(e))
            f.write('\n')




if __name__ == '__main__':
    app.run_server(debug=True)
1 Like