# Set Trace selected via callback

**URL:** <https://community.plotly.com/t/set-trace-selected-via-callback/31947>\
**Category:** Dash Python\
**Created:** [November 27, 2019, 3:51pm UTC](https://community.plotly.com/t/set-trace-selected-via-callback/31947 "2019-11-27T15:51:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![smartLife](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@smartLife](https://community.plotly.com/u/smartLife)\
**Post date:** [November 27, 2019, 3:51pm UTC](https://community.plotly.com/t/set-trace-selected-via-callback/31947/1 "2019-11-27T15:51:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Emmanuelle](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emmanuelle/32/7853_2.png) [@Emmanuelle](https://community.plotly.com/u/Emmanuelle)\
**Post date:** [November 27, 2019, 4:15pm UTC](https://community.plotly.com/t/set-trace-selected-via-callback/31947/2 "2019-11-27T16:15:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![smartLife](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@smartLife](https://community.plotly.com/u/smartLife)\
**Post date:** [November 27, 2019, 4:38pm UTC](https://community.plotly.com/t/set-trace-selected-via-callback/31947/3 "2019-11-27T16:38:20Z")

</div>

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:

```auto
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)

```
