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.