When I click a menu option, the component automatically refreshes dozens of times. This behavior disappears when I remove all instances of Streamlit.setComponentValue from the menu component or I put the code outside the st.sidebar.
When I execute st.rerun in Streamlit, this component first refreshes dozens of times, then it gets destroyed and rebuilt (remounted), leading to display errors.
Here is the code to reproduce the issue:
import streamlit as st
import streamlit_antd_components as sac
with st.sidebar:
selected_module = sac.menu(
[
sac.MenuItem("Home", icon="house-fill"),
sac.MenuItem(type="divider"),
sac.MenuItem("Samples", icon="file-richtext-fill"),
],
open_all=True,
color="blue",
index=2,
)
st.write("Current selection:", selected_module)
if st.button("re run"):
st.rerun()
After launching Streamlit, click on Home in the menu, and then click the button rerun. The menu component will be destroyed and rebuilt, resulting in the current selected value becoming Samples, while the component’s return value remains Home.
I’ve been trying to figure out the cause for a long time and haven’t been able to contact the author. Can anyone help?
I’m using streamlit==1.38.0, which version are you using? I’ve added a GIF to the post showing where the error occurs. Are you sure you can’t reproduce this issue?
import streamlit as st
from streamlit import session_state as ss
import streamlit_antd_components as sac
if "idx" not in ss: ss.idx = -1
with st.sidebar:
selected_module = sac.menu(
[
sac.MenuItem("Home", icon="house-fill"),
sac.MenuItem(type="divider"),
sac.MenuItem("Samples", icon="file-richtext-fill"),
],
open_all=True,
color="blue",
index=ss.idx,
)
st.write("Current selection:", selected_module)
if st.button("re run"): st.rerun()
I just understood what you mean. There is indeed an issue with ‘st. rerun()’. Normal component triggering does not exist. If you must use ‘st. rerun()’, I suggest you refer to Shawn_Pereira
Or use the following methods to solve this problem:
import streamlit as st
from streamlit import session_state as ss
import streamlit_antd_components as sac
def onChange_test():
st.session_state.idx = st.session_state.test
if "idx" not in ss: ss.idx = 0
with st.sidebar:
selected_module = sac.menu(
[
sac.MenuItem("Home", icon="house-fill"),
sac.MenuItem(type="divider"),
sac.MenuItem("Samples", icon="file-richtext-fill"),
],
open_all=True,
color="blue",
index=ss.idx,
key='test',
return_index=True, #
on_change=onChange_test #
)
st.write(st.session_state)
st.write("Current selection:", selected_module)
if st.button("re run"): st.rerun()
@Shawn_Pereira@lkdd-ao
Thanks to both of you for your help, but I’ve thought of another question:
Official components can retain their state after executing a rerun, but custom components cannot. For the components I’ve developed myself, is there a way to preserve their state after a rerun?
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.