hey, would appreciate advice from anybody more knowledgeable:)
i am plotting a figure, and below i have multiple fields that the user is to enter after inspecting a figure. i expect the user to zoom in, and then enter the values. problem is, that any time you enter anything, the plot resets. i don’t want that.
my desired behavior is as follows:
zoom in on the plotly figure (client state of plotly figure is changed)
enter a number / select an option
press a button to update the plot based on number/select inputs
my problem is that the plotly figure resets itself after step 2 (after even an unsubmitted select, the client state of the plotly figure is reset).
i tried putting everything into an st.form(), tried caching / session-stating the figure; neither work. client changes to plotly figure always get reset
i attached a simple code snippet as an example.
would appreciate advice!
import streamlit as st
import plotly.figure_factory as ff
import numpy as np
if 'bin_size' not in st.session_state:
st.session_state['bin_size'] = 0.1
if 'plot_select' not in st.session_state:
st.session_state['plot_select'] = 'plot1'
# put figure inside a form?
with st.form("form2"):
submitted2 = st.form_submit_button(label='update plot')
if submitted2:
x = {'plot1': np.random.randn(100), 'plot2': np.random.randn(50)}
fig = ff.create_distplot([x[st.session_state['plot_select']]], [st.session_state['plot_select']], bin_size=[st.session_state['bin_size']])
st.plotly_chart(fig, use_container_width=True)
# put select inside a form?
with st.form("form"):
new_plot_select = st.selectbox("Select plot", ['plot1', 'plot2'])
new_bin_size = st.number_input('Enter bin size', value = 0)
submitted = st.form_submit_button(label='i want plot to reset on button press only')
if submitted:
st.session_state['bin_size'] = new_bin_size
st.session_state['plot_select'] = new_plot_select
st.header("zoom in on the plot before selecting/entering values")
st.header("any action would cause the plot to reset; i don't want that")
import numpy as np
import plotly.figure_factory as ff
import streamlit as st
if "bin_size" not in st.session_state:
st.session_state["bin_size"] = 0.1
if "plot_select" not in st.session_state:
st.session_state["plot_select"] = "plot1"
# put figure inside a form?
with st.form("form2"):
submitted2 = st.form_submit_button(label="update plot")
if submitted2:
x = {"plot1": np.random.randn(100), "plot2": np.random.randn(50)}
fig = ff.create_distplot(
[x[st.session_state["plot_select"]]],
[st.session_state["plot_select"]],
bin_size=[st.session_state["bin_size"]],
)
fig.update_layout({"uirevision": "foo"}, overwrite=True)
st.plotly_chart(fig, use_container_width=True)
# put select inside a form?
with st.form("form"):
new_plot_select = st.selectbox("Select plot", ["plot1", "plot2"])
new_bin_size = st.number_input("Enter bin size", value=0)
submitted = st.form_submit_button(
label="i want plot to reset on button press only"
)
if submitted:
st.session_state["bin_size"] = new_bin_size
st.session_state["plot_select"] = new_plot_select
st.header("zoom in on the plot before selecting/entering values")
st.header("any action would cause the plot to reset; i don't want that")
Until Streamlit version 1.9.0 my plotly charts with uirevision parameter worked as expected, but after updating to the latest Streamlit 1.11.1 they seem to not work anymore.
Here a simple example adding the uirevision parameter - that doesn’t work. Do you know what is wrong? Thanks a lot in advance!
import streamlit as st
import plotly.figure_factory as ff
import numpy as np
# Add a "useless" button just to be able to rerun app with a click and see if uirevision works
st.button('Rerun app')
# Add histogram data
if 'x1' not in st.session_state:
st.session_state.x1 = np.random.randn(200) - 2
st.session_state.x2 = np.random.randn(200)
st.session_state.x3 = np.random.randn(200) + 2
# Group data together
hist_data = [st.session_state.x1, st.session_state.x2, st.session_state.x3]
group_labels = ['Group 1', 'Group 2', 'Group 3']
# Create distplot with custom bin_size
fig = ff.create_distplot(
hist_data, group_labels, bin_size=[.1, .25, .5])
# Add uirevision parameter to fig
fig.update_layout({"uirevision": "foo"}, overwrite=True)
# Plot!
st.plotly_chart(fig, use_container_width=True)
Hi, is there a way to get the ‘updated’ state out of the ‘updated’ figure? for example if the user zooms in, how can we get the updated x axes range out of the layout? if you st.write(fig.layout) this doesn’t seem to change…
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.