Set Trace selected via callback

Hey everyone how can I set/trigger the selected property of the Figure-Trace in Python via a callback ?

1 Like

Hi @smartLife in a callback you need to update the whole figure, that is create a figure object with data (including the trace of interest) and layout and pass it to the figure property of a dcc.Graph in the output of the callback.

ok I got it, thank you. I have to set the clickmode=‘select’ and manipulate the selectedpoints in the data
Here is a toy-example if somebody needs:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, Input, State

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(children=[
    html.H1(children='Prototype'),

    html.Div(children='''
        Dash: A web application framework for Python.
    '''),
    html.Button('Select A', id='button1'),

    dcc.Graph(
        id='example_graph',
        figure={
            'data': [
                {'x': ['A'], 'y': [5], 'type': 'bar', 'name': 'A'},
                {'x': ['B'], 'y': [7], 'type': 'bar', 'name': 'B'},

            ],
            'layout': {
                'clickmode' : 'event+select',
                'hovermode': 'closest',


            }
        }
    )
])


@app.callback(Output(component_id='example_graph', component_property='figure'),
              [Input(component_id='button1', component_property='n_clicks')],
              [State(component_id='example_graph', component_property='figure')])
def update_output1(n_clicks, fig_state):

    if n_clicks is not None:
        fig_state['data'][0]['selectedpoints']= [0]
        fig_state['data'][1]['selectedpoints']= []

    return  fig_state

if __name__ == '__main__':
    app.run_server(debug=True)
2 Likes