Streamlit multi-page sidebar not updating for the first time

Hi,
I have a multi-page app setup.
I run my main.py with “–client.showSidebarNavigation=False” so that by default the sidebar is not visible.
The contents of my sidebar will be based on the login credential.
Once the user logs in, I do a “switch_page” to say “pages/admin_view.py”.
In this page, I render a logout button (my own st.button)
And then create new links in the sidebar using “st.page_link”
In each such new page whose link is now in the side-bar, I render the “logout” button when it is run (i.e when clicked) and also render the same page-links related to “admin”. Thus the side-bar contents remain the same when I switch between these pages.

ISSUE:
The first time I switch to one of the pages created using “st.page_link”, I encounter duplicate key issue with my st.button that I use to implement “logout”
But from the next click onwards, its all well set and no issue arises.
Its only for the first time, I encounter this weird issue.
I am using streamlit 1.31.
I also tried with 1.32 and the problem is the same.
I think this is a bug… Anyone else has witnessed such an issue?

Show us the code or create a minimal example code to show the issue.

Sure, will do next week. Right now, I am living with the issue, working towards a hard deadline.

1 Like

Ok, I identified the issue.

I had 2 pages:
admin_view.py
admin_tasks.py

The sidebar drawing function is in admin_view.py and renders a logout button at the end after rending the different page_links pertaining to the admin role.

In admin_tasks, I import like this → “from pages.admin_view import admin_sidebar”, and then invoke the function.

When doing so, the very first time when you click “admin_view” link in the app, you encounter this issue of duplicate widget and after that no issue is seen,

If I replicate the admin_sidebar function in admin_view.py, there is no issue at all.

Hope this helps.

I have a repro case. How do I upload this?
–EDIT–
Never mind, I just added a post below with repro. Hope that works for you. Thanks!

repro

Streamlit version
1.32.2

Folder structure
Sorry for poor markdown. I dont have a good tree command

|-app.py
|-pages
|-----| admin_view.py
|-----| admin_tasks.py

app.py

import streamlit as st

st.title('Main page')
st.switch_page('pages/admin_view.py')

pages/admin_view.py

import streamlit as st

st.title('Admin view page')

def admin_sidebar():
    with st.sidebar:
        st.page_link('./pages/admin_view.py', label='Admin home')
        st.page_link('./pages/admin_task.py', label='Admin tasks')
        st.button('logout', key='logout')

admin_sidebar()

pages/admin_tasks.py

import streamlit as st
from pages.admin_view import admin_sidebar

st.title('Admin tasks page')

admin_sidebar()

Cmdline

streamlit run --client.showSidebarNavigation=False app.py

The problem
Click on ‘Admin tasks’ link in the side-bar.
It fails for the first time and then subsequent clicks are fine.

UPDATE: I re-tested it again today and I see that when I move the admin_sidebar() to a separate file, it just works fine. I must have missed something. Please ignore this post

btw… The error persisted even when I moved the “admin_sidebar()” function to a separate file called admin_utils.py

When I moved the logout button to the actual file (admin_tasks.py), everything worked well…

@ferdy The repro case is above the post. The workaround is in this post. Hope this suffices. Thanks!

I can reproduce it.

One fix is to put the admin_sidebar() in a different module.

module/menu.py

import streamlit as st

def admin_sidebar():
    with st.sidebar:
        st.page_link('./pages/admin_view.py', label='Admin home')
        st.page_link('./pages/admin_task.py', label='Admin tasks')
        st.button('logout', key='logout')

Then other files can use thru imports.

pages/admin_task.py and pages/admin_view.py

import streamlit as st
from modules.menu import admin_sidebar

admin_sidebar()

@ferdy Thanks for testing and for your suggestion. I had already tried your suggestion and updated in a post above. That did not work too… The “st.button” call needs to be in the admin_xxx file. The page_links can be outside in a module… You can verify this too. Thanks again!

The one I am suggesting works well from my test. I have not encountered any errors.

image

1 Like

Oops… you are right. I re-tested it again and your suggestion works just fine. I must have been mistaken yesterday.
Let me post a errata in my prev post.
Thanks for your time!