Can't enter values without updating a plotly figure

Hi @savva, welcome to the Streamlit Community! :wave: :partying_face:

Firstly, thank you for providing a reproducible example! It makes debugging so much easier.

go.Layout() has a uirevision parameter that when set to any value preserves the state of the figure.

In your example, add the following line after you create the figure:

fig.update_layout({"uirevision": "foo"}, overwrite=True)

Example

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")

Output

plotly-preserve-state

Happy Streamlit-ing! :balloon:
Snehan

3 Likes