on_select event with plotly_chart not detected

Hello,
I am stuck since some days on the following issue : with new on_select parameter of st.plotly_chart everithing works well if I am using doc sample with px.data.iris, but if I am using my own dataframe selection event does not trigger and I can’t get back selection informations.
An idea?
Thanks.
David

run app locally
python 3.10
streamlit 1.46.0
plotly_express 0.4.1

import streamlit as st
import plotly.express as px
import numpy as np
import pandas as pd

# df = px.data.iris()  # iris is a pandas DataFrame
# Generate the large dataset
large_data = {
    'sepal_length': np.random.rand(1000),
    'col2': np.random.rand(1000)
}
df = pd.DataFrame(large_data)
df_downsampled = df.iloc[::10, :]
st.write(df_downsampled)

fig = px.scatter(df_downsampled, x=df_downsampled.index, y=df_downsampled.sepal_length)

event = st.plotly_chart(fig, key="test", on_select="rerun")
event

When you click on the plot, the app reruns and a new large_data dictionary is generated because new calls to np.random.rand are triggered. In other words, you are generating a new dataset on every app interaction. You can avoid that using st.session_state:

import streamlit as st
import plotly.express as px
import numpy as np
import pandas as pd

# df = px.data.iris()  # iris is a pandas DataFrame
# Generate the large dataset
if "large_data" not in st.session_state:
    st.session_state.large_data = {"sepal_length": np.random.rand(1000), "col2": np.random.rand(1000)}

df = pd.DataFrame(st.session_state.large_data)
df_downsampled = df.iloc[::10, :]
st.write(df_downsampled)

fig = px.scatter(df_downsampled, x=df_downsampled.index, y=df_downsampled.sepal_length)

event = st.plotly_chart(fig, key="test", on_select="rerun")
event