Can someone confirm that when the switch_page function is called, all the code of the page I’m supposed to leave is re-executed once?
Little snippet as example [I’ve launched streamlit from conda terminal]
In Page1:
print(“Test”)
button = st.button(“Go to page 2”)
if button:
___switch_page(“Page2”)
The page is correctly changed but in my conda terminal I’m seeing “Test” printed twice.
As written, page 1 will reload and then the redirect to page 2 will trigger. The value of the variable button is false when page 1 first loads (redirect not triggered). When you click, since you have no callback function, page 1 will immediately reload, this time assigning true to button at which point you enter the conditional and trigger the redirect.
def do_a_thing():
# do a thing
st.button('Do A Thing', on_click=do_a_thing)
Or if you want to pass it the switch_page function directly:
st.button('Go to Page 2', on_click=switch_page, args=('Page2',))
When you interact with a widget, the callback function will run before reloading the page. I haven’t used the extras library, so I’m not sure how the page switching is implemented and if there would be a need to step around things more delicately. If the above doesn’t work, then you can put the logic to switch pages at the beginning of your script, pulling the button value from session_state directly if you need to.
Thank you so much for the advices. Unfortunately the callback it’s not working because it’s in conflict with the extras library. But the idea was great.
As workaround at the end I’ve implemented something similar but more code-related. I have a pandas profile call that is quite time-consuming, so by a session state flag I’m avoiding executing it the second time so even if the page is rerun the side-effect is invisible.
Anyway I found this quite annoying. I’ve another page quite “heavy” and full of functions and calls with some checkboxes, every time a checkbox is clicked (they’re supposed to be clicked and unclicked oftenly) all the page is always refreshed. I can get the logic of the reason, but it’s limiting a lot the powerful of this library. Sorry for the outburst though…
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.