Calendar using Python and Streamlit
Introduction:
The Calendar is a simple yet powerful web application built using Streamlit, a Python library for creating interactive web apps, to visualize and display a calendar for any specified year. With its intuitive interface, users can easily navigate through different months and years, making it an essential tool for organizing schedules and events.
Project Prerequisites:
Before diving into the Calendar project, ensure you have the following prerequisites set up on your system:
- Python: Make sure Python is installed on your system. You can download and install Python from the official website: Python Official Website
- Streamlit: Streamlit is a Python library used for building interactive web applications. You can install it via pip:
pip install streamlit
Getting Started:
Follow these steps to set up and run the Calendar project:
Step 1: Install Python and Streamlit
If you haven’t already, download and install Python from the official website: Python Official Website
Open your command line interface (CLI) or terminal.
Install Streamlit by running the following command:
pip install streamlit
Step 2: Create a New Python Script
Open your preferred text editor or Integrated Development Environment (IDE).
Create a new Python script file, e.g., calendar_app.py
.
Step 3: Write the Code
Copy and paste the following code into your Python script file:
import streamlit as st
import calendar
def main():
st.title("Calendar")
# Get user input for the year
year = st.number_input("Enter a year", min_value=0, max_value=9999, value=2022)
# Create a calendar for the specified year
cal = calendar.Calendar(calendar.SUNDAY)
# Create a list to hold all months' calendars
all_months = []
# Loop through each month of the year
for month in range(1, 13):
# Generate the month's calendar
month_cal = cal.monthdayscalendar(year, month)
month_name = calendar.month_name[month]
# Create a title for the month
month_title = f"### {month_name} {year}"
# Create a header row with day names
days_header = "|".join(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"])
separator = "|".join(["---"] * 7)
header_row = f"|{days_header}|\n|{separator}|"
# Format the month's calendar as a Markdown table
month_table = month_title + "\n" + header_row + "\n"
for week in month_cal:
week_str = "|".join(f"{day:2}" if day != 0 else " " for day in week)
month_table += f"|{week_str}|\n"
all_months.append(month_table)
# Display all months' calendars
st.markdown("\n\n".join(all_months), unsafe_allow_html=True)
if __name__ == "__main__":
main()
Step 4: Run the Application
Save the Python script file.
Open your CLI or terminal and navigate to the directory where your Python script is saved.
Run the following command to start the Streamlit application:
streamlit run calendar_app.py
Step 5: Explore the Calendar
Once the Streamlit application is running, you’ll see an input box where you can enter the year for which you want to display the calendar.
Enter the desired year and press Enter.
You’ll see the calendar displayed for the specified year, with each month presented in a tabular format.
You can navigate through different months and years using the provided input box.
That’s it! You’ve successfully set up and used the Calendar project. Feel free to customize the code or add additional features to enhance the application further.
Join the conversation