Create a variable number of tabs based on dataframe values

In general, tabs have a formulation like the following

    tab1, tab2 = st.tabs(["title1", "title2"])
    with tab1:
        some_output()
    with tab2:
        some_output()

However, what I’d like to do is create a dynamic number of tabs with labels that are based on the unique values for a given dataframe column. Let’s suppose I have a dataframe that looks something like the following:

var value
var1 234
var1 72
var2 9832
var2 972
var3 821
var3 082

What I’d like to do here is create a tab for each unique value of var, which in this case would be 3. So in the end it would behave as though I had coded

tab1, tab2, tab3 = st.tabs(["var1", "var2","var3"])
    with tab1:
        some_output()
    with tab2:
        some_output()
    with tab3:
        some_output()

In other situations, there may be more unique values for var, in which case it would be ncessary to have additjonal tabs.

I’m curious if anyone has solved a similar problem; or if there is some other way to go about this that I am missing.

Is this what you want?

def some_output():
    st.write("The output goes here.")

df = pd.DataFrame(
    {
        "var": ["var1", "var1", "var2", "var2", "var3", "var3"],
        "value": [234, 72, 9832, 972, 821, 82],
    }
)

tab_labels = df["var"].unique().tolist()
for tab in st.tabs(tab_labels):
    with tab:
        some_output()

this is working great for me!

curious how i would pass “var1” to the first tab, “var2” to the second tab… etc

Just loop over (label, tab) pairs.

def some_output(label):
    st.write(f"Write **{label}** here.")

df = pd.DataFrame(
    {
        "var": ["var1", "var1", "var2", "var2", "var3", "var3"],
        "value": [234, 72, 9832, 972, 821, 82],
    }
)

tab_labels = df["var"].unique().tolist()
tabs = st.tabs(tab_labels)
for label, tab in zip(tab_labels, tabs):
    with tab:
        some_output(label)