File size: 3,527 Bytes
e2f7a02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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()