I am really new to python and dashboard. I am trying to update table from @Callback. So the table will be refresfed every 40 seconds. The data of the table are coming from a MySQL table. I am trying to return the whole table but I get the error “Invalid argument columns[0].name
passed into DataTable”. I hope someone could help me. I will greatly appreciate that. My code is below:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import mysql.connector
from dash import Dash, dcc, html, Input, Output, dash_table, no_update
import pandas as pd
import pandas as pd
app = dash.Dash(__name__)
app.layout = html.Div([
html.Div(id="table1"),
dcc.Interval(
id='interval-component',
interval=40,
n_intervals=0) ,
])
@app.callback(
Output('table1','children'),
Input('interval-component', 'n_intervals'),
)
def update_output_div(input_value):
mydb = mysql.connector.connect(
host="localhost",user="user",password="password",database="database" )
mycursor = mydb.cursor()
mycursor.execute("SELECT X1 , X2, X3 FROM Table")
Db = mycursor.fetchall()
#Reproduce the table
L0=list(Db[0])
L0.insert(0,"Y1")
L0=tuple(L0)
L1=list(Db[1])
L1.insert(0,"Y2")
L1=tuple(L1)
L2=list(Db[2])
L2.insert(0,"Y3")
L2=tuple(L2)
Data= [L0,L1,L2]
df = pd.DataFrame(Data)
data=df.to_dict('records')
columns=[{"name": i, "id": i} for i in df.columns]
return dash_table.DataTable(data=data, columns=columns)
if __name__ == '__main__':
app.run_server(debug=True,port=8040)