如何在流式多页面应用程序中隐藏页面?

问题描述 投票:0回答:1

我使用侧边栏开发了一个具有多个页面的 Streamlit 应用程序。我的目标是实现一种机制,单击侧边栏中的一个页面将隐藏其他页面。我正在寻求有关如何有效实现此行为的指导。您能否为实现此功能提供帮助或建议?

我创建了一个包含多个页面的 Streamlit 应用程序,但目前,当我单击一个页面时,侧边栏仍保留在其他页面上。我想修改此行为,以便单击一个页面会隐藏其他页面上的侧边栏。 这是代码:

enter code here

def run():
    with st.sidebar:
        app = option_menu(
            menu_title="pondering",
            options=['Account','Trending','Your Posts','About'],
                    default_index=1)
    if app == 'Account':
        account.app()
    if app == 'home':
        home.app()
    if app == 'Trending':
        trending.app()
    if app == 'Your Posts':
        your_posts.app()
    if app == 'About':
        about.app()
    run()
   
python user-interface web-applications show-hide streamlit
1个回答
0
投票

您可以使用以下自定义函数,并在要隐藏侧边栏的页面函数的第一行中调用它。

def hide_sidebar():
    st.markdown("""
    <style>
        section[data-testid="stSidebar"][aria-expanded="true"]{
            display: none;
        }
    </style>
    """, unsafe_allow_html=True)

示例:

def home():
    hide_sidebar() 
    ... 

如果您不想使用某个功能,则必须复制

markdown
并将其粘贴到所有页面中。

© www.soinside.com 2019 - 2024. All rights reserved.