Hi there, I’m looking for a way to update my datatable output every 5 seconds, the site gets updated but the output doesn´t change… I’m doing something bad in my callback ?
import dash
import dash_core_components as dcc
import dash_html_components as html
from random import random
import plotly
import plotly.express as px
import pandas as pd
import numpy as np
from datetime import datetime
from dash.exceptions import PreventUpdate
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
import pyodbc
import dash_table
global data
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.MINTY, 'https://use.fontawesome.com/releases/v5.9.0/css/all.css'])
server = '10.****'
database = '****'
username = '***'
password = '***'
conn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
def connectSQLServer(conn):
connSQLServer = conn
return connSQLServer
sql_query = ('''
#query
'''
)
df = pd.read_sql(sql_query,conn)
dff = df.apply(lambda x: x.str.strip()
if x.dtype == "object" else x) # Trim whitespaces
count_row = dff.shape[0]
table = dash_table.DataTable(id='memory-table',
columns=[{"name": i, "id": i} for i in dff.columns],
data = dff.to_dict('records')
)
badge = dbc.Button(
["Total:", dbc.Badge(count_row, color="secondary", className="ml-1")],
color="primary",style={'position':'absolute'}
)
navbar = dbc.Navbar(
[
html.A(
# Use row and col to control vertical alignment of logo / brand
dbc.Row(
[
dbc.Col(html.Img(src=app.get_asset_url('logo.png'), height="65px")),
dbc.Col(dbc.NavbarBrand("Op***", className="ml-4", style={
'position': 'relative',
'left': '50%',
'transform': 'translateX(-50%)',
}
)
),
],
align="center",
no_gutters=True,
),
href="#",
),
dbc.NavbarToggler(id="navbar-toggler"),
],
color="primary",
dark=True,
)
footer = dbc.Container(
dbc.Row(
dbc.Col(
html.P(
[
html.Span('C****', className='mr-2'),
html.A(html.I(className='fas fa-envelope-square mr-1'), href='mailto:<it.***>@<***>.com'),
],
className='lead'
)
)
)
)
app.layout = html.Div([dcc.Store(id='memory-output'),
navbar,
table,
dcc.Interval(id='interval_component',
interval=5000,
n_intervals=0
),
badge,
html.Hr(),
footer,
],
id='tableDiv',
className= 'tableDiv',)
@app.callback(Output('memory-output', 'data'), [Input('interval_component', 'n_intervals')])
def update_table(n_intervals):
dataSQL = [] # set an empty list
global data
sql_conn = connectSQLServer(conn)
cursor = sql_conn.cursor()
cursor.execute(sql_query)
rows = cursor.fetchall()
for row in rows:
dataSQL.append(list(row))
labels = ['Tipo id', 'id', 'Nombre', 'Fecha Ingreso', 'Hora Ingreso', 'Fecha Salida', 'Descripcion', 'Dx Ingreso', 'Op. Minutos']
data = pd.DataFrame.from_records(dataSQL, columns=labels)
data = dff.to_dict('records')
return data
if __name__ == '__main__':
app.run_server(debug=True, host='0.0.0.0', port = 8050)
app.config.suppress_callback_exceptions = True # see https://dash.plot.ly/urls
app.title = 'Ta**' # appears in browser title bar