plotly / dash-core-components

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

Config not changed by callback #252

Open charleyferrari opened 5 years ago

charleyferrari commented 5 years ago

cc @plotly/dash

This was uncovered in the dash community: https://community.plot.ly/t/remove-show-figure-modebar-programatically/12254

I can't see any reason why config wouldn't update like any other prop in dcc.Graph. This is how I've been testing it out, the dropdown should just remove the modebar by updating the config.

The only thing I can think of is the shape of config not being matched, but it works fine when you hardcode config in the dcc.Graph

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go

app = dash.Dash()

app.layout = html.Div([
    dcc.Dropdown(id='dropdown', options=[
        {'label': 'Display Mode Bar', 'value': 'yes'},
        {'label': 'Don\'t display Mode Bar', 'value': 'no'}
    ], value='yes'),
    dcc.Graph(id='fig', figure=go.Figure(
        data=[
            go.Bar(x=['one', 'two', 'three'], y=[1, 2, 3])
        ]
    ))
])

@app.callback(
    Output(component_id='fig', component_property='config'),
    [Input(component_id='dropdown', component_property='value')]
)
def update_config(val):
    if val == 'yes':
        print('yes')
        return {'displayModeBar': True}
    else:
        print('no')
        return {'displayModeBar': False}

if __name__ == '__main__':
    app.run_server()
chriddyp commented 5 years ago

I can't see any reason why config wouldn't update like any other prop in dcc.Graph. This is how I've been testing it out, the dropdown should just remove the modebar by updating the config.

Looking at the code, we're just not handling the config argument: https://github.com/plotly/dash-core-components/blob/f9b2e93df60bc79aaf641459b1abb070229e72eb/src/components/Graph.react.js#L170-L191

charleyferrari commented 5 years ago

@chriddyp I tried building this myself appending this below the figureChanged logic:

    const configChanged = this.props.figure !== nextProps.config;

    if (configChanged) {
      this.plot(nextProps);
    }

and wasn't getting a modebar change working in a callback.

Do you know if there's something else needed?

chriddyp commented 5 years ago

@charleyferrari - Can you push up a PR? Then I can comment on your code directly.

In particular, this logic looks wrong:

    const configChanged = this.props.figure !== nextProps.config;

it should be

    const configChanged = this.props.config !== nextProps.config;
luggie commented 1 month ago

@chriddyp has there been done any fix on this since then?