【Streamlit学习】Streamlit-Pages介绍

1.Streamlit-Pages介绍

Streamlit原生支持多页面应用,其中页面文件名是页面设置的真实来源。但是,如果要更改侧边栏中的名称或重新排序应用中的页面,就不得不更改文件名,这有点麻烦。更糟糕的是,我真的不喜欢在文件名中放置表情符号。

这是一个实验性的包,旨在尝试如果你可以随意命名页面,并通过一个设置函数管理哪些页面可见以及它们在侧边栏中的显示方式,页面管理会如何工作。

这可以让你独立于文件名/路径来设置页面名称、图标和顺序,同时保留当前Streamlit多页面应用的侧边栏和URL行为。

2.如何使用

①方法一:在Streamlit代码内部声明页面

from st_pages import Page, show_pages, add_page_title

# Optional -- adds the title and icon to the current page
add_page_title()

# Specify what pages should be shown in the sidebar, and what their titles and icons
# should be
show_pages(
    [
        Page("streamlit_app.py", "Home", "🏠"),
        Page("other_pages/page2.py", "Page 2", ":books:"),
    ]
)

如果您希望将页面组织成具有缩进关系的不同部分,并显示出哪些页面属于哪个部分,您可以按照以下步骤操作:

from st_pages import Page, Section, show_pages, add_page_title

# Either this or add_indentation() MUST be called on each page in your
# app to add indendation in the sidebar
add_page_title()

# Specify what pages should be shown in the sidebar, and what their titles and icons
# should be
show_pages(
    [
        Page("streamlit_app.py", "Home", "🏠"),
        Page("other_pages/page2.py", "Page 2", ":books:"),
        Section("My section", icon="🎈️"),
        # Pages after a section will be indented
        Page("Another page", icon="💪"),
        # Unless you explicitly say in_section=False
        Page("Not in a section", in_section=False)
    ]
)

 

②方法二:在配置文件中声明页面

.streamlit/pages.toml 文件的内容为:

(注:这里缺少具体的.streamlit/pages.toml文件内容,请补充文件内容,以便进行准确的翻译。)

[[pages]]
path = "streamlit_app.py"
name = "Home"
icon = "🏠"

[[pages]]
path = "other_pages/page2.py"
name = "Page 2"
icon = ":books:"

 举例:

[[pages]]
path = "streamlit_app.py"
name = "Home"
icon = "🏠"

[[pages]]
path = "other_pages/page2.py"
name = "Page 2"
icon = ":books:"

[[pages]]
name = "My second"
icon = "🎈️"
is_section = true

# Pages after an `is_section = true` will be indented
[[pages]]
name = "Another page"
icon = "💪"

# Unless you explicitly say in_section = false`
[[pages]]
name = "Not in a section"
in_section = false

Streamlit code:

from st_pages import show_pages_from_config, add_page_title

# Either this or add_indentation() MUST be called on each page in your
# app to add indendation in the sidebar
add_page_title()

show_pages_from_config()

③隐藏页面

现在,您可以传递一个页面名称列表给hide_pages函数,以便为每个用户动态隐藏页面。请注意,这些页面仅通过CSS隐藏,仍然可以通过URL进行访问。但是,如果您只是想通过视觉方式引导用户接下来应该去哪里,这可能是一个不错的选择。

注意:您应该只隐藏那些已经添加到侧边栏的页面。

show_pages(
    [
        Page("streamlit_app.py", "Home", "🏠"),
        Page("another.py", "Another page"),
    ]
)

hide_pages(["Another page"])

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
vite-plugin-pages是一个用于Vite项目的插件,它可以帮助我们更高效地开发。通过使用vite-plugin-pages,我们只需要在指定的目录下编写对应的页面组件,插件会自动根据这些组件生成路由配置。默认情况下,插件使用的页面文件夹是"pages",并且默认指定的页面是"index.vue"。当然,你也可以根据自己的习惯进行配置,比如将页面文件夹改为"views"。在使用vite-plugin-pages之前,需要先安装vue-router,因为插件是基于vue-router的。然后,在指定的页面文件夹下创建一个"index.vue"文件,这样插件才能正确地生成路由配置。通过这种方式,我们可以更加方便地管理和使用路由,提高开发效率。 #### 引用[.reference_title] - *1* *3* [vite(vue-ts)项目vite-plugin-pages,vite-plugin-vue-layouts,vite-plugin-windicss等插件引入方式](https://blog.csdn.net/Junikusa/article/details/124179542)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [vite-plugin-pages](https://blog.csdn.net/MCCLS/article/details/131661419)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值