Created
October 25, 2024 11:58
-
-
Save KobaKhit/4f3d86f1c48e8641d300e2db4f04499a to your computer and use it in GitHub Desktop.
Plolty Click Events in Streamlit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
plot.selections.in.streamlit.mp4