0

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.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I suggest reading the [getting started guide](https://docs.streamlit.io/library/get-started) from the streamlit documentation. I don't think you are using `text_input()` correctly at all and this should explain it. – Code-Apprentice Feb 23 '22 at 18:51

2 Answers2

2

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

ferdy
  • 4,396
  • 2
  • 4
  • 16
0

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268