Spaces:
Sleeping
Sleeping
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() | |