Dmitry Beresnev
commited on
Commit
Β·
e7a399b
1
Parent(s):
7e1fe44
fix news feed
Browse files- app/components/news.py +93 -150
app/components/news.py
CHANGED
|
@@ -7,24 +7,7 @@ import html as html_module
|
|
| 7 |
|
| 8 |
|
| 9 |
def display_news_card(news_item: dict):
|
| 10 |
-
"""Display a single news card with professional styling."""
|
| 11 |
-
|
| 12 |
-
# Sentiment color mapping
|
| 13 |
-
sentiment_colors = {
|
| 14 |
-
'positive': '#10b981', # Green
|
| 15 |
-
'negative': '#ef4444', # Red
|
| 16 |
-
'neutral': '#6b7280' # Gray
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
# Impact badge styling
|
| 20 |
-
impact_styles = {
|
| 21 |
-
'high': 'background: #fee2e2; color: #991b1b; border: 1px solid #fca5a5;',
|
| 22 |
-
'medium': 'background: #fef3c7; color: #92400e; border: 1px solid #fcd34d;',
|
| 23 |
-
'low': 'background: #dbeafe; color: #1e40af; border: 1px solid #93c5fd;'
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
sentiment_color = sentiment_colors.get(news_item['sentiment'], '#6b7280')
|
| 27 |
-
impact_style = impact_styles.get(news_item['impact'], '')
|
| 28 |
|
| 29 |
# Calculate time ago
|
| 30 |
time_diff = datetime.now() - news_item['timestamp']
|
|
@@ -35,78 +18,52 @@ def display_news_card(news_item: dict):
|
|
| 35 |
else:
|
| 36 |
time_ago = f"{time_diff.seconds // 3600}h ago"
|
| 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 |
-
{news_item.get('summary', '').strip()}
|
| 85 |
-
</h3>
|
| 86 |
-
|
| 87 |
-
<!-- Meta info -->
|
| 88 |
-
<div style='color: #9ca3af; font-size: 13px; display: flex; gap: 16px; flex-wrap: wrap;'>
|
| 89 |
-
<span>π {time_ago}</span>
|
| 90 |
-
<span>β€οΈ {news_item['likes']:,}</span>
|
| 91 |
-
<span>π {news_item['retweets']:,}</span>
|
| 92 |
-
<span>β‘ Engagement: {(news_item['likes'] + news_item['retweets'] * 2):,}</span>
|
| 93 |
-
</div>
|
| 94 |
-
</div>
|
| 95 |
-
|
| 96 |
-
<!-- Action button -->
|
| 97 |
-
<a href='{news_item['url']}' target='_blank'
|
| 98 |
-
style='background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
|
| 99 |
-
color: white; padding: 10px 20px; border-radius: 8px;
|
| 100 |
-
text-decoration: none; white-space: nowrap; font-size: 14px;
|
| 101 |
-
font-weight: 600; box-shadow: 0 2px 4px rgba(59, 130, 246, 0.3);
|
| 102 |
-
transition: all 0.2s; display: inline-block;'>
|
| 103 |
-
Read More β
|
| 104 |
-
</a>
|
| 105 |
-
</div>
|
| 106 |
-
</div>
|
| 107 |
-
"""
|
| 108 |
-
|
| 109 |
-
st.markdown(html, unsafe_allow_html=True)
|
| 110 |
|
| 111 |
|
| 112 |
def display_news_feed(df: pd.DataFrame, max_items: int = 20):
|
|
@@ -168,7 +125,7 @@ def display_news_statistics(stats: dict):
|
|
| 168 |
|
| 169 |
|
| 170 |
def display_category_breakdown(stats: dict):
|
| 171 |
-
"""Display news breakdown by category."""
|
| 172 |
|
| 173 |
if 'by_category' not in stats:
|
| 174 |
return
|
|
@@ -185,75 +142,61 @@ def display_category_breakdown(stats: dict):
|
|
| 185 |
col1, col2, col3 = st.columns(3)
|
| 186 |
|
| 187 |
with col1:
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
{categories.get('macro', 0)}
|
| 195 |
-
</div>
|
| 196 |
-
<div style='color: #9ca3af; font-size: 12px;'>{macro_pct:.1f}% of total</div>
|
| 197 |
-
</div>
|
| 198 |
-
""", unsafe_allow_html=True)
|
| 199 |
|
| 200 |
with col2:
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
{categories.get('geopolitical', 0)}
|
| 208 |
-
</div>
|
| 209 |
-
<div style='color: #9ca3af; font-size: 12px;'>{geo_pct:.1f}% of total</div>
|
| 210 |
-
</div>
|
| 211 |
-
""", unsafe_allow_html=True)
|
| 212 |
|
| 213 |
with col3:
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
{categories.get('markets', 0)}
|
| 221 |
-
</div>
|
| 222 |
-
<div style='color: #9ca3af; font-size: 12px;'>{markets_pct:.1f}% of total</div>
|
| 223 |
-
</div>
|
| 224 |
-
""", unsafe_allow_html=True)
|
| 225 |
|
| 226 |
|
| 227 |
def display_breaking_news_banner(df: pd.DataFrame):
|
| 228 |
-
"""Display breaking news banner at the top."""
|
| 229 |
|
| 230 |
breaking = df[df['is_breaking'] == True] if not df.empty and 'is_breaking' in df.columns else pd.DataFrame()
|
| 231 |
|
| 232 |
if not breaking.empty:
|
| 233 |
latest = breaking.iloc[0]
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
def display_news_card(news_item: dict):
|
| 10 |
+
"""Display a single news card with professional styling using Streamlit components."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Calculate time ago
|
| 13 |
time_diff = datetime.now() - news_item['timestamp']
|
|
|
|
| 18 |
else:
|
| 19 |
time_ago = f"{time_diff.seconds // 3600}h ago"
|
| 20 |
|
| 21 |
+
# Create container with custom styling
|
| 22 |
+
with st.container():
|
| 23 |
+
# Add custom CSS for this card
|
| 24 |
+
st.markdown("""
|
| 25 |
+
<style>
|
| 26 |
+
.news-card {
|
| 27 |
+
background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
|
| 28 |
+
border: 1px solid #374151;
|
| 29 |
+
border-radius: 12px;
|
| 30 |
+
padding: 20px;
|
| 31 |
+
margin-bottom: 16px;
|
| 32 |
+
}
|
| 33 |
+
</style>
|
| 34 |
+
""", unsafe_allow_html=True)
|
| 35 |
+
|
| 36 |
+
# Header row with source and badges
|
| 37 |
+
col1, col2 = st.columns([3, 1])
|
| 38 |
+
|
| 39 |
+
with col1:
|
| 40 |
+
# Source and badges in one line
|
| 41 |
+
badge_cols = st.columns([2, 1, 1, 1])
|
| 42 |
+
with badge_cols[0]:
|
| 43 |
+
st.markdown(f"**:blue[{news_item['source']}]**")
|
| 44 |
+
with badge_cols[1]:
|
| 45 |
+
if news_item['impact'] == 'high':
|
| 46 |
+
st.markdown("π΄ **HIGH**")
|
| 47 |
+
elif news_item['impact'] == 'medium':
|
| 48 |
+
st.markdown("π‘ **MED**")
|
| 49 |
+
else:
|
| 50 |
+
st.markdown("π’ **LOW**")
|
| 51 |
+
with badge_cols[2]:
|
| 52 |
+
sentiment_emoji = {'positive': 'π', 'negative': 'π', 'neutral': 'β‘οΈ'}
|
| 53 |
+
st.markdown(f"{sentiment_emoji.get(news_item['sentiment'], 'β‘οΈ')} {news_item['sentiment'].title()}")
|
| 54 |
+
with badge_cols[3]:
|
| 55 |
+
st.markdown(f"**#{news_item['category']}**")
|
| 56 |
+
|
| 57 |
+
with col2:
|
| 58 |
+
st.markdown(f"[**Read More β**]({news_item['url']})")
|
| 59 |
+
|
| 60 |
+
# Title/Summary
|
| 61 |
+
st.markdown(f"### {news_item.get('summary', '').strip()}")
|
| 62 |
+
|
| 63 |
+
# Meta information
|
| 64 |
+
st.caption(f"π {time_ago} | β€οΈ {news_item['likes']:,} | π {news_item['retweets']:,}")
|
| 65 |
+
|
| 66 |
+
st.markdown("---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
def display_news_feed(df: pd.DataFrame, max_items: int = 20):
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
def display_category_breakdown(stats: dict):
|
| 128 |
+
"""Display news breakdown by category using Streamlit components."""
|
| 129 |
|
| 130 |
if 'by_category' not in stats:
|
| 131 |
return
|
|
|
|
| 142 |
col1, col2, col3 = st.columns(3)
|
| 143 |
|
| 144 |
with col1:
|
| 145 |
+
macro_count = categories.get('macro', 0)
|
| 146 |
+
macro_pct = (macro_count / total) * 100
|
| 147 |
+
with st.container():
|
| 148 |
+
st.markdown("**:blue[π MACRO]**")
|
| 149 |
+
st.markdown(f"# {macro_count}")
|
| 150 |
+
st.caption(f"{macro_pct:.1f}% of total")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
with col2:
|
| 153 |
+
geo_count = categories.get('geopolitical', 0)
|
| 154 |
+
geo_pct = (geo_count / total) * 100
|
| 155 |
+
with st.container():
|
| 156 |
+
st.markdown("**:orange[π GEOPOLITICAL]**")
|
| 157 |
+
st.markdown(f"# {geo_count}")
|
| 158 |
+
st.caption(f"{geo_pct:.1f}% of total")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
with col3:
|
| 161 |
+
markets_count = categories.get('markets', 0)
|
| 162 |
+
markets_pct = (markets_count / total) * 100
|
| 163 |
+
with st.container():
|
| 164 |
+
st.markdown("**:green[πΉ MARKETS]**")
|
| 165 |
+
st.markdown(f"# {markets_count}")
|
| 166 |
+
st.caption(f"{markets_pct:.1f}% of total")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
|
| 169 |
def display_breaking_news_banner(df: pd.DataFrame):
|
| 170 |
+
"""Display breaking news banner at the top using Streamlit components."""
|
| 171 |
|
| 172 |
breaking = df[df['is_breaking'] == True] if not df.empty and 'is_breaking' in df.columns else pd.DataFrame()
|
| 173 |
|
| 174 |
if not breaking.empty:
|
| 175 |
latest = breaking.iloc[0]
|
| 176 |
|
| 177 |
+
# Create container with red background styling
|
| 178 |
+
with st.container():
|
| 179 |
+
st.markdown("""
|
| 180 |
+
<style>
|
| 181 |
+
.breaking-news-container {
|
| 182 |
+
background: linear-gradient(135deg, #dc2626 0%, #991b1b 100%);
|
| 183 |
+
border-radius: 12px;
|
| 184 |
+
padding: 20px;
|
| 185 |
+
margin-bottom: 24px;
|
| 186 |
+
border: 2px solid #fca5a5;
|
| 187 |
+
}
|
| 188 |
+
</style>
|
| 189 |
+
""", unsafe_allow_html=True)
|
| 190 |
+
|
| 191 |
+
# Layout with emoji and content
|
| 192 |
+
col1, col2, col3 = st.columns([1, 8, 2])
|
| 193 |
+
|
| 194 |
+
with col1:
|
| 195 |
+
st.markdown("# π¨")
|
| 196 |
+
|
| 197 |
+
with col2:
|
| 198 |
+
st.markdown(f"**:red[BREAKING NEWS β’ {latest['source'].upper()}]**")
|
| 199 |
+
st.markdown(f"## {latest.get('summary', '').strip()}")
|
| 200 |
+
|
| 201 |
+
with col3:
|
| 202 |
+
st.markdown(f"[**READ NOW β**]({latest['url']})")
|