dash-pivottable icon indicating copy to clipboard operation
dash-pivottable copied to clipboard

Pivottable doesn't get updated in successive callbacks

Open WestXu opened this issue 5 years ago • 10 comments

This works:

import dash
import dash_html_components as html
from dash.dependencies import Input, Output

import dash_pivottable

app = dash.Dash(__name__)

app.layout = html.Div(
    [html.Button("Rfresh", id="button"), html.Div(id="pivottable_div")]
)


@app.callback(Output("pivottable_div", "children"), [Input("button", "n_clicks")])
def refresh_pivottable(n_clicks):
    if n_clicks:
        print(n_clicks)
        return [
            html.Div(str(n_clicks)),
            dash_pivottable.PivotTable(data=[["a"], [n_clicks]], cols=["a"])
            if n_clicks % 2 == 1
            else "a",
        ]


if __name__ == "__main__":
    app.run_server(debug=False)

This doesn't:

import dash
import dash_html_components as html
from dash.dependencies import Input, Output

import dash_pivottable

app = dash.Dash(__name__)

app.layout = html.Div(
    [html.Button("Rfresh", id="button"), html.Div(id="pivottable_div")]
)


@app.callback(Output("pivottable_div", "children"), [Input("button", "n_clicks")])
def refresh_pivottable(n_clicks):
    if n_clicks:
        print(n_clicks)
        return [
            html.Div(str(n_clicks)),
            dash_pivottable.PivotTable(data=[["a"], [n_clicks]], cols=["a"])
            # if n_clicks % 2 == 1
            # else "a",
        ]


if __name__ == "__main__":
    app.run_server(debug=False)

The only difference of them is the 2 commented lines.

My environment:

windows7 64bit
python3.7 64bit
dash 1.4.1

Please run the cases to see the difference. Many thanks.

WestXu avatar Nov 01 '19 03:11 WestXu

has this been solved?

futureplant avatar Jan 12 '20 14:01 futureplant

+1, I am also running into this issue.

If I use a dash_table, it updates when the callback is fired after the first time. Same callback structure and everything for dash_pivottable and it does not update when the callback is fired.

Has anybody come up with a workaround?

ghost avatar Feb 04 '20 19:02 ghost

Still no luck on this one? Encountering the same issue..

Qua111 avatar Mar 04 '20 19:03 Qua111

I also had some trouble updating the pivot table properties though callbacks. You can hack it by placing the PT inside a dcc.Loading element, and make a secondary callback to a non data property (I used menuLimit) when data is changed, but its all pretty hacky.

abogaard avatar Jun 17 '20 08:06 abogaard

Same issue here

radupm avatar Oct 07 '20 09:10 radupm

Same issue, @abogaard Could you please give us an example code? I tried to put the PT inside a dcc.Loading, and fire the callback using same input with the data generator(Some dropdown selector to filter data or choose file), but it still doesn't work. :(

standard-outlier avatar Nov 05 '20 09:11 standard-outlier

Same issue, @abogaard Could you please give us an example code? I tried to put the PT inside a dcc.Loading, and fire the callback using same input with the data generator(Some dropdown selector to filter data or choose file), but it still doesn't work. :(

Update: My problem is caused by the table's ID, I generate every table with a random table id in callbacks solved my problem.

standard-outlier avatar Nov 07 '20 14:11 standard-outlier

@wittyfans could you explain it more or provide a code example?

adrienpacifico avatar Sep 09 '21 12:09 adrienpacifico

@adrienpacifico I had the same problem with loading in contents from a file that is chosen by a dropdown. This solution works for me. I think @wittyfans did it with some random number generator but the main point seems to be to change the id.

from datetime import datetime

@app.callback(
    Output('pivottable_div', 'children'),
    Input('file-dropdown', 'value')
)
def update_output(value):
    # Do stuff to load data
    return [
        dash_pivottable.PivotTable(
            id="%s" % datetime.now(),
            ...
        )
    ]

tomdaniek avatar Oct 26 '21 13:10 tomdaniek

@adrienpacifico I had the same problem with loading in contents from a file that is chosen by a dropdown. This solution works for me. I think @wittyfans did it with some random number generator but the main point seems to be to change the id.

from datetime import datetime

@app.callback(
    Output('pivottable_div', 'children'),
    Input('file-dropdown', 'value')
)
def update_output(value):
    # Do stuff to load data
    return [
        dash_pivottable.PivotTable(
            id="%s" % datetime.now(),
            ...
        )
    ]

work like a charm!

namnguyen8-tiki avatar Aug 23 '24 11:08 namnguyen8-tiki