have a plotly graph with the ability to switch to box select in the modeBarButtons
To upload files, please first save the app
import streamlit as st
import plotly.express as px
import numpy as np
import pandas as pd
# Create sample data
np.random.seed(42)
df = pd.DataFrame({
'x': np.random.normal(0, 1, 100),
'y': np.random.normal(0, 1, 100),
'group': np.random.choice(['A', 'B', 'C'], 100)
})
# Create the scatter plot
fig = px.scatter(df, x='x', y='y', color='group')
# Update the layout to include box select in the modebar
fig.update_layout(
dragmode='select', # Default to select mode
modebar_add=['select2d'] # Add box select to modebar
)
# Display the plot and handle selections
selection = st.plotly_chart(
fig,
use_container_width=True,
key="scatter_plot",
on_select="rerun"
)
# Show selection data when points are selected
if selection and selection.selection:
st.write("Selected points:")
selected_indices = selection.selection.get("point_indices", [])
if selected_indices:
st.dataframe(df.iloc[selected_indices])
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?