Next button / open new page / session state

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

Thanks in advance!

1 Like

Hey @Isa ,

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

pages

Let us know if this was helpful !

1 Like

Yes. Thank you very much!