【问题标题】:How to create tabs on streamlit that work independently如何在 streamlit 上创建独立工作的选项卡
【发布时间】:2022-08-19 04:13:05
【问题描述】:

我正在尝试创建在 Streamlit 上独立工作的选项卡。

官方文档解释了here 如何为例如创建三个选项卡。它工作正常。但是,当我更改(假设是 Cat 选项卡)的代码以便引发错误时,我无法再切换到 Dog 选项卡或 Owl 选项卡。

代码 :

import streamlit as st

tab1, tab2, tab3 = st.tabs([\"Cat\", \"Dog\", \"Owl\"])

with tab1:
    st.header(\"A cat\")
    st.image(\"_\", width=200) # I changed here on purpose to raise an error

with tab2:
    st.header(\"A dog\")
    st.image(\"https://static.streamlit.io/examples/dog.jpg\", width=200)

with tab3:
    st.header(\"An owl\")
    st.image(\"https://static.streamlit.io/examples/owl.jpg\", width=200)

错误 :

FileNotFoundError:[Errno 2] 没有这样的文件或目录:\'_\'

你有什么解决方案来解决这个问题吗?

    标签: python streamlit


    【解决方案1】:

    您可以使用try:except: 来处理该问题,然后在exception E,g 之后写入错误消息:st.error("No file found")st.text("No file found")

    import streamlit as st
    
    tab1, tab2, tab3 = st.tabs(["Cat", "Dog", "Owl"])
    
    with tab1:
        st.header("A cat")
        try:
            st.image("_", width=200) # I changed here on purpose to raise an error
        except FileNotFoundError:
            st.text("File not found")
    
    with tab2:
        st.header("A dog")
        try:
            st.image("https://static.streamlit.io/examples/dog.jpg", width=200)
        except FileNotFoundError:
            st.text("File not found")
    
    with tab3:
        st.header("An owl")
        try:
            st.image("https://static.streamlit.io/examples/owl.jpg", width=200)
        except FileNotFoundError:
            st.text("File not found")
    

    您可以对提供单独操作的函数执行相同操作,因为您说您的第一个 tab1 拥有大量数据

    import streamlit as st
    
    tab1, tab2, tab3 = st.tabs(["Cat", "Dog", "Owl"])
    
    def compute_tab1():
        st.header("A cat")
        try:
            st.image("_", width=200) # I changed here on purpose to raise an error
        except FileNotFoundError:
            st.text("File not found")
    
    
    def compute_tab2():
        st.header("A dog")
        try:
            st.image("https://static.streamlit.io/examples/dog.jpg", width=200)
        except FileNotFoundError:
            st.text("File not found")
    
    def compute_tab3():
        st.header("An owl")
        try:
            st.image("https://static.streamlit.io/examples/owl.jpg", width=200)
        except FileNotFoundError:
            st.text("File not found")
    
    with tab1:
        compute_tab1()
    
    with tab2:
        compute_tab2()
    
    with tab3:
        compute_tab3()
    

    【讨论】:

    • 想象一下,如果 tab1 有大量的指令要执行,我们如何告诉 Streamlit 绕过 tab1 并执行所选选项卡的指令(例如 tab2)?我尝试使用if elif 语句,但它不起作用。
    • 像什么巨大的指示?..这还不清楚。您可以为此提供一个演示。
    • 实际上我有三个选项卡,第一个大约需要 1 分钟才能显示结果(因为它使用 pandas 处理一些大数据帧),所以这意味着除非完全执行 tab1 的指令,否则我无法切换到 tab2。这意味着我必须等待一分钟才能切换到 tab2 或 tab3。我希望这次我足够清楚
    • 在这种情况下,您可以@cache 来加快您的数据帧过程。如果您愿意,我可以在答案中包含带有cache 的简单函数,以便您了解情况。
    • 即使使用@st.cache,这也会给用户带来糟糕的体验,因为应用程序的第一次运行将是缓存未命中,这意味着用户必须等待一分钟才能切换选项卡。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 2018-03-02
    • 1970-01-01
    相关资源
    最近更新 更多