day_in_history / app.py
santhosh's picture
Create app.py
e2f7a02 verified
raw
history blame
3.53 kB
import streamlit as st
import pandas as pd
from datasets import load_dataset
# Load data
@st.cache_data # Cache data for performance improvement
def load_data():
dataset = load_dataset("santhosh/day_in_history", 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}"]
def main():
df = load_data()
# Page title and header
st.title("Day in History")
with st.form("my_form"):
st.header("Select a date to view results")
col1, col2, col3 = st.columns(3)
# Datepicker
with col1:
selected_day = st.selectbox("Select Day", range(1, 32), index=14)
with col2:
selected_month = st.selectbox(
"Select Month",
[
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
],
index=7,
)
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:
# Process data based on selected date
if selected_year is None or selected_year == "":
filtered_data = df[
(df["month"] == selected_month) & (df["day"] == int(selected_day))
]
else:
filtered_data = df[
(df["year"] == int(selected_year))
& (df["month"] == selected_month)
& (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.title(f"{row['year']} {row['month']} {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_year} {selected_month} {selected_day}"
)
if __name__ == "__main__":
main()