Skip to content

Commit

Permalink
refactor load_feeds and add cache_data for the recommendation
Browse files Browse the repository at this point in the history
  • Loading branch information
timho102003 committed Sep 13, 2023
1 parent 94b04ac commit 642b410
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 317 deletions.
78 changes: 16 additions & 62 deletions template/feed_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from streamlit_extras.row import row

from config import FEED_ARTICLE_NUMS, NEWS_CATEGORIES
from utils import generate_feed_layout, load_cat_feed, load_feeds, load_search_feed, load_feeds_merge
from utils import generate_feed_layout, fetch_feeds


def feed_template():
Expand All @@ -12,7 +12,6 @@ def feed_template():
unsafe_allow_html=True,
)
st.session_state.feed_dayrange = 3
# st.session_state.cat_selection = ["Feed"]
_, search_col, _ = st.columns([0.25, 0.5, 0.25])
with search_col:
with st.form("Search Form", clear_on_submit=True):
Expand All @@ -32,69 +31,24 @@ def feed_template():
help="Choose articles from the last n days up to today.",
key="feed_dayrange",
)
cat_selection = st.multiselect(
st.session_state.cat_selection = st.multiselect(
label="News Categories",
options=["Feed"] + NEWS_CATEGORIES,
max_selections=1
max_selections=1,
default=st.session_state.get("cat_selection", [])
)

if search_submit:
cat_selection = ["search"]
st.session_state.cat_selection = ["search"]

if not cat_selection or cat_selection[0] == "Feed":
asyncio.run(
load_feeds_merge(total_articles=FEED_ARTICLE_NUMS, data_range=st.session_state.feed_dayrange)
)
# load_feeds(
# total_articles=FEED_ARTICLE_NUMS, data_range=st.session_state.feed_dayrange
# )
generate_feed_layout()
elif cat_selection[0] == "World":
load_cat_feed(
category="world",
total_articles=FEED_ARTICLE_NUMS,
data_range=st.session_state.feed_dayrange,
)
generate_feed_layout()
elif cat_selection[0] == "Nation":
load_cat_feed(
category="nation",
total_articles=FEED_ARTICLE_NUMS,
data_range=st.session_state.feed_dayrange,
)
generate_feed_layout()
elif cat_selection[0] == "Technology":
load_cat_feed(
category="Technology",
total_articles=FEED_ARTICLE_NUMS,
data_range=st.session_state.feed_dayrange,
)
generate_feed_layout()
elif cat_selection[0] == "Science":
load_cat_feed(
category="science",
total_articles=FEED_ARTICLE_NUMS,
data_range=st.session_state.feed_dayrange,
)
generate_feed_layout()
elif cat_selection[0] == "Entertainment":
load_cat_feed(
category="entertainment",
total_articles=FEED_ARTICLE_NUMS,
data_range=st.session_state.feed_dayrange,
)
generate_feed_layout()
elif cat_selection[0] == "Business":
load_cat_feed(
category="business",
total_articles=FEED_ARTICLE_NUMS,
data_range=st.session_state.feed_dayrange,
)
generate_feed_layout()
elif cat_selection[0] == "search":
load_search_feed(
search_msg=query_search,
total_articles=FEED_ARTICLE_NUMS,
data_range=st.session_state.feed_dayrange,
)
generate_feed_layout()
if not st.session_state.cat_selection or st.session_state.cat_selection[0] == "Feed":
st.session_state.recommend = fetch_feeds(total_articles=FEED_ARTICLE_NUMS, data_range=st.session_state.feed_dayrange)
elif st.session_state.cat_selection[0] == "search":
st.session_state.recommend = fetch_feeds(total_articles=FEED_ARTICLE_NUMS,
data_range=st.session_state.feed_dayrange,
mode=st.session_state.cat_selection[0],
search_msg=query_search,
thresh=0.2)
else:
st.session_state.recommend = fetch_feeds(total_articles=FEED_ARTICLE_NUMS, data_range=st.session_state.feed_dayrange, mode=st.session_state.cat_selection[0])
generate_feed_layout()
Loading

0 comments on commit 642b410

Please sign in to comment.