アルサ
アルサ

Reputation: 53

How to stop streamlit to reseting after using .radio?

I have this code(A sample of a larger one).

import streamlit as st
from PIL import Image
import datetime as dt
from streamlit_option_menu import option_menu

img_16 = Image.open("PATH/81.png")
with st.container():
    st.write("---")
    left_column, middle_column, right_column = st.columns(3)

    with left_column:
        st.subheader(16) 
        st.image(img_16)       
        if st.button("Click me ⤵️",key=16):
            st.write("""
            Team:    ABC\n
            """ )
            st.write("---")

            condition_now = st.radio(label = "Have a live 🎤",options = ["ichi", "ni", "san"])
            st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True)
            if condition_now == "ichi":
                st.write("ichi da!")
            elif condition_now == "ni":
                st.write("ni da!")
            else:
                st.write("san?")

After clicking the "click me" button I want to choose one one the radio buttons, yet when I click on any radio button all will dissapear or get rest. How can I stop resetting?

The st.session_stat could be the key, but couldn't figure how.

Upvotes: 0

Views: 818

Answers (1)

NorthPole
NorthPole

Reputation: 37

adding st.button() to form is not supported, and st.button() has ephemeral True states which are only valid for a single run. Here you need to capture and save status of both button and Radio. Here is the code I tired and its not resetting radios

import streamlit as st
from PIL import Image
if 'bt_clkd' not in st.session_state:
    st.session_state.bt_clkd = ''
if 'rd_clkd' not in st.session_state:
    st.session_state.rd_clkd='film'

img_16 = Image.open("sampleImage.png")
with st.container():
    st.write("---")
    left_column, middle_column, right_column = st.columns([3,2,2],gap='small')

    with left_column:
        st.subheader(16)
        st.image(img_16)
        # n = st.session_state.bt
        b = st.button("Click me ⤵️")
# b = st.button('ckick me',key='a')

        if (b) or (st.session_state.bt_clkd== 'y'):
            rd = st.radio('select choice',options=['film','surfing','reading'],key='rdkey',index=0,horizontal=True)
            st.session_state.bt_clkd = 'y'
            if (st.session_state.bt_clkd=='y') and (rd =='film'):
                st.session_state.rd_clkd = 'film'
                st.write('film!!')
            elif (st.session_state.bt_clkd=='y') and (rd=='surfing'):
                st.session_state.rd_clkd = 'surfing'
                st.write('surfing!!')
            else:
                st.session_state.rd_clkd = 'reading'
                st.write('reading!')

Upvotes: 0

Related Questions