I’m attempting to create a live-update dashboard that connects to a Postgres DB on AWS. I’ve made a function that connects to the database, I initiated a connection and mapped the database. In the callback I’ve called the function. On initial load up, I get the data and it works well, however, when I update the database, the callback doesn’t update the new data. I’m not sure where my error in reasoning is, if anyone could give me a pointer. I’ve made an abbreviated example of my code below. Thanks
import plotly.graph_objs as go
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from plotly.subplots import make_subplots
from sqlalchemy import create_engine, MetaData, Table
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Integer, String, func, Date, ARRAY
from sqlalchemy.orm import sessionmaker
app = dash.Dash(__name__, external_stylesheets=[BS], suppress_callback_exceptions=True, update_title=None)
server=app.server
class DbConnection:
def __init__(self, **kwargs):
self.DB_URI = os.environ.get('DB_URI', kwargs.get('DB_URI'))
# self.DB_URI = aws_parameter['Parameter']['Value']
self.echo = kwargs.get('echo', True)
self.future = kwargs.get('future', True)
# Now create the engine
self.engine = create_engine(self.DB_URI, echo=self.echo, future=self.future)
# Make the session maker
self.session_maker = sessionmaker(bind=self.engine)
@property
def session(self):
"""Return a session as a property"""
return self.session_maker()
# instantiate the database connection and map your base
my_db_connection = DbConnection() # provide kwargs as needed
session = my_db_connection.session # necessary to assign property to a variable
# Map the classes
Base = automap_base()
Base.prepare(my_db_connection.engine, reflect=True)
User = Base.classes.users
Datex = Base.classes.data
@lru_cache()
def get_blood_pressure(session, user_id, date):
blood_pressure, time = [], [],
query = session.query(Datex) \
.filter(Datex.user_id == user_id) \
.filter(Datex.date_time == date)
for instance in query:
blood_pressure.append([instance.systolic, instance.mean, instance.diastolic])
time.append(instance.time)
return blood_pressure, time,
app.layout = html.Div([
dcc.Store(id='time', storage_type='session'),
dcc.Store(id='blood_pressure', storage_type='session'),
html.Div(dcc.Graph(id='live-graph', animate=False), className='w-100'),
html.Div(id= "testing"),
dcc.Interval(
id='graph-update-BP',
interval=30000,
n_intervals=0
)]), width={"size": 10, "offset": 0.5}),
@app.callback(
dash.dependencies.Output('live-graph', 'figure'),
dash.dependencies.Output('blood_pressure', 'data'),
dash.dependencies.Output('time', 'data'),
[dash.dependencies.Input('graph-update-BP', 'n_intervals')],
)
def update_graph_scatter_1(n):
trace = []
blood_pressure = []
time = []
blood_pressure, time = get_blood_pressure(session=session, user_id=3, date='Monday,Apr:26')
for i in range(0, len(blood_pressure)):
trace.append(go.Box(y=blood_pressure[i],
x=time[i],
line=dict(color='#6a92ff'),
hoverinfo='all'))
fig = make_subplots(rows=1, cols=1)
def append_trace():
for i in range(0, len(trace)):
fig.append_trace(trace[i], 1, 1)
append_trace()
return fig, blood_pressure, time,