Hey. In my application I have a radio on the sidebar where I can change between different pages (Page 1, Page 2, …). When I am on Page 1 I want to have a next button that directly leads me to Page 2. In my code the Page 2 appears below Page 1, but I want it to be displayed exactly the same way as it would have been selected with the radio bar, as only the Page 2.
import streamlit as st
st.session_state.page_select = st.sidebar.radio('Pages', ['Page 1', 'Page 2', 'Page 3'])
if st.session_state.page_select == 'Page 1':
st.title('Page 1')
next = st.button('Go to page 2')
if next:
st.session_state.page_select = 'Page 2'
if st.session_state.page_select == 'Page 2':
st.title('Page 2')
next2 = st.button('Go to page 3')
if next2:
st.session_state.page_select = 'Page 3'
if st.session_state.page_select == 'Page 3':
st.title('Page 3')
There is a workaround this without using session_state. It will allow you to move through your radio in the sidebar by clicking on the next button like it was explained in this solution.
To make it more clear for you I have customized the solution to respond to your request with this code:
import streamlit as st
import pickle as pkle
import os.path
pages = ['Page1','Page2','Page3']
if os.path.isfile('next.p'):
next_clicked = pkle.load(open('next.p', 'rb'))
if next_clicked == len(pages):
next_clicked = 0
else:
next_clicked = 0
if next:
next_clicked = next_clicked+1
if next_clicked == len(pages):
next_clicked = 0
choice = st.sidebar.radio("Pages",('Page1','Page2', 'Page3'), index=next_clicked)
pkle.dump(pages.index(choice), open('next.p', 'wb'))
if choice == 'Page1':
st.title('Page 1')
elif choice == 'Page2':
st.title('Page 2')
elif choice == 'Page3':
st.title('Page 3')
next = st.button('Go to next page')
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.