Obtain data using st.form and st.checkbox

In a st.form, I have a list of st.checkbox and one submit button. These checkboxes are associated with certain movie ids and are displayed on the page using a for loop.

After the user presses the ‘submit’ button is there a way to obtain all the movie ids the user selected? I thought about using an array and appending the entries but have no idea how to retrieve each movie id. Also is there a way to prevent the page from refreshing when the submit button is clicked as I don’t want new movie titles to appear as a result of refreshing.
Thanks!

Hi @yu1929, please read about session state in the streamlit docs - that will enable you to implement this.

Cheers

Hi Shawn,
I read about session state but I’m having trouble implementing this in the presence of other factors such as st.form and nested loops.

My code looks a little like this:

movies_list = movies[‘movies’][‘items’]
if len(movies_list) > 0:
with st.form(f"{movie[‘id’]}", clear_on_submit=False, border = False):
submitted = st.form_submit_button(“Add Selected Movies”)

      for movie in recommendations:
        cols = st.columns(2)
        cols[0].image(movie['images'][0]['url'])
        cols[1].write(movie['name'])
        cols[1].write(f"**Director:** " + movie['director'][0]['name'])

        save_key =  f"save_key{movie['id']}"          
        save_btn = cols[1].checkbox("Select Movie", key={movie['id']})

Some guidance and pointers on how to do this would be greatly appreciated, thanks.

Hey @yu1929 , according to my understanding, you don’t want to refresh the page when the button is pressed right. Correct if I’m wrong? If it is the case just maintain a session variable and update it for preventing refresh. For example:-

bt1=st.button('Submit')
if st.session_state.get('button') != True:
            st.session_state['button'] = bt1
if st.session_state['button'] == True:
        
            if rno not in keys_length:
                st.warning('🚨 Roll Number Incorrect! 😱')
            else:
                conn=sqlite3.connect('vitap.db')
                query='select name from vitap2 where roll_number = ?;'
                old_name=[i[0] for i in conn.execute(query,(rno,))]
                conn.commit()
                conn.close()
                st.success('🔑 Roll Number Correct! 🎉')
                new_name=st.text_input('Enter new name')
                if st.button('Update'):
                    st.session_state['button'] = False
                    st.success(updated_new_name_funcion(new_name,rno))
                    st.balloons()
                    st.write('Old Name: {}'.format(old_name[0]))
                    st.write('New Name: {}'.format(new_name))