File size: 3,679 Bytes
5a85659
04cdb6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a85659
04cdb6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 fetch_top_news as news_api
import time

# Function to load news into session state
def load_news():
    if "news_data" not in st.session_state:
        # Fetch news if it's not available in session state
        st.session_state.news_data = news_api.get_top_news(language="en")
        st.session_state.last_fetch_time = time.time()  # Save the fetch time

# Function to check if the news needs to be refreshed
def should_refresh_data(): 
    if "last_fetch_time" not in st.session_state:
        return True  # Force refresh if no fetch time is found
    time_since_last_fetch = time.time() - st.session_state.last_fetch_time
    return time_since_last_fetch > 10800  # Refresh every 10 minutes

# Function to filter news by category
def filter_news_by_category(news_data, category):
    if category == "All":
        return news_data
    return [news for news in news_data if news['category'] == category]

def main():
    st.markdown("<h1 style='font-size: 50px;'>QuantumQuest</h1>", unsafe_allow_html=True)

    st.markdown("""

        <style>

        .category-button {

            background-color: #4CAF50; /* Green */

            border: none;

            width: 150px;  /* Set fixed width */

            height: 50px;  /* Set fixed height */

            color: white;

            padding: 10px 24px;

            text-align: center;

            text-decoration: none;

            display: inline-block;

            font-size: 16px;

            margin: 4px 2px;

            cursor: pointer;

            border-radius: 8px;

            transition-duration: 0.4s;

        }



        .category-button:hover {

            background-color: #3e8e41;

            color: white;

        }

        </style>

        """, unsafe_allow_html=True)

    # List of categories
    categories = ["All", "Sports", "Business", "Science/Tech", "Politics", "Entertainment", "Others"]

    # Load or refresh news data
    if should_refresh_data():
        load_news()

    tab1, tab2, tab3 = st.tabs(["Categories", "Popular News", "Test Area"])

    # Categories Tab
    with tab1:
        st.header("Categories")

        # Create category selection using st.pills
        selection = st.pills("", categories)
        st.write(f"You have selected: {selection}")

        # Fetch news data
        if "news_data" in st.session_state:
            news_data = st.session_state.news_data
            filtered_news = filter_news_by_category(news_data, selection)

            # Display each news in its own bordered container
            for news in filtered_news:
                with st.container(border = True):
                    st.markdown(f"**{news['title']}**")
                    st.write(news['summary'])
                    st.markdown(f"[Read more]({news['url']})")
                    st.divider()  # Adds a divider between news items

    # Popular News Tab
    with tab2:
        if "news_data" in st.session_state:
            news_data = st.session_state.news_data
            st.write(f"Fetched {len(news_data)} news articles.")

            # Display the news articles
            for news in news_data:
                st.subheader(f"{news['index']}. {news['title']}")
                st.write(news['summary'])
                st.write(f"[Read more]({news['url']})")
                st.divider()

    # Test Area Tab
    with tab3:
        container1 = st.container()
        container1.write("This is container 1")

        container2 = st.container()
        container2.write("This is container 2")

if __name__ == "__main__":
    main()