Get selected data from plotly parallel coordinates plot (in notebook or streamlit app)

I’m building a streamlit app and I would like to have a parallel coordinates plot with a table below it showing the entries that were selected.

This is the sample code for a paralell coordinates plot, within streamlit (but the same applies to a jupyter notebook):

import plotly.express as px
import streamlit as st

df = px.data.iris()
fig = px.parallel_coordinates(df, color="species_id",
                              dimensions=['sepal_width', 'sepal_length', 'petal_width',
                                          'petal_length'],
                              color_continuous_scale=px.colors.diverging.Tealrose,
                              color_continuous_midpoint=2)

st.plotly_chart(fig)

if st.button('show selection'):
    st.text(fig['data'])
    tmp_df = df # Do something here to get the selected data
    st.dataframe(tmp_df)

Some posts regarding this topic are using dash to build their platform, that that is not my case. I am using streamlit and don’t fully understand how they are achieving this. The variable “data” from my figure does not update when selecting data, in streamlit or a test notebook, and I am not used to working with callbacks in python o I don’t know what to look for. I would appreciate help understading how to make this work

Mirror question in Stack overflow

Please refer to the answer on stackoverflow.