HI everyone,
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
I just tried this myself using your code and I’m getting the point to return (I’m running it on streamlit cloud), so I’m guessing it’s unique to your environment. I know this is dumb, but if you try rebooting does that help? Or worst case, recreate the project?
thanks for your time, I think you do not understant the issua.
the issue is st.session_state["var"] on the left does not updated with the x value…there a delay, try to click a bubble and see the st.session_state["var"] you will see that it is not updated. try that on your own.(see the pictire below)
So you might understand this, but without having the session state update in a callback from st.plotly_chart, I wouldn’t expect that value to update immediately. Instead, it will lag one behind, so if you click a second point, you will then see that value update to the first point you clicked
This happens because the entire code re-runs top to bottom when you click a point and plot() isn’t hit until after you write the “var” session state
Instead, you can do this, which captures the point in “var” with a callback and then re-runs
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 _update_session_state():
event = st.session_state.iris
if len(event.selection.points) != 0:
st.session_state["var"] = event.selection.points[0]["x"]
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"],
)
st.plotly_chart(fig, key="iris", on_select=_update_session_state)
with col3:
st.write(st.session_state["var"])
with col4:
plot()
That’s interesting, I haven’t used the callback with plotly_chart all that much, but it seems to be one of the few widgets in streamlit that doesn’t accept args. What you’re doing looks correct for almost any other widget
In this case, I think you would have to use session state, like so:
I was also looking for something similar, one thing that I have noticed it that with every user interaction the chart is re-rendered, how to control this behavior ?
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.