Introduction
In this guide you will build an interactive Age Calculator web app using Python and Streamlit. The app calculates your exact age in years, months, and days — and even shows fun insights like the total number of days you have been alive.
Features
Prerequisites
Make sure you have the following installed before you begin:
pip install streamlitStep 1: Create the Python Script
Create a new file called age_calculator_app.py and paste in the following code:
import streamlit as st
from datetime import datetime, date
st.set_page_config(page_title="Age Calculator", layout="centered", page_icon="🎂")
st.title("🎉 Age Calculator App 🎂")
st.sidebar.header("📅 Enter Your Birthdate")
dob = st.sidebar.date_input(
"Select your date of birth:",
value=datetime(2000, 1, 1),
min_value=datetime(1900, 1, 1),
max_value=datetime.now(),
)
def calculate_age(birthdate):
today = date.today()
years = today.year - birthdate.year
months = today.month - birthdate.month
days = today.day - birthdate.day
if days < 0:
months -= 1
days += 30
if months < 0:
years -= 1
months += 12
total_days = (today - birthdate).days
return years, months, days, total_days
years, months, days, total_days = calculate_age(dob)
col1, col2, col3 = st.columns(3)
col1.metric("Years", years)
col2.metric("Months", months)
col3.metric("Days", days)
st.info(f"You have been alive for **{total_days:,} days**! 🎉")Step 2: Run the App
Open your terminal, navigate to the folder where you saved the file, and run:
streamlit run age_calculator_app.pyStep 3: Use the App
The app will open in your browser automatically. Select your date of birth from the sidebar and the app instantly displays your age in years, months, and days, plus a fun total-days count.
Key Concepts
What to Try Next
st.download_button().