im new to streamlit and to programming in general.
i wanted to use a date_input with the objective of colecting an user age, and if it is less than 18 years old it gives an error, the problem is that the value of the date_input doesnt change from its initial value, can anyone help me?
birthDate = st.date_input(“Birthday”, format=“DD/MM/YYYY”,value=None, max_value=date.today(), min_value=date(1900, 1, 1))
st.write(birthDate)
if birthDate != None:
if check_age(birthDate):
st.write(“Please note that this will be checked further”)
else:
st.error(“You must be 18 or older to sign in(Please note that this will be checked further)”)
Hello Tom!
I think you didn’t import datetime properly; it should be: from datetime import date. I recreated your code:
import streamlit as st
from datetime import date
def check_age(birth_date: date):
today = date.today()
age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day))
return age>=18
birthDate = st.date_input("Birthday",
format="DD/MM/YYYY",
value=None,
max_value=date.today(),
min_value=date(1900, 1, 1))
st.write(birthDate)
if birthDate != None:
if check_age(birthDate):
st.write("Please note that this will be checked further")
else:
st.error("You must be 18 or older to sign in(Please note that this will be checked further)")
Hello Carlos, thanks for your help, i have rewrited my code with what you said and the problem is not fixed.
As you saw in the script, the initial value of “birthDate” is None, and after the users inputs a date, it continues to be None, or at least the st.write(birthDate) continues to show None.
If it helps, im using visual studio code.
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.