Split st.radio in columns


i have a list of 30 stock tickers and want to split them into 3 columns and use them in radio-buttons in
the sidebar.

Is it possible to select only one radio-button in one of the three columns?


Thank you

Hi Mick,

I’ve tried to get this working but unfortunately I don’t see a way to split the same st.radio widget into three columns.
As an alternative where people can still see all the options you could use a selectbox instead to let people pick one option.

A hacky way of getting this to “work” (which I wouldn’t recommend) would be to update every selection in any of the radiobutton widgets in a session state variable and use that as the current picked option.
But visually you would still see the other radio buttons selected which makes it confusing for the user.

I’ll try to see if I can make a working example later today but that’s the only sort of solution to the problem I can come up with. Hope someone else has a better solution for you :slight_smile:

Hi again Mick,

I’ve tried to use an on_change callback but I couldn’t get it to pass along the “new choice” to update the session state’s current choice in the callback method so I made a workaround that’s not very elegant but it works:

import streamlit as st

col1_options = ["APPS", "BBBY", "CHPT", "CNC", "COUP", "DAL", "DDD", "FL", "FSLY", "GME"]
col2_options = ["GPS", "GRWG", "KSS", "LVS", "M", "MARA", "NVAX", "OKTA", "PENN"]
col3_options = ["PLUG", "RNG", "SHAK", "STEM", "STX", "UAL", "URBN", "YY", "ZS"]

if "current" not in st.session_state:
    st.session_state.current = col1_options[0]

if "col1_old" and "col2_old" and "col3_old" not in st.session_state:
    st.session_state.col1_old = col1_options[0]
    st.session_state.col2_old = col2_options[0]
    st.session_state.col3_old = col3_options[0]

col1, col2, col3 = st.columns(3)

col1_choice = col1.radio("", col1_options)
col2_choice = col2.radio("", col2_options)
col3_choice = col3.radio("", col3_options)

if col1_choice != st.session_state.col1_old:
    st.session_state.current = col1_choice
    st.session_state.col1_old = col1_choice

if col2_choice != st.session_state.col2_old:
    st.session_state.current = col2_choice
    st.session_state.col2_old = col2_choice

if col3_choice != st.session_state.col3_old:
    st.session_state.current = col3_choice
    st.session_state.col3_old = col3_choice

if st.session_state.current != None:
    st.write("You've picked: ", st.session_state.current)

Hope it helps somewhat! :smiley:

Thank you, i will try it and let you know the result.

1 Like

It doesnt work.

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
File "/home/user/stocks/streamlit/streamlit.py", line 200, in <module>
    if col1_choice != st.session_state.col1_old: