2

Hi Guys i try to update the datatable data inside app that initialized from a display() function like this, but the data table is not updated, however if initialized the apps without a display() but write the div inside directly, the data table is updated, is there a way for me to update the datatable data using callback if i declare the div inside a function ?

import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_table as dt
from dash.dependencies import Input, Output
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')

app = dash.Dash(__name__)



states = df.State.unique().tolist()

app.layout = display()

@app.callback(
    Output('table-container', 'data'),
    [Input('filter_dropdown', 'value') ] )
def display_table(state):
    dff = df[df.State==state]
    return dff.to_dict("records")

display()

if __name__ == '__main__':
    app.run_server(debug=True, use_reloader=False)

def display() :
   return html.Div(
    children=[
    dcc.Dropdown(
            id='filter_dropdown',
            options=[{'label':st, 'value':st} for st in states],
            value = states[0]
            ),
    dt.DataTable(id='table-container') ]
    )```

1 Answer 1

2

Setting the initial data on layout looks to achieve what you need:

import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_table as dt
from dash.dependencies import Input, Output
import pandas as pd


df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')
app = dash.Dash(__name__)

states = df.State.unique().tolist()

@app.callback(Output('table-container', 'data'),
              [Input('filter_dropdown', 'value') ])
def display_table(state):
    dff = df[df.State==state]
    return dff.to_dict("records")


def display() :
   return html.Div(
    children=[
    dcc.Dropdown(
            id='filter_dropdown',
            options=[{'label':st, 'value':st} for st in states],
            value = states[0]
            ),
    dt.DataTable(
        id='table-container',
        columns=[{"name": i, "id": i} for i in df.columns],
        data=df.to_dict('records')
        )]
    )


app.layout = display()


if __name__ == '__main__':
    app.run_server(debug=True, use_reloader=True)

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

This is it. Dash doesn't like uninitialized values. You could even set it to data=[] on load, and let the callback update it with actual data.
Can't believe this is causing the problem! Thanks so much tom!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.