Zurichko_101101
Zurichko_101101

Reputation: 1

How to make N checkboxes in streamlit?

I want to make a remainder app, kind that user can infinity many times make checkboxes. for example:

  1. do hw
  2. read this article ...

it is basically a to do list.

'''

import streamlit as st
hw = []
todo =st.text_input("Input To do list elements here")


while todo != "done":
    hw.append(todo)
    todo = st.text_input("Input To do list elements here")

for i in hw:
    checkbox=st.checkbox(i)

'''

this is the code I am trying to use, I know this won't get me there, but I am for now just want to be able to make checkboxes and check them in streamlit but cannot solve the error message which says that I cannot use same key for multiple st.text_input or other error of running loop infinity many times even tho I input the break statement "done".

Maybe there is a different solution, I want to hear it all.

Upvotes: 0

Views: 4292

Answers (2)

ferdy
ferdy

Reputation: 5024

You have 2 st.text_input(), you need to define a key, example

todo =st.text_input("Input To do list elements here", key=1)

keys should be different.

Here is a simple todo app notice the checkbox keys.

Code

"""
Todo list app
"""

import streamlit as st


st.title("Todo list app")

# 1. Create a variable to store todos.
if not 'todolist' in st.session_state:
    st.session_state.todolist = []

# 2. Prompt the user in the form
with st.form(key='form'):
    todo = st.text_input(label='Enter todo description')
    is_submit = st.form_submit_button('submit')

# 3. Store todo in todolist when submit button is hit.
if is_submit:
    st.session_state.todolist.append(todo)
    
# 4. Display the contents of todolist
with st.expander(label='List of todos', expanded=True):
    for i, todo_text in enumerate(st.session_state.todolist):
        st.checkbox(label=f'{todo_text}', key=i)

Output

enter image description here

Upvotes: 2

Code-Apprentice
Code-Apprentice

Reputation: 83527

It looks like you think that st.text_input("Input To do list elements here") will ask for input immediately from the user. However, that's not how it works. Instead text_input() creates a text widget that you can place inside of a web page for a user to type into. The actual input will not be returned immediately.

I suggest that you read Main Concepts from the streamlit documentation to help you understand the fundamentals.

Upvotes: 0

Related Questions