您可以使用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()