Checkboxes and remove options from a list

Hi,

I am creating an app where the user can store a list (eg : favorite authors or movies). The list would be displayed on the app. How can I make it easy for the user to remove items from the list using an β€œx” button or something like β€œ-” next to each item.

Also, I would like to create checkboxes next to them to be able to export them or remove multiple items at a time.

Hi @streamlitdev - Here’s a program which does what you want based on my understanding of your requirements:

import streamlit as st

state = st.session_state
if 'MOVIES' not in state:
    state.MOVIES = []

drama = ['drama1', 'drama2', 'drama3', 'drama4']
thriller = ['thriller1', 'thriller2', 'thriller3', 'thriller4']
horror = ['horror1', 'horror2', 'horror3', 'horror4']
comedy = ['comedy1', 'comedy2', 'comedy3', 'comedy4']
all_genres = drama + thriller + horror + comedy
movies_db = {'all': all_genres, 'drama': drama, 'thriller': thriller, 'horror': horror, 'comedy': comedy}

def _set_movies_cb():
    state.MOVIES = state.movies_choice

def _add_movies_bundle_cb(bundle):
    movies_to_add = movies_db[bundle]
    # state.MOVIES = list(set(state.MOVIES + movies_to_add))
    for movie in movies_to_add:
        if movie not in state.MOVIES:
            state.MOVIES.append(movie)
def _del_movies_bundle_cb(bundle):
    movies_to_del = movies_db[bundle]
    for movie in movies_to_del:
        if movie in state.MOVIES:
            state.MOVIES.pop(state.MOVIES.index(movie))

st.multiselect('Choose your movies', options=all_genres, default=state.MOVIES, on_change=_set_movies_cb, key='movies_choice')

c1, c2, c3, c4, c5 = st.columns([1,1,1,1,1])
c1.button('+all', on_click=_add_movies_bundle_cb, args=('all',))
c2.button('+drama', on_click=_add_movies_bundle_cb, args=('drama',))
c3.button('+thriller', on_click=_add_movies_bundle_cb, args=('thriller',))
c4.button('+horror', on_click=_add_movies_bundle_cb, args=('horror',))
c5.button('+comedy', on_click=_add_movies_bundle_cb, args=('comedy',))

c1, c2, c3, c4, c5 = st.columns([1,1,1,1,1])
c1.button('-all', on_click=_del_movies_bundle_cb, args=('all',))
c2.button('-drama', on_click=_del_movies_bundle_cb, args=('drama',))
c3.button('-thriller', on_click=_del_movies_bundle_cb, args=('thriller',))
c4.button('-horror', on_click=_del_movies_bundle_cb, args=('horror',))
c5.button('-comedy', on_click=_del_movies_bundle_cb, args=('comedy',))

st.download_button('Export List', data=str(state.MOVIES), file_name='movies.txt', mime='text/plain', disabled=(len(state.MOVIES) == 0))

st.write(state.MOVIES)

HTH,
Arvindra

Thank you so much!

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