Last active
November 17, 2018 21:52
-
-
Save bakirillov/1c2e838ab70e0248a98b69651760385a to your computer and use it in GitHub Desktop.
Minimal example for incorrect DataTable behavior under tabs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dash | |
import dash_table | |
import pandas as pd | |
import dash_core_components as dcc | |
import dash_html_components as html | |
from dash.dependencies import Input, State, Output | |
external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"] | |
app = dash.Dash(__name__, external_stylesheets=external_stylesheets) | |
app.title = "Minimal tab error example" | |
app.layout = html.Div([ | |
dcc.Tabs(id="tabs", value="tab-1", children=[ | |
dcc.Tab(label="Some tab", value="tab-1"), | |
dcc.Tab(label="Second tab", value="tab-2") | |
]), | |
html.Div(id="tabs-content") | |
]) | |
app.config['suppress_callback_exceptions']=True | |
@app.callback( | |
Output("tabs-content", "children"), | |
[Input("tabs", "value")]) | |
def render_content(tab): | |
df = pd.DataFrame( | |
{"A":[1,2,3], "B": [1,2,3]}, | |
columns=["A", "B"] | |
) | |
if tab == "tab-1": | |
return( | |
html.Div([ | |
"Dash version: "+dash.__version__, html.Br(), | |
"Dash Table version: "+dash_table.__version__, html.Br(), | |
"Dash Core Components version: "+dcc.__version__, html.Br(), | |
"Dash HTML Components version: "+html.__version__, | |
html.Button("Change the data in the table", id="gobutton") | |
]) | |
) | |
elif tab == "tab-2": | |
return( | |
html.Div([ | |
dash_table.DataTable( | |
id='table', | |
columns=[{"name": i, "id": i} for i in df.columns], | |
data=df.to_dict("rows"), sorting=True, row_selectable="single", | |
filtering=True | |
) | |
]) | |
) | |
@app.callback( | |
Output("table", "rows"), | |
[Input("gobutton", "n_clicks")] | |
) | |
def on_click(n_clicks): | |
df = pd.DataFrame( | |
{"A":[5,6,7], "B": [5,6,7]}, | |
columns=["A", "B"] | |
) | |
if n_clicks: | |
return(df.to_dict("rows")) | |
if __name__ == '__main__': | |
app.run_server(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment