How change state in st.tabs() like st.button and st.selectbox()?

Hi, I am currently using Streamlit for a simple dashboard application. I want to display a temperature and speed value (changes continuously using random function). I have 2 tabs created using st.tabs() using st.empty() I’m refreshing the values it running fine. but the issue I’m facing when the value changes continuously in tab1 using st.metrics and when i click tab2 I’m unable to view tab2, I know it is because halted in tab1 while. if it is a radiobutton or selectbox no problem. for tab what I should do to both tabs with while running separately.
following is the code.

def tab_1():
    Vstatusholder = st.empty()
    while True:
        temp_val = random.randint(25,40)
        speed_val = random.randint(2500,3000)
       
        with Vstatusholder.container():
            c1, c2 = st.columns(2)
            c1.metric("Temperature", temp_val, "%")
            c2.metric("Speed", speed_val, "RPM")
            t.sleep(2)
            Vstatusholder.empty()

def tab_2():
    Vholder = st.empty()
    while True:
        temp_val = random.randint(25,40)
        speed_val = random.randint(2500,3000)
       
        with Vstatusholder.container():
            c1, c2 = st.columns(2)
            c1.metric("Temperature", temp_val, "%")
            c2.metric("Speed", speed_val, "RPM")
            t.sleep(3)
            Vholder.empty()

def main():
     
    tab1, tab2 = st.tabs(["tab1", "tab2"])
  
    with tab1:
        tab_1()
    
    with tab2:
        tab_2()
    
    
    
if __name__ == "__main__":
    st.set_page_config(page_title="Dashboard", layout="wide")
    
    main()

Python version 3.10
streamlit latest version

As you already found out, the behavior of selectboxes and radio buttons fits your use case much better than tabs, because they allow you to detect state changes and make choices based on that.

There must be some third party component that looks like tabs and behaves like radio buttons, if you really want that kind of UI that is probably your best option. But be aware that third party components often become unmaintained and you can get stuck with old versions if you depend on them.

Thank you for your reponse.

This is for temporary only, what is the third party you mentioned can you share the details or link.

There is a TabBar in Extra-Streamlit-Components.

Thank you @Goyo

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