I want to be able to dynamically define the number of tabs. That is all quite easy.
t = ["Foo", "Bar"]
tabs = st.tabs(t)
However, now I want to iterate over created tabs and do some stuff depending on the id or caption of the tab:
for tab in tabs:
if tab == "Foo"
However, I haven’t found it possible to compare the tab object with the caption name, tried all sorts of things but didn’t make it. Which attribute of the tab object I need to use to be able to compare it to “Foo”?
You can use this approach of having a function map dictionary to control your layout and functions.
import streamlit as st
def render_foo(id):
with tabs[id]:
st.subheader("This is foo")
def render_bar(id):
with tabs[id]:
st.subheader("This is bar")
fn_map = {"Foo": render_foo, "Bar": render_bar}
tabs = st.tabs([i for i in fn_map.keys()])
for idx, i in enumerate(fn_map):
fn = fn_map.get(i)
fn(idx)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.