How to Create Tabs on Streamlit That Work Independently

I'm trying to create tabs that works independently on 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("", width=200)

with tab3:
    st.header("An owl")
    st.image("", width=200)

ERROR :

FileNotFoundError: [Errno 2] No such file or directory: '_'

Do you have any solution to fix that ?

2 Answers

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")

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("", width=200)
    except FileNotFoundError:
        st.text("File not found")

with tab3:
    st.header("An owl")
    try:
        st.image("", width=200)
    except FileNotFoundError:
        st.text("File not found")

You can do same with functions that provide individual operations, since you said your first tab1 holds large data

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("", width=200)
    except FileNotFoundError:
        st.text("File not found")

def compute_tab3():
    st.header("An owl")
    try:
        st.image("", width=200)
    except FileNotFoundError:
        st.text("File not found")

with tab1:
    compute_tab1()

with tab2:
    compute_tab2()

with tab3:
    compute_tab3()
7

Native solution

You can't do it with tabs. However, you can achieve a similar practical effect with st.radio() component.

import streamlit as st

animal = st.radio("Your animal:", ["Cat", "Dog", "Owl"])

if animal == "Cat":
    ...

External links

  • Documentation

3rd party: Pills

However, from UI perspective, I'd recommend using a third-party library such as steramlit-pills:

pip install streamlit-pills

import streamlit as st
from streamlit_pills import pills

animal = pills("Your animal:", ["Cat", "Dog", "Owl"])

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

if animal == "Dog":
    st.header("A dog")
    st.image("", width=200)

if animal == "Owl":
    st.header("An owl")
    st.image("", width=200)

External links:

  • Demo
  • Documentation

3rd party: Antd

Another alternative would be Antd buttons

pip install streamlit-antd-components

import streamlit_antd_components as sac

animal = sac.buttons(['Cat', 'Dog', 'Owl'])

if animal == "Cat":
    ...

External links

  • Demo
  • Documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest