Akshayram1 commited on
Commit
7ddc5f5
1 Parent(s): c53d975

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -27
app.py CHANGED
@@ -37,8 +37,9 @@ def parse_news(news_table):
37
 
38
  for x in news_table.findAll('tr'):
39
  try:
40
- # Get the headline text
41
  text = x.a.get_text()
 
42
  # Get the date and time from the first <td> tag
43
  date_scrape = x.td.text.strip().split()
44
 
@@ -55,14 +56,14 @@ def parse_news(news_table):
55
  datetime_parsed = parser.parse(datetime_str)
56
 
57
  # Append the parsed news to the list
58
- parsed_news.append([datetime_parsed, text])
59
 
60
  except Exception as e:
61
  print("Error parsing news:", e)
62
  continue
63
 
64
  # Convert the list to a DataFrame
65
- columns = ['datetime', 'headline']
66
  parsed_news_df = pd.DataFrame(parsed_news, columns=columns)
67
 
68
  return parsed_news_df
@@ -123,30 +124,35 @@ df = pd.DataFrame({'datetime': datetime.datetime.now(), 'ticker': ticker}, index
123
 
124
 
125
  try:
126
- st.subheader("Hourly and Daily Sentiment of {} Stock".format(ticker))
127
- news_table = get_news(ticker)
128
- parsed_news_df = parse_news(news_table)
129
- print(parsed_news_df)
130
- parsed_and_scored_news = score_news(parsed_news_df)
131
- fig_hourly = plot_hourly_sentiment(parsed_and_scored_news, ticker)
132
- fig_daily = plot_daily_sentiment(parsed_and_scored_news, ticker)
133
-
134
- st.plotly_chart(fig_hourly)
135
- st.plotly_chart(fig_daily)
136
-
137
- description = """
138
- The above chart averages the sentiment scores of {} stock hourly and daily.
139
- The table below gives each of the most recent headlines of the stock and the negative, neutral, positive and an aggregated sentiment score.
140
- The news headlines are obtained from the FinViz website.
141
- Sentiments are given by the nltk.sentiment.vader Python library.
142
- """.format(ticker)
143
-
144
- st.write(description)
145
- st.table(parsed_and_scored_news)
146
-
 
 
 
 
 
147
  except Exception as e:
148
- print(str(e))
149
- st.write("Enter a correct stock ticker, e.g. 'AAPL' above and hit Enter.")
150
 
151
  hide_streamlit_style = """
152
  <style>
@@ -154,4 +160,4 @@ hide_streamlit_style = """
154
  footer {visibility: hidden;}
155
  </style>
156
  """
157
- st.markdown(hide_streamlit_style, unsafe_allow_html=True)
 
37
 
38
  for x in news_table.findAll('tr'):
39
  try:
40
+ # Get the headline text and link
41
  text = x.a.get_text()
42
+ link = x.a['href']
43
  # Get the date and time from the first <td> tag
44
  date_scrape = x.td.text.strip().split()
45
 
 
56
  datetime_parsed = parser.parse(datetime_str)
57
 
58
  # Append the parsed news to the list
59
+ parsed_news.append([datetime_parsed, text, link])
60
 
61
  except Exception as e:
62
  print("Error parsing news:", e)
63
  continue
64
 
65
  # Convert the list to a DataFrame
66
+ columns = ['datetime', 'headline', 'link']
67
  parsed_news_df = pd.DataFrame(parsed_news, columns=columns)
68
 
69
  return parsed_news_df
 
124
 
125
 
126
  try:
127
+ st.subheader("Hourly and Daily Sentiment of {} Stock".format(ticker))
128
+ news_table = get_news(ticker)
129
+ parsed_news_df = parse_news(news_table)
130
+ print(parsed_news_df)
131
+ parsed_and_scored_news = score_news(parsed_news_df)
132
+ fig_hourly = plot_hourly_sentiment(parsed_and_scored_news, ticker)
133
+ fig_daily = plot_daily_sentiment(parsed_and_scored_news, ticker)
134
+
135
+ st.plotly_chart(fig_hourly)
136
+ st.plotly_chart(fig_daily)
137
+
138
+ description = """
139
+ The above chart averages the sentiment scores of {} stock hourly and daily.
140
+ The table below gives each of the most recent headlines of the stock and the negative, neutral, positive and an aggregated sentiment score.
141
+ The news headlines are obtained from the FinViz website.
142
+ Sentiments are given by the nltk.sentiment.vader Python library.
143
+ """.format(ticker)
144
+
145
+ st.write(description)
146
+
147
+ # Convert links to clickable HTML
148
+ parsed_and_scored_news['link'] = parsed_and_scored_news['link'].apply(lambda x: f'<a href="{x}" target="_blank">Link</a>')
149
+
150
+ # Display the table with the new link column
151
+ st.write(parsed_and_scored_news.to_html(escape=False), unsafe_allow_html=True)
152
+
153
  except Exception as e:
154
+ print(str(e))
155
+ st.write("Enter a correct stock ticker, e.g. 'AAPL' above and hit Enter.")
156
 
157
  hide_streamlit_style = """
158
  <style>
 
160
  footer {visibility: hidden;}
161
  </style>
162
  """
163
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)