victoryhb / streamlit-option-menu

streamlit-option-menu is a simple Streamlit component that allows users to select a single item from a list of options in a menu.
MIT License
586 stars 69 forks source link

Page Config (page_title) #56

Open gclark01 opened 9 months ago

gclark01 commented 9 months ago

I figure I must be doing something wrong here.

    import streamlit as st
    from streamlit_option_menu import option_menu

    import home, workouts

    if __name__ == '__main__':
        st.set_page_config(
            page_title="Coach Workouts",
        )

    class MultiApp:

        def __init__(self):
            self.apps = []

        def add_app(self, title, function):
            self.apps.append({
                "title": title,
                "function": function
            })

        def run():
            with st.sidebar:
                app = option_menu(
                    menu_title='Coach Workouts',
                    options=['Home', 'Add Workout'],
                    icons=['house-fill', 'trophy-fill', 'house-fill'],
                    menu_icon='chat-text-fill',
                    default_index=0,
                    styles={
                        "container": {"padding": "5!important", "back icon":{"color": "white", "font-size": "23px"}},
                        "nav-link": {"color": "white", "font-size": "12px", "text-nav-selected":{"background-color": "#02AB21"}},
                        "menu-title": {"color": "white", "font-size": "15px"}
                        }
                )

            if app == "Home":
                home.app()
            if app == "Add Workout":
                workouts.app()

        run()

I am getting this error...

_StreamlitAPIException: set_pageconfig() can only be called once per app page, and must be called as the first Streamlit command in your script.

It seems to be coming from this module in __init__.py where it is setting page_title. If I comment out this line st.set_page_config(page_title="Option Menu", layout="wide") - then I no longer receive the error and I am able to add my own page title.

Is there a way to work around this or please help me understand what I am doing wrong.

kianniak commented 6 months ago

interested in this too. very frustrating issue!

fgdvir commented 6 months ago

I've tried this example myself with streamlit=1.29 and it works. The only thing I've changed from your example is """python if app == "Home": st.write("Home") if app == "Add Workout": st.write("Workout") """ Instead of your original code since I don't have those imports.

In general - streamlit lets you choose the page_config once. You should only put it in the main script you are running. If you add it more than once (regardless of this component) it will throw the error above.

I think this issue can be closed but I'll let you reply, if you think it is still related to the component and not to how you run your streamlit app

gclark01 commented 6 months ago

I guess that's the point the component is defining the page_config, which makes it, so I am unable to do so. I can go into the component and comment it out, but is that really the best approach?

fgdvir commented 6 months ago

@gclark01 - Ok sorry, now I see what you mean. Indeed in the init we have a config. @victoryhb - I see it came from the initial commit, is this on purpose?

@gclark01 - Can you give me a reproducable example where it fails? Even though I know it is not the best practice, for me in version 1.29 it doesn't fail with the code above (with the minor change I had to make, but I'm just wondering if something is happening there)

Please supply any relevant information so I can reproduce and see what is going on.

kianniak commented 6 months ago

if you run this function and st.config at same time you will reproduce it.

fgdvir commented 6 months ago

As I mentioned, I ran the code above, which contains the st.config but it didn't reproduce.

gclark01 commented 6 months ago

Very odd... I just fired up the app where I was testing this and with streamlit version 1.29 it appears to be working. No code changes. If I can find sometime today, I will build a new app with this module to make sure I didn't miss something. As of now it seems to be working.

kianniak commented 6 months ago

I forked, cloned and committed an edit fixing it but to my (limited knowledge) I believe that would just impact me.

Dikarabo-Molele commented 5 months ago

I got the same issue where I call set_page_config on the main page.

The module seems to be calling the same function in: https://github.com/victoryhb/streamlit-option-menu/blob/2197b89d4eec193681748f8890f00a280dd5ac72/streamlit_option_menu/__init__.py#L80

The reason I call set_page_config in my main function is because layout=wide in the module does not take effect for some reason.

Below screenshot shows my codebase and error message goes away when I comment out line 303 or remove 290-293

image

Potential cause: https://github.com/victoryhb/streamlit-option-menu/blob/2197b89d4eec193681748f8890f00a280dd5ac72/streamlit_option_menu/__init__.py#L80

Dikarabo-Molele commented 5 months ago

Just noticed a different behaviour. So I swapped the order of statements. Initially option_menu call was the first statement in my script and resulted into that issue.

I placed line 303 above line 290 and the error message went away