I am creating a todo web app, and while testing it I notice when I hit enter when there was text in the input box the text remained there!
Steps to reproduce
Code snippet:
import streamlit as st
import function
todos = function.get_todos()
def add_todo():
todo = st.session_state["new_todo"] + '\n'
todos.append(todo)
function.write_todos(todos)
st.title("My Todo App")
st.subheader('This is my todo app.')
st.write("The app is to increase your productivity")
for index, todo in enumerate(todos):
checkbox = st.checkbox(todo, key=todo)
if checkbox:
todos.pop(index)
function.write_todos(todos)
del st.session_state[todo]
st.experimental_rerun()
st.text_input(label=' ', placeholder='Add new todo...',
on_change=add_todo, key='new_todo')
* Link to your deployed app: https://nishanth-pallela-my-todo-app-todowebapp-7h260z.streamlit.app/
Visit the link above to get to my app.
Please help me!!
def add_todo():
todo = st.session_state[ânew_todoâ] + â\nâ
todos.append(todo)
function.write_todos(todos)
# Clear the input box after hitting enter
st.session_state["new_todo"] = ""
##This code works by first storing the value of the input box in the session state. Then, after the user hits enter, the session state is cleared. This will cause the input box to be empty the next time the user clicks on it.
A slight change to @mathcatsandâs solution where using the get() method, thereâs no need to initialize a key in the session state beforehand. Also, see this post on Stack Overflow.
A working solution:
import streamlit as st
def clear_text():
st.session_state.my_text = st.session_state.widget
st.session_state.widget = ""
st.text_input('Enter text here:', key='widget', on_change=clear_text)
my_text = st.session_state.get('my_text', '')
# ^^^ <--- here
st.write(my_text)
Itâs so very simpler than the most peopleâs approach, we didnât even need a seperate function for it! But anyway i have solved it myself, and in my case i have to store the value in database so i had to use custom function which i use to store the text in the text_inbox so i added this single line to my write_text function and thatâs it, simple as that. I donât understand why are people usually trying to make it look harder than it is. I am not saying that itâs (coding) easy or anything but itâs not impossible difficult either, it can be achieved.
I played around with this for a while and found these solutions finicky. They didnât do exactly what I wanted and required me to do a bunch of âstuffâ to manage state. The beautiful thing about streamlit is that you donât need to know about those types of things. Managing a bunch of text boxes as described above feels very off-brand for the awesomeness of streamlit.
I use the nav_to function that was shown here a while back. When I decide the page needs to be âreset,â I redirect to the current page. That way, I donât need to do ANY state management; I just got the page reloaded, which seems quite happy to give me a new clean form. The only pain point for this code is knowing if you are on the dev server or the production server since the URLs for these are different. In my actual nav_to function I have a way to make URLs know if they should be production or development.
def nav_to(url):
"""
Redirects the user to a new web page by generating a meta refresh tag.
This was recommended here:
https://discuss.streamlit.io/t/programmatically-send-user-to-a-web-page-with-no-click/21904/6
Parameters:
url (str): The URL of the destination web page.
Example:
nav_to('https://example.com') # Redirects to https://example.com
"""
nav_script = f"""<meta http-equiv="refresh" content="0; url='{url}'">"""
st.write(nav_script, unsafe_allow_html=True)
Then I write code that looks like this:
if pressed:
ST_Data.log_job_comment(note):
st.success('Comment Saved')
# Allow the user to see the status
time.sleep(1.0)
# Now jump to the same page to force a refresh
st_tool.nav_to("http://localhost:8501/PCSJobComment")
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking âAccept allâ, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.