简体   繁体   English

如何在 streamlit 上创建独立工作的选项卡

[英]How to create tabs on streamlit that work independently

I'm trying to create tabs that works independently on Streamlit.我正在尝试创建在 Streamlit 上独立工作的选项卡。

The official documentation explains here how to create three tabs for eg.官方文档 在这里解释了如何为例如创建三个选项卡。 and it works fine.它工作正常。 But when I change the code of (let's say the Cat's tab) so can an error be raised, I can't anymore switch to Dog's tab or Owl's tab.但是,当我更改(假设是猫的标签)的代码以便引发错误时,我不能再切换到狗的标签或猫头鹰的标签。

CODE:代码:

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)

ERROR:错误:

FileNotFoundError: [Errno 2] No such file or directory: '_' FileNotFoundError:[Errno 2] 没有这样的文件或目录:'_'

Do you have any solution to fix that?你有什么解决方案来解决这个问题吗?

You can handle that by using try: and except: then write error message after the exception E,g: st.error("No file found") or st.text("No file found")您可以通过使用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")

You can do same with functions that proves individual operations, since you said your first tab1 holds large data您可以对证明单个操作的函数执行相同操作,因为您说您的第一个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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM