Access variables from different st.radio in the same page

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()

Hi @serdar_bay!

  1. Firstly you need to initze session states
  2. Secondly - you should add arguments to add_and_multiply function

After above changes it seems to work as expected? :slight_smile:

Working example:

import streamlit as st

# initize session state
if "a" not in st.session_state:
    st.session_state["a"] = 0
if "b" not in st.session_state:
    st.session_state["b"] = 0
if "c" not in st.session_state:
    st.session_state["c"] = 0
if "d" not in st.session_state:
    st.session_state["d"] = 0

def add(a, b):
    return a + b

def multiply(c, d):
    return c * d

# added arguments to the function
def add_and_multiply(a, b, c, d):
    a_var = a
    b_var = 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")
            st.session_state["c"] = c
            st.session_state["d"] = d

func = add_and_multiply(
    st.session_state["a"],
    st.session_state["b"],
    st.session_state["c"],
    st.session_state["d"],
)
st.write(func)
1 Like

thank you @TomJohn!

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