Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page Config (page_title) #56

Open
gclark01 opened this issue Nov 26, 2023 · 10 comments
Open

Page Config (page_title) #56

gclark01 opened this issue Nov 26, 2023 · 10 comments

Comments

@gclark01
Copy link

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_page_config() 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
Copy link

interested in this too. very frustrating issue!

@fgdvir
Copy link
Collaborator

fgdvir commented Feb 12, 2024

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
Copy link
Author

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
Copy link
Collaborator

fgdvir commented Feb 12, 2024

@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
Copy link

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

@fgdvir
Copy link
Collaborator

fgdvir commented Feb 14, 2024

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

@gclark01
Copy link
Author

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
Copy link

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

@Dikarabo-Molele
Copy link

Dikarabo-Molele commented Apr 8, 2024

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:

st.set_page_config(page_title="Option Menu", layout="wide")

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:

st.set_page_config(page_title="Option Menu", layout="wide")

@Dikarabo-Molele
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants