I am trying to build Live Sentiment analysis and update the graph using local DB i.e., Sqlite3 and I have put up a time interval of 10000 in milliseconds. The idea here is to update the graph as soon as the Sqlite3 database is updated, get reflected in the graph without refreshing the page. The URL shows “Updating” but the graph is not.
The code is as follows. Any help would be appreciated.
Local DB name: example.db
Table name: sample (which has only one column “sentence”)
import time
import dash
from dash.dependencies import Output,Input
import dash_html_components as html
import dash_core_components as dcc
import plotly
import random
import plotly.graph_objs as go
from collections import deque
import pandas as pd
import sqlite3
from textblob import TextBlob
conn = sqlite3.connect(‘C:/Users/mowlanicab/Desktop/Voicea/Sentiment Analysis/Live Sentiment Analysis/Live/example.db’)
c = conn.cursor()
df=pd.read_sql_query(“SELECT * FROM sample;”,conn)
#df = pd.read_csv(file, header=None)
df[‘sentiment’] = df[‘sentence’].apply(lambda x: TextBlob(x).sentiment.polarity)
#df[‘smoothed’] = df[‘sentiment’].rolling(int(len(df)/5)).mean()
df.dropna(inplace=True)
#print(df)
df[‘ID’] = df.index + 0 # Adding a serial number column
X = df.ID.values
#Y = df.smoothed.values
Y = df.sentiment.values
app = dash.Dash(name)
app.layout = html.Div([html.H2(‘Live Sentiment analysis’),
dcc.Graph(id=‘live-graph’,
figure={‘data’: [{‘x’ : X, ‘y’ : Y}]}, animate=True),
dcc.Interval(id=“interval”, interval=1*1000000), html.P(id=“output”)])
@app.callback(dash.dependencies.Output(‘live-graph’, ‘figure’),[Input(“interval”, “n_intervals”)])
def update_recent_tweets(input_data):
conn = sqlite3.connect(‘example.db’)
c = conn.cursor()
df = pd.read_sql(“SELECT * FROM sample”, conn)
df[‘sentiment’] = df[‘sentence’].apply(lambda x: TextBlob(x).sentiment.polarity)
df.dropna(inplace=True)
df[‘ID’] = df.index + 0
print(df)
X = df.ID.values
Y = df.sentiment.values
app.layout = html.Div([html.H2(‘Live Sentiment analysis’),
dcc.Graph(id=‘live-graph’,
figure={‘data’: [{‘x’ : X, ‘y’ : Y}]}, animate=True),
dcc.Interval(id=“interval”, interval=1*1000000), html.P(id=“output”)])
return app.layout
if name == ‘main’:
app.run_server(debug=True)