File size: 3,883 Bytes
e2f7a02
fa12a52
e2f7a02
5c42427
e2f7a02
 
 
 
5c42427
 
e2f7a02
 
 
fa12a52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c42427
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa12a52
e2f7a02
 
 
 
fa12a52
5c42427
 
 
 
 
 
e2f7a02
 
 
5c42427
 
 
e2f7a02
 
 
5c42427
 
 
e2f7a02
 
 
 
 
 
 
5c42427
e2f7a02
 
 
5c42427
 
e2f7a02
 
 
 
5c42427
e2f7a02
 
 
 
 
 
 
 
 
5c42427
 
 
e2f7a02
 
 
 
 
 
 
ed67125
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import pandas as pd
import streamlit as st
from datasets import load_dataset
from datetime import datetime


# Load data
@st.cache_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()