Hijri_Calendar / app.py
Dearsawan's picture
Update app.py
c666ae5 verified
import streamlit as st
import calendar
from hijri_converter import Hijri, Gregorian
from datetime import datetime
import pandas as pd
# Hijri Month Names
hijri_months = [
"Muharram", "Safar", "Rabi' al-Awwal", "Rabi' al-Thani", "Jumada al-Awwal",
"Jumada al-Thani", "Rajab", "Sha'ban", "Ramadan", "Shawwal", "Dhul-Qi'dah", "Dhul-Hijjah"
]
# Helper function to get the Hijri month for a given Gregorian month and year
def get_hijri_month(gregorian_year, gregorian_month):
# Get the first day of the month in Gregorian
first_day_of_month = Gregorian(gregorian_year, gregorian_month, 1).to_hijri()
# Get the number of days in the Gregorian month
_, num_days = calendar.monthrange(gregorian_year, gregorian_month)
# Generate the Hijri days for this month
hijri_days = []
for day in range(1, num_days + 1):
gregorian_date = Gregorian(gregorian_year, gregorian_month, day)
hijri_day = gregorian_date.to_hijri()
hijri_days.append((day, hijri_day.month, hijri_day.day))
return hijri_months[first_day_of_month.month - 1], hijri_days
# Main Streamlit App
st.title("Hijri Calendar")
# Get the current date in Gregorian
current_date = datetime.today()
gregorian_year = current_date.year
# Sidebar for year navigation
st.sidebar.header("Navigate Calendar")
year = st.sidebar.slider("Select Year", 1900, 2100, gregorian_year)
# Days of the week
days_of_week = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
# Initialize the layout for displaying 12 months
for i in range(12):
hijri_month, hijri_days = get_hijri_month(year, i + 1)
# Get Gregorian month name
gregorian_month_name = calendar.month_name[i + 1]
# Display both Gregorian and Hijri month names
st.subheader(f"{gregorian_month_name} - {hijri_month}")
# Create an empty list for the table data
table_data = []
# Create a row to represent days of the week header
table_data.append([f"{day}" for day in days_of_week])
# Create an empty row for the calendar days
row = [None] * 7 # A row has 7 days (Sunday to Saturday)
# Fill the table with the Hijri days
for j, (gregorian_day, hijri_month, hijri_day) in enumerate(hijri_days):
week_day = (gregorian_day + 6) % 7 # Find the weekday (0 = Sunday, 6 = Saturday)
# Place the Gregorian and Hijri day in the correct position
row[week_day] = f"{gregorian_day} ({hijri_day})"
# If the row is full (7 days), add it to the table and reset the row
if week_day == 6:
table_data.append(row)
row = [None] * 7 # Reset for the next row
# If there are remaining days in the last row, append them
if any(day is not None for day in row):
table_data.append(row)
# Create a DataFrame from the table data
df = pd.DataFrame(table_data)
# Display the table with custom styles
st.markdown(f"### {hijri_month}")
st.dataframe(df.style.set_properties(
subset=[0, 1, 2, 3, 4, 5, 6],
**{'width': '80px', 'text-align': 'center'}
).applymap(lambda x: 'background-color: #D1E8E2' if x is not None else '', subset=pd.IndexSlice[1:, :])) # Color alternating rows
# Add a separator between months
st.write("---")