Spaces:
Sleeping
Sleeping
import pandas as pd | |
import streamlit as st | |
from datasets import load_dataset | |
from datetime import datetime | |
# Load data | |
# Cache data for performance improvement | |
def load_data(language="en"): | |
dataset = load_dataset("santhosh/day_in_history", language, split="train") | |
df = pd.DataFrame(dataset) | |
return df | |
# Function to process user input (date) and return description and reference | |
def process_date(day, month, year=None): | |
# Filter data based on selected date | |
if year is None or year == "": | |
filtered_data = df[(df["month"] == month) & (df["day"] == int(day))] | |
else: | |
filtered_data = df[ | |
(df["year"] == int(year)) & (df["month"] == month) & (df["day"] == int(day)) | |
] | |
if not filtered_data.empty: | |
# Prepare empty lists to store descriptions and references | |
descriptions = [] | |
references = [] | |
# Loop through filtered data and append descriptions and references | |
for index, row in filtered_data.iterrows(): | |
descriptions.append(row["event_description"]) | |
references.append(row["reference"]) | |
# Return lists of descriptions and references | |
return descriptions | |
else: | |
return [f"No data found for selected date {year} {month} {day}"] | |
supported_languages = { | |
"en": "English", | |
"ml": "Malayalam", | |
} | |
MONTHS = [ | |
"January", | |
"February", | |
"March", | |
"April", | |
"May", | |
"June", | |
"July", | |
"August", | |
"September", | |
"October", | |
"November", | |
"December", | |
] | |
def main(): | |
# Page title and header | |
st.title("Day in History") | |
with st.form("my_form"): | |
st.header("Select a date") | |
selected_language = st.selectbox( | |
"Select language", | |
supported_languages.keys(), | |
format_func=lambda x: supported_languages[x], | |
index=0, | |
) | |
col1, col2, col3 = st.columns(3) | |
# Datepicker | |
with col1: | |
selected_day = st.selectbox( | |
"Select Day", range(1, 32), index=datetime.now().day - 1 | |
) | |
with col2: | |
selected_month = st.selectbox( | |
"Select Month", | |
range(0, 12), | |
format_func=lambda x: MONTHS[x], | |
index=datetime.now().month - 1, | |
) | |
with col3: | |
selected_year = st.number_input( | |
"Enter Year (optional)", min_value=0, max_value=9999, value=None | |
) | |
submitted = st.form_submit_button("Submit") | |
if submitted: | |
df = load_data(selected_language) | |
# Process data based on selected date | |
if selected_year is None or selected_year == "": | |
filtered_data = df[ | |
(df["month"] == int(selected_month + 1)) | |
& (df["day"] == int(selected_day)) | |
] | |
else: | |
filtered_data = df[ | |
(df["year"] == int(selected_year)) | |
& (df["month"] == selected_month + 1) | |
& (df["day"] == int(selected_day)) | |
] | |
# Display results | |
if not filtered_data.empty: | |
st.subheader("Search Results") | |
for index, row in filtered_data.iterrows(): | |
container = st.container(border=True) | |
container.subheader( | |
f"{row['year']} {MONTHS[row['month']-1]} {row['day']}" | |
) | |
container.markdown( | |
f"{row['event_description']}", unsafe_allow_html=True | |
) | |
if row["reference"] is not None: | |
container.markdown(f"{row['reference']}", unsafe_allow_html=True) | |
else: | |
st.warning( | |
f"No data found for selected date {selected_day} {MONTHS[selected_month]} {selected_year}" | |
) | |
if __name__ == "__main__": | |
main() | |