Toggle 2 buttons in Streamlit

A Streamlit page with a button “Show summary”, if user clicked this, will show the summary chart and change the button text to “Show Trend”. If user clicked on it, will show a trend graph and change the button text to “Show summary”.
The progress then keep repeating.

How to achieve this by code ?

Hi @aiya6502, is this what you are looking for?

import streamlit as st

btnlst = ["Show Summary", "Show Trend"]
if "btnptr" not in st.session_state:
    st.session_state.btnptr = 0

def btnCB():
    if st.session_state.btnptr == 0:
        st.session_state.btnptr = 1
        st.write("Here is where I show my trend...")
    else:
        st.session_state.btnptr = 0
        st.write("Here is where I show my summary...")

st.button(btnlst[st.session_state.btnptr], on_click=btnCB)

Cheers

2 Likes

Yes, it works perfectly

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