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”?
Hi @Tomaz_Bratanic, you could probably try this:
t = ["Foo", "Bar"]
tabs = st.tabs(t)
for tabname in t:
st.write(tabname)
if tabname == 'Foo':
...do something
Cheers
1 Like
Hi @Tomaz_Bratanic .
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)
1 Like