I seem to have trouble accessing the variable defined under a different st.radio
. Please see reproduced code to explain better:
def add(a, b):
return a + b
def multiply(c, d):
return c * d
def add_and_multiply():
added_num = add(a, b)
multiplied_num = multiply(c, d)
return added_num + multiplied_num
col1, col2 = st.columns([1, 1])
one = 'one'
two = 'two'
with col1:
options = st.radio('options:', options = [one, two])
if options == one:
with col2:
a = st.number_input('input a')
b = st.number_input('input b')
elif options == two:
with col2:
c = st.number_input('input c')
d = st.number_input('input d')
func = add_and_multiply()
I get NameError: name 'a', 'b' are not defined
when I go to st.radio
two
. I tried to apply st.session_state
but was unsuccessful. See below my attempt:
def add(a, b):
return a + b
def multiply(c, d):
return c * d
def add_and_multiply():
a_var = st.session_state(a)
b_var = st.session_state(b)
added_num = add(a_var, b_var)
multiplied_num = multiply(c, d)
return added_num + multiplied_num
col1, col2 = st.columns([1, 1])
one = 'one'
two = 'two'
with col1:
options = st.radio('options:', options = [one, two])
if options == one:
with col2:
a = st.number_input('input a')
b = st.number_input('input b')
st.session_state['a'] = a
st.session_state['b'] = b
elif options == two:
with col2:
c = st.number_input('input c')
d = st.number_input('input d')
func = add_and_multiply()