Streamlit selectbox setting value

Hi all.
I have a dashboard that uses a dropdown selectbox to select a page, as well as buttons in the sidebar. When using the selectbox, I can use widgets on any page with no problems.
However, when selecting a page from the buttons in the sidebar, the relevant page loads, but as soon as I try to interact with a widget on that page it automatically refreshes back to the 1st page, likely because the selectbox is still showing that as the selected option. Please see code below.
How can I set the value of the selectbox to avoid this?

# Import necessary libraries 

from DashboardStreamlit import dbrd

from page2 import dbrd2

import streamlit as st

# Define the multipage class to manage the multiple apps in our program 

class MultiPage: 

    # Framework for combining multiple streamlit applications.

    def __init__(self) -> None:

        # Constructor class to generate a list which will store all our applications as an instance variable.

        self.pages = []

    

    def add_page(self, title, func) -> None: 

        # Class Method to Add pages to the project

        # Args:

        #     title ([str]): The title of page which we are adding to the list of apps 

            

        #     func: Python function to render this page in Streamlit

        self.pages.append({

          

                "title": title, 

                "function": func

            })

    def run(self):

        # Dropdown to select the page to run

        mainbtn = st.sidebar.button("Main dashboard interface")

        pg2btn = st.sidebar.button("Page 2")

        

        if mainbtn == True:

            page={'title': 'Main dashboard interface', 'function': dbrd}

            

        if pg2btn==True:

            page={'title': 'Page 2', 'function': dbrd2}

            

        else:

            page = st.selectbox(

            'Globalstratos Analytics Navigation', 

            self.pages, 

            format_func=lambda page: page['title']

            )

        #print(page)

        # run the selected app function

        

        page['function']()

Hello all, just bumping as I really need a solution here.

you can try this

select=st.sidebar.radio("choose an option",("A","B","C"))

or

select=st.sidebar.selectbox("choose an option",("A","B","C"))

two ways both can change pages

From a quick look I think the problem here is actually these lines:

mainbtn = st.sidebar.button("Main dashboard interface")
pg2btn = st.sidebar.button("Page 2")

The way buttons work is that their value will be True only in the instant they were clicked. Afer that, their value goes back to False.

So when you click on Page 2 and then interact with any other widget, pg2btn goes back to False and you’re sent to the else: clause.

The solution would be to replace the buttons with a stateful widget like a selectbox (if you have lots of pages) or radio buttons (if only a few pages). For example:

pages = {
  "main": "Main dashboard interface",
  "page2": "Page 2",
}
  
selected_page = st.radio("Select page", pages.values())

if selected_page == pages["main"]:
  ...
elif selected_page == pages["page2"]:
  ...

By the way, we’re working on an official way for doing multi-page apps right now. It’s still in early conceptual phase, but I’m already loving the directions we’re exploring! Should make this kind of thing much simpler

Awesome!

Thank you for the info :slight_smile:

image

Well that’s quite similar to my code and I get a neat little sidebar like this:

image

But on selecting any other option, the function against it does not run (due to a streamlit re-run). Details: How to create recurring forms with unique keys? - #9 by joe733

Where am I going wrong?

Hi Joe. Check this other thread of mine out, where I think there was useful guidance.

https://discuss.streamlit.io/t/set-value-of-selectbox/15779

Hi, @ryanblumenow I’m unable to access that link…

Here you go Joe. I hope it helps.

1 Like