I am developing a graph with plotly, it works well but I want to display some information using st.write on the screen when any point on the plot is clicked. In plotly, this can be easily done using the on_click method. But it doesn't work on streamlit. I have tried another library called plotly events, but it generates a black and white graph in the newer versions of streamlit. The graph is a simple scatterplot. import streamlit as st import plotly.express as px def click(trace, points, selector): st.write("hello") x = [1,2,3] y = [4,5,6] z = [10,11,2] fig = px.scatter(x=x, y=y, size = z) fig.data[0].on_click(click) st.plotly_chart(fig, use_container_width=True) btw, it doesn't matter if the line with the fig.data[0] line is put after st.plotly_chart line because its still not working. plz help
To upload files, please first save the app
import streamlit as st
import plotly.express as px
import pandas as pd
import numpy as np
# Initialize session state to store selected points
if 'selected_point' not in st.session_state:
st.session_state.selected_point = None
# Create random data for the scatterplot
np.random.seed(42)
x = np.random.rand(20) * 10
y = np.random.rand(20) * 10
z = np.random.rand(20) * 100
fig = px.scatter(x=x, y=y, size=z)
# Display the plot and capture selection event
event = st.plotly_chart(fig, use_container_width=True, on_select="rerun")
# Check if there is a selection from the event
if event and event['selection']['points']:
# Store the first selected point in session state
st.session_state.selected_point = event['selection']['points'][0]
# If a point is selected in the session state, display its details
if st.session_state.selected_point:
st.dataframe(pd.DataFrame([st.session_state.selected_point]), hide_index=True)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?