[Python] How to make streamlit session_state updated when I select a point on plotly_chart

Discussão em 'Python' iniciado por Stack, Fevereiro 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have this use case where I have a plotlychart :

    import streamlit as st
    import plotly.express as px

    if "var" not in st.session_state:
    st.session_state["var"] = 0

    col3,col4 = st.columns(2)


    def plot():
    df = px.data.iris()
    fig = px.scatter(
    df,
    x="sepal_width",
    y="sepal_length",
    color="species",
    size="petal_length",
    hover_data=["petal_width"],
    )

    event = st.plotly_chart(fig, key="iris", on_select="rerun")
    event.selection
    if len(event.selection.points)!=0:
    st.session_state["var"] = event.selection.points[0]["x"]
    # st.rerun()


    with col3:
    st.write(st.session_state["var"])

    with col4:
    plot()


    my issue is when I select a point on the graph, logically the st.session_state["var"] should be updated directly …but it is not the case here…I have tried to use st.rerun() but it is enter in infinity loop…do you have any idea please to make st.session_state["var"] updated directly when I select a point ? I want to keep the order of cols as it is. thanks

    Continue reading...

Compartilhe esta Página