Use st.tabs labels in f-strings

Hi! I was wondering if it was possible to get the labels I give to my tabs into f-strings. By label, I mean the name of each tab.

Let’s say I have two tabs: t1 and t2.

Code snippet:

import streamlit as st

t1, t2 = st.tabs(["tab1", "tab2"])

# This line should give me the label of the tab, where 'x' is the label
t1.write(f"This tab is {x}") 

I’ve tried using x = t1.label . However, I got this: <function DeltaGenerator.getattr.<locals>.wrapper at 0x00000206DE79A660>

The desired output should be:

This tab is tab1

I thank you for any help you can give me!

Hey @cgarc14,

Thanks for sharing this question.

Is the idea to display/access the name of the tab that the user is currently viewing?

Hello @Caroline ,

Exactly!

For example, if the user is viewing a tab named ā€œUser Informationā€, I would want to have a line of text that reads ā€œYou are currently viewing: User Informationā€. If the user then changes tabs to a second one named ā€œBilling Informationā€, that same line of text should then read ā€œYou are currently viewing: Billing Informationā€

Hi there @cgarc14 ,

See the snippet below. Also an additional tip: in some cases it is useful to unpack tabs (and columns) into separate variables (as your snippet does with t1 and t2), in others it is best to keep them as a single tabs iterable.

import streamlit as st

tab_titles = ['tab title 1','tab title 2']

tabs = st.tabs(tabs=tab_titles)

for i,tab in enumerate(tabs):
    tab.header(f'You are currently viewing: {tab_titles[i]}')
2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.