I want a scatter plot of data points where I can click a data point to toggle the colour between its original colour and black (to indicate it has been investigated). I want to be able to click the same data point repeatedly to toggle it back and forth. At the moment if you click the same data point more than once, the callback is not triggered. Here is a simple example of my code so far:
# Sample DataFrame
df = pd.DataFrame({
'Latitude': [51.5074, 48.8566, 40.7128],
'Longitude': [-0.1278, 2.3522, -74.0060],
'GinNumber': ['A123', 'B456', 'C789'],
'DateIncident': ['2024-01-01', '2024-02-01', '2024-03-01'],
'IncidentLocation': ['London', 'Paris', 'New York'],
'CasualtyName': ['John Doe', 'Jane Smith', 'Ben Johnson'],
'Inspected': [False, False, False]
})
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Graph(id='map'),
dcc.Store(id='df-store', data=df.to_json()),
html.Button(id='dummy-button', style={'display': 'none'}) # Dummy button to trigger callback
])
@app.callback(
Output('map', 'figure'),
[Input('map', 'clickData'),
Input('dummy-button', 'n_clicks')],
[State('df-store', 'data')]
)
def update_marker_color(clickData, dummy_button_n_clicks, df_json):
df_new = pd.read_json(df_json)
if clickData is not None:
pointIndex = clickData['points'][0]['pointIndex']
# Toggle the 'Inspected' value
df_new.at[pointIndex, 'Inspected'] = not df_new.at[pointIndex, 'Inspected']
else:
print('click is none')
# Update marker color based on 'Inspected' column
fig = px.scatter_mapbox(df_new, lat="Latitude", lon="Longitude", hover_name="GinNumber", color="Inspected", zoom=3, height=300, hover_data=['GinNumber','DateIncident','IncidentLocation','CasualtyName','Latitude','Longitude'])
fig.update_layout(mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig['layout']['uirevision'] = 'some-constant'
return fig
if __name__ == '__main__':
app.run_server(debug=True)