question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: Tabs

See original GitHub issue

Problem

Sorry if this has been discussed before (couldn’t find anything here on on the forum). While building my app, I thought it could be nice to have a tabbed UI component, e.g. to show different “views” of the data (visualisation, table view, JSON, more detailed docs etc.) If the user clicks a tab, that content is generated – otherwise it’s not.

Solution

API could maybe look something like this? Specifying the group name could be optional (default is the same group).

with streamlit.tab("Tab one", "tabgroup1"):
    st.write("This is tab content and only run if user selects the tab")
with streamlit.tab("Tab two", "tabgroup1"):
    st.write("This is tab content and only run if user selects the tab")

Additional context

Probably clear, but better to be explicit. By “tabs”, I mean something like this:

Screenshot 2019-10-02 at 13 05 22

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:150
  • Comments:16

github_iconTop GitHub Comments

16reactions
benlindsaycommented, Apr 17, 2021

So I finally got around to trying the markdown + bootstrap + query parameters hack that was in my head and it sorta works. You’ll lose all widget values because each tab link would refresh the whole page I think, so if you want to preserve variables you’d probably have to have all of them stored as a query parameter instead of just the tab values. Here’s code that works for me on streamlit==0.80.0 and a little demo on my old, slow, computer:

import streamlit as st

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":
    st.write("Welcome to my lovely page!")
    st.write("Feel free to play with this ephemeral slider!")
    st.slider(
        "Does this get preserved? You bet it doesn't!",
        min_value=0,
        max_value=100,
        value=50,
    )
elif active_tab == "About":
    st.write("This page was created as a hacky demo of tabs")
elif active_tab == "Contact":
    st.write("If you'd like to contact me, then please don't.")
else:
    st.error("Something has gone terribly wrong.")

tabs

A more streamlit-native approach to tabs that treats tab selection as just another widget value to remember would be much more ideal, but this is a potential interim solution, especially if you don’t need to preserve widget values between tabs.

8reactions
MarcSkovMadsencommented, Nov 1, 2019

And @ines

I really like the api you are proposing. I have requested something similar for the app and grid layout here.

https://github.com/streamlit/streamlit/issues/486#issuecomment-548666166

Read more comments on GitHub >

github_iconTop Results From Across the Web

Feature request: Tabs · Discussion #5109 · supabase ... - GitHub
Currently if I write a query on the SQL Editor and wanna see it directly in the table I can either navigate to...
Read more >
Feature requests??? - Ultimate Guitar
Is there a forum or other place to request new features for the app? I like to add notes to myself in the...
Read more >
[Feature Request] Split screen for Tabs
Hi! I would like to see the split screen function for tabs to be implemented in Edge, where you can view two tabs...
Read more >
Working with Tabs feature request - Soft8Soft
Tabs help organize things, but I'd like a few additions to the UI: 1 - Renaming Tabs 2 - Reordering Tabs 3 -...
Read more >
[Feature Request] More Flexible Slider & Tabs
[Feature Request] More Flexible Slider & Tabs ... so like Posts element is super flexible - ideally Sliders and Tabs could be the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found