Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# Load data
|
| 7 |
+
@st.cache_data # Cache data for performance improvement
|
| 8 |
+
def load_data():
|
| 9 |
+
dataset = load_dataset("santhosh/day_in_history", split="train")
|
| 10 |
+
df = pd.DataFrame(dataset)
|
| 11 |
+
return df
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Function to process user input (date) and return description and reference
|
| 15 |
+
def process_date(day, month, year=None):
|
| 16 |
+
# Filter data based on selected date
|
| 17 |
+
if year is None or year == "":
|
| 18 |
+
filtered_data = df[(df["month"] == month) & (df["day"] == int(day))]
|
| 19 |
+
else:
|
| 20 |
+
filtered_data = df[
|
| 21 |
+
(df["year"] == int(year)) & (df["month"] == month) & (df["day"] == int(day))
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
if not filtered_data.empty:
|
| 25 |
+
# Prepare empty lists to store descriptions and references
|
| 26 |
+
descriptions = []
|
| 27 |
+
references = []
|
| 28 |
+
|
| 29 |
+
# Loop through filtered data and append descriptions and references
|
| 30 |
+
for index, row in filtered_data.iterrows():
|
| 31 |
+
descriptions.append(row["event_description"])
|
| 32 |
+
references.append(row["reference"])
|
| 33 |
+
|
| 34 |
+
# Return lists of descriptions and references
|
| 35 |
+
return descriptions
|
| 36 |
+
else:
|
| 37 |
+
return [f"No data found for selected date {year} {month} {day}"]
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def main():
|
| 41 |
+
df = load_data()
|
| 42 |
+
# Page title and header
|
| 43 |
+
st.title("Day in History")
|
| 44 |
+
with st.form("my_form"):
|
| 45 |
+
st.header("Select a date to view results")
|
| 46 |
+
col1, col2, col3 = st.columns(3)
|
| 47 |
+
# Datepicker
|
| 48 |
+
with col1:
|
| 49 |
+
selected_day = st.selectbox("Select Day", range(1, 32), index=14)
|
| 50 |
+
with col2:
|
| 51 |
+
selected_month = st.selectbox(
|
| 52 |
+
"Select Month",
|
| 53 |
+
[
|
| 54 |
+
"January",
|
| 55 |
+
"February",
|
| 56 |
+
"March",
|
| 57 |
+
"April",
|
| 58 |
+
"May",
|
| 59 |
+
"June",
|
| 60 |
+
"July",
|
| 61 |
+
"August",
|
| 62 |
+
"September",
|
| 63 |
+
"October",
|
| 64 |
+
"November",
|
| 65 |
+
"December",
|
| 66 |
+
],
|
| 67 |
+
index=7,
|
| 68 |
+
)
|
| 69 |
+
with col3:
|
| 70 |
+
selected_year = st.number_input(
|
| 71 |
+
"Enter Year (optional)", min_value=0, max_value=9999, value=None
|
| 72 |
+
)
|
| 73 |
+
submitted = st.form_submit_button("Submit")
|
| 74 |
+
if submitted:
|
| 75 |
+
# Process data based on selected date
|
| 76 |
+
if selected_year is None or selected_year == "":
|
| 77 |
+
filtered_data = df[
|
| 78 |
+
(df["month"] == selected_month) & (df["day"] == int(selected_day))
|
| 79 |
+
]
|
| 80 |
+
else:
|
| 81 |
+
filtered_data = df[
|
| 82 |
+
(df["year"] == int(selected_year))
|
| 83 |
+
& (df["month"] == selected_month)
|
| 84 |
+
& (df["day"] == int(selected_day))
|
| 85 |
+
]
|
| 86 |
+
|
| 87 |
+
# Display results
|
| 88 |
+
if not filtered_data.empty:
|
| 89 |
+
st.subheader("Search Results")
|
| 90 |
+
|
| 91 |
+
for index, row in filtered_data.iterrows():
|
| 92 |
+
container = st.container(border=True)
|
| 93 |
+
container.title(f"{row['year']} {row['month']} {row['day']}")
|
| 94 |
+
container.markdown(
|
| 95 |
+
f"{row['event_description']}", unsafe_allow_html=True
|
| 96 |
+
)
|
| 97 |
+
if row["reference"] is not None:
|
| 98 |
+
container.markdown(f"{row['reference']}", unsafe_allow_html=True)
|
| 99 |
+
else:
|
| 100 |
+
st.warning(
|
| 101 |
+
f"No data found for selected date {selected_year} {selected_month} {selected_day}"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
main()
|