I am new to this - please bear with me if I have gravely missed something.
One feature I would love to see is multiple tabs so the app can host multiple windows. Is it available? I looked through the online documents as well as the community site, but could not find it.
Thereās another potential hack while weāre waiting for Streamlit-native tabs if you donāt need the power of Bokeh. You can make use of query args and Bootstrap tabs rendered via st.markdown(). Hereās a little demo inside the issue requesting tabs in streamlit: Feature request: Tabs Ā· Issue #233 Ā· streamlit/streamlit Ā· GitHub
thanks for share this code , i use it in my app.
I have a question,when i want to use each page in separate python file and then use all of this file in one file as main.py.
But i canāt do this in this way, can you help me more about this?
Yeah, you can handle this with standard module importing logic. Hereās an example modified from my original example:
import streamlit as st
from pages import home, about, contact
st.markdown(
'<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">',
unsafe_allow_html=True,
)
query_params = st.experimental_get_query_params()
tabs = ["Home", "About", "Contact"]
if "tab" in query_params:
active_tab = query_params["tab"][0]
else:
active_tab = "Home"
if active_tab not in tabs:
st.experimental_set_query_params(tab="Home")
active_tab = "Home"
li_items = "".join(
f"""
<li class="nav-item">
<a class="nav-link{' active' if t==active_tab else ''}" href="/?tab={t}">{t}</a>
</li>
"""
for t in tabs
)
tabs_html = f"""
<ul class="nav nav-tabs">
{li_items}
</ul>
"""
st.markdown(tabs_html, unsafe_allow_html=True)
st.markdown("<br>", unsafe_allow_html=True)
if active_tab == "Home":
home.write()
elif active_tab == "About":
about.write()
elif active_tab == "Contact":
contact.write()
else:
st.error("Something has gone terribly wrong.")
For this to work, youād need to make a āpagesā folder with a home.py, about.py, and contact.py file, and each of those files should have a āwriteā function that does that handles logic for that page. Hope that helps!
@benlindsay
Thanks for your solution, it is a new break.
Can we use the css file locally , change the background color when tab is hover or clicked?
It seems it not work with other bootstrap version like newest 5.0.2
You can include a local css file in a hacky way as shown in this post. I havenāt tried tinkering with CSS other than including bootstrap, so Iām not sure what the limitations are or what will clash with streamlitās built-in CSS, but go ahead and tinker away!
Hi @benlindsay - love this hack to get tabs working. Iāve been using this in my app and it works fine when hosted locally however doesnāt work when the app is deployed. Encountering 404 page not found. Do you have a workaround for this?
Just took a look at it and the way streamlit sharing urls work, going to a link with an href of ā/ā doesnāt take you to the current page like it does when running streamlit locally. Youāll need to do something like
Hey, I would like to contribute to that discussion about tabs. While a completely understand all your dev reasons to not implement tabs right now, I and other community members, probably think itās a great feature to have, mostly to create more organized apps for final users.
I have tried that approach using experimental_get_query_parameters, but my session cache was invalidated on each tab interaction (aka, losing previously checked options).
So I looked for a more simple approach, and voilĆ”, tabs using a radio component as a base for interactions.
UPDATE: I just realize that approach will āoverrideā any radio component.
I looked at some CSS solutions for that but, since we donāt have any reference to that component on HTML code, I couldnāt find a way to isolate it.
Hey dev almighties, would be great to have ākey=ānameāā at some point in the HTML code of the component.
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.