Hey guys, is it possible to create a multiple choice grid in Streamlit, you know like the one in Google Forms?
Kinda like this:
Hi @utkrshm
I have implemented this with checkboxes instead of radio buttons (as it looks good and is easier). You can’t put this code into a form as there is a callback.
import streamlit as st
mystate = st.session_state
if "parameters" not in mystate:
mystate.parameters = {"Parameter A": "", "Parameter B": "", "Parameter C": "", }
choices = ("Very Good", "Good", "Fair", "Bad", "Very Bad")
cols = st.columns((2,1,1,1,1,1))
for i in range(len(choices)): # write choices hdr
cols[i+1].markdown(choices[i].replace(' ', '<br>'), True)
def RCB(vkey):
vkey_s = vkey.split("|")
vparameter, vchoice = vkey_s[0], vkey_s[1]
mystate.parameters[vparameter] = vchoice
for i in range(len(choices)):
if choices[i] != vchoice:
st.session_state[f"{vparameter}|{choices[i]}"] = False
st.write(mystate.parameters)
for vpkey in mystate.parameters.keys():
cols = st.columns((2,1,1,1,1,1))
cols[0].write(vpkey)
stcol = 0
for j in range(len(choices)):
stcol += 1
vkey = f"{vpkey}|{choices[j]}"
cols[stcol].checkbox("", value=False, key=vkey, on_change=RCB, args=(vkey,), label_visibility="collapsed")
if st.button("Submit"):
st.info("Final Choices")
st.write(mystate.parameters)
Please test this code separately before inserting it into your code / modifying it for your use. I am using the latest version of Streamlit.
Cheers
Hey @Shawn_Pereira, this works perfectly.
Tho, would you mind telling me how you went about building this? Would really like to know.
Hi @utkrshm
Well, basically, I wrote:
a. your rows in a loop
b. used a checkbox widget that has a callback function
c. within the callback, I reset all the other checkboxes using the checkbox keys
You’ll need to be familiar with all the streamlit commands / functions used in the code and also know about session state.
You also have the possibility of jazzing up the output by using JS.
Go through the topics on the forum as well as the streamlit docs.
Cheers
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.