plotly / dash-table

OBSOLETE: now part of https://github.com/plotly/dash
https://dash.plotly.com
MIT License
420 stars 72 forks source link

Reset active_cell when data is changed (bug?) #813

Open vantaka2 opened 4 years ago

vantaka2 commented 4 years ago

https://www.loom.com/share/fc2a6156ee5940f7b9fd46e0adfd35cc

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

a = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']), 'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df1 = pd.DataFrame(a)

b = {'three': pd.Series([10, 20, 30], index=['a', 'b', 'c']), 'four': pd.Series([10., 20., 30., 40.], index=['a', 'b', 'c', 'd'])}
df2 = pd.DataFrame(b)

app = dash.Dash(__name__)

app.layout = html.Div(
    [dash_table.DataTable(
    id='table',
),
dcc.RadioItems( id='table_select', options=[
        {'label': 'df1', 'value': 'df1'},
        {'label': 'df2', 'value': 'df2'},
    ],
    value='df1',)])
@app.callback([
    Output('table', 'columns'),
    Output('table', 'data'),
    Output('table', 'active_cell')],
    [Input('table_select', 'value')]
)
def update_styles(value):
    print(value)
    if value == 'df1':
        df = df1 
    else:
        df = df2
    return [[{"name": i, "id": i} for i in df.columns],df.to_dict('records'),None]

if __name__ == '__main__':
    app.run_server(debug=True)
gentrexha commented 4 years ago

One alternative solution is to put your DataTable object in a html.Div and then select that container as the Output and just return a whole new DataTable object.

vantaka2 commented 4 years ago

@gentrexha - Appreciate the resposne, This is definitely an alternative but adds a lot of complexity if you have over 20+ data sets.

If there are callbacks required for the DataTable, one would have to re-create it for every instance of the DataTable.