Spaces:
Running
Running
Benjamin Consolvo
commited on
Commit
·
9fd65b3
1
Parent(s):
15d1512
links to articles
Browse files
app.py
CHANGED
@@ -438,10 +438,53 @@ class TradingApp:
|
|
438 |
elif symbol and sentiment_symbol == symbol and sentiment_result is None:
|
439 |
st.markdown("**Sentiment:** No sentiment available")
|
440 |
|
|
|
441 |
if symbol and sentiment_symbol == symbol and article_headlines:
|
442 |
-
st.markdown(
|
443 |
-
|
444 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
445 |
elif symbol and sentiment_symbol == symbol and sentiment_result is not None and not article_headlines:
|
446 |
st.markdown("_No headlines available._")
|
447 |
|
|
|
438 |
elif symbol and sentiment_symbol == symbol and sentiment_result is None:
|
439 |
st.markdown("**Sentiment:** No sentiment available")
|
440 |
|
441 |
+
# Shrink headlines font and remove bullets, add clickable links
|
442 |
if symbol and sentiment_symbol == symbol and article_headlines:
|
443 |
+
st.markdown(
|
444 |
+
"<div style='font-size: 0.85em; margin-bottom: 0.5em;'><b>Recent Headlines:</b></div>",
|
445 |
+
unsafe_allow_html=True
|
446 |
+
)
|
447 |
+
# Try to get URLs for headlines if available
|
448 |
+
# Fetch the latest headlines and URLs from the same source as get_sentiment_and_headlines
|
449 |
+
# For NewsAPI, get 'url' from articles; for Alpha Vantage, get 'url' from feed
|
450 |
+
headlines_with_links = []
|
451 |
+
try:
|
452 |
+
if sentiment_source == "NewsAPI":
|
453 |
+
articles = self.sentiment.newsapi.get_everything(q=symbol, language='en', sort_by='publishedAt', page=1)
|
454 |
+
articles = articles.get('articles', [])[:5]
|
455 |
+
headlines_with_links = [
|
456 |
+
(a.get('title'), a.get('url')) for a in articles if a.get('title')
|
457 |
+
]
|
458 |
+
elif sentiment_source == "AlphaVantage":
|
459 |
+
import requests
|
460 |
+
url = (
|
461 |
+
f"https://www.alphavantage.co/query?function=NEWS_SENTIMENT&tickers={symbol}"
|
462 |
+
f"&apikey={self.sentiment.alpha_vantage_api_key}"
|
463 |
+
)
|
464 |
+
resp = requests.get(url)
|
465 |
+
data = resp.json()
|
466 |
+
feed = data.get("feed", [])[:5]
|
467 |
+
headlines_with_links = [
|
468 |
+
(item.get("title"), item.get("url")) for item in feed if item.get("title")
|
469 |
+
]
|
470 |
+
else:
|
471 |
+
# fallback: just show headlines without links
|
472 |
+
headlines_with_links = [(headline, None) for headline in article_headlines]
|
473 |
+
except Exception as e:
|
474 |
+
logger.error(f"Error fetching URLs for headlines: {e}")
|
475 |
+
headlines_with_links = [(headline, None) for headline in article_headlines]
|
476 |
+
|
477 |
+
for headline, url in headlines_with_links:
|
478 |
+
if url:
|
479 |
+
st.markdown(
|
480 |
+
f"<div style='font-size: 0.85em; margin-bottom: 0.2em; color: #444;'><a href='{url}' target='_blank' style='color: #444; text-decoration: underline;'>{headline}</a></div>",
|
481 |
+
unsafe_allow_html=True
|
482 |
+
)
|
483 |
+
else:
|
484 |
+
st.markdown(
|
485 |
+
f"<div style='font-size: 0.85em; margin-bottom: 0.2em; color: #444;'>{headline}</div>",
|
486 |
+
unsafe_allow_html=True
|
487 |
+
)
|
488 |
elif symbol and sentiment_symbol == symbol and sentiment_result is not None and not article_headlines:
|
489 |
st.markdown("_No headlines available._")
|
490 |
|