Function is not called

I have this code, the function of this code is to show a chart and when I select a part it calls a function, this code worked in jupyter notebook, but I had to pass the code to streamlit and just now when I select something in the chart the function is not called.

import streamlit as st
import plotly.graph_objs as go
import pandas as pd

st.set_page_config(layout="wide")

def find_extremes(trace, points, selector):
        print("Hey")
        st.write(selector)
        xrange = selector.xrange
        keep_condition = (df['time_stamp'] >= xrange[0]) & (df['time_stamp'] <= xrange[1])
        filtered_df = df[keep_condition]
        st.write(filtered_df)

Csv_uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
if Csv_uploaded_file is not None:
    df = pd.read_csv(Csv_uploaded_file, names=["gx", "gy", "gz", "ax", "ay", "az", "roll", "pitch", "yaw", "time_stamp", "FLAG_VIDEO", "TAG"])
    trace1 = go.Scatter(y=df['ax'], x=df['time_stamp'], mode='lines', name='AccX')
    trace2 = go.Scatter(y=df['az'], x=df['time_stamp'], mode='lines', name='AccZ')
    data = [trace1, trace2]
    fig = go.FigureWidget(data=data)
    fig.update_layout(
        width=1000,
        height=500,
        dragmode="select",
        selectdirection="h",
    ) 
    scatter = fig.data[0]
    st.plotly_chart(fig)
    scatter.on_selection(find_extremes)

Here’s a thread that might help explain why FigureWidget click interactions don’t work as expected in Streamlit: