Skip to content

Instantly share code, notes, and snippets.

@KobaKhit
Created October 25, 2024 11:58
Show Gist options
  • Save KobaKhit/4f3d86f1c48e8641d300e2db4f04499a to your computer and use it in GitHub Desktop.
Save KobaKhit/4f3d86f1c48e8641d300e2db4f04499a to your computer and use it in GitHub Desktop.
Plolty Click Events in Streamlit
import plotly.express as px
import streamlit as st
# Sample data
df = px.data.iris()
# Create the Plotly figure
fig = px.scatter(df,
x="sepal_width",
y="sepal_length",
custom_data = ['species','species_id'])
# Display the chart with event handling
selected_points = st.plotly_chart(fig, key="iris_chart", on_select="rerun")
# Handle the selection event
if selected_points and selected_points['selection']["points"]!=[]:
customdata = selected_points['selection']["points"][0]['customdata']
c1,c2 = st.columns(2)
with c1:
st.write("Selected points:")
st.write(selected_points['selection']["points"])
with c2:
st.write(f'Species is: {customdata[0]}')
st.write(df[df.species_id == customdata[1]])
@KobaKhit
Copy link
Author

plot.selections.in.streamlit.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment