I have a multipage app with two subpages. A button outputs a graphic when the button is clicked. If I now change the page via navigation and back again, the output of the button is gone. But how can I keep the old output there? It should only update the output (graphic) when I click again on the button.
I tried submit_form elements but still the output of the button is gone, if switch between the subpages. Is there a solution?
Based on your description, the general structure should work.
# importing libraries
import streamlit as st
import matplotlib.pyplot as plt
# define generic ploting function
def plot():
fig, ax = plt.subplots()
ax = plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
return fig
# create sidebar-selectbox for multi-pages
page = st.sidebar.selectbox(label='page', options=('Page One', 'Page Two'))
# define first page of app
if page == 'Page One':
st.title(body='Plotting Page')
if st.button(label='Plot'):
# create the plot then display it
fig = plot()
st.pyplot(fig)
# save the plot to session state
st.session_state.plot = fig
# define second page of app
else:
st.title(body='Second Page')
if st.button(label='recall plot'):
# recall plot from session state
fig = st.session_state.plot
st.pyplot(fig)
Your example shows the problem very well. If I click the button on page 1 and then switch to page 2 and back to page 1 the output is gone. (I have to click button on page 1 again…). I tried also with session states but it does not hold the output on multipages.
Okay, I think I understand your problem better now. What you want to do is then, check if the plot exists in the session state, if true you plot it, else you must click the button to create the plot for the first time.
# importing libraries
import streamlit as st
import matplotlib.pyplot as plt
# define generic ploting function
def plot():
fig, ax = plt.subplots()
ax = plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
return fig
# create sidebar-selectbox for multi-pages
page = st.sidebar.selectbox(label='page', options=('Page One', 'Page Two'))
# define first page of app
if page == 'Page One':
st.title(body='Plotting Page')
# check if plot is already in session state
if 'plot' in st.session_state:
fig = st.session_state.plot
st.pyplot(fig)
elif st.button(label='Plot'):
# create the plot then display it
fig = plot()
st.pyplot(fig)
# save the plot to session state
st.session_state.plot = fig
# define secon page of app
else:
st.title(body='Second Page')
if st.button(label='recall plot'):
# recall plot from session state
fig = st.session_state.plot
st.pyplot(fig)
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.