os1187 commited on
Commit
0424c3b
1 Parent(s): c40f5aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -13
app.py CHANGED
@@ -46,6 +46,7 @@ def calculate_combined_scores_for_stocks(stocks, index_averages):
46
  scores.append({'Stock': ticker_symbol, 'Combined Score': score})
47
  return pd.DataFrame(scores)
48
 
 
49
  # User interface in Streamlit
50
  st.title('S&P 500 Stock Comparison Tool')
51
 
@@ -62,31 +63,28 @@ col1, col2 = st.columns([1, 3])
62
 
63
  with col1:
64
  st.subheader("Stock Overview")
65
- # Make sure to convert 'Combined Score' to numeric if it's not already
66
  scores_df_sorted['Combined Score'] = pd.to_numeric(scores_df_sorted['Combined Score'], errors='coerce')
67
- # Apply color based on 'Combined Score' value
68
- def color_combined_score(value):
69
- color = 'green' if value > 0 else 'red' if value < 0 else 'none'
70
- return f'background-color: {color};'
71
-
72
- # Use 'Styler' to apply the style function to the 'Combined Score' column
73
- styled_scores_df = scores_df_sorted.style.applymap(color_combined_score, subset=['Combined Score'])
74
- st.dataframe(styled_scores_df)
75
 
76
  with col2:
77
  st.subheader("Stock Details")
78
- ticker_symbol = st.selectbox('Select a stock for details', options=sp500_list)
 
 
79
  if ticker_symbol:
80
  with st.spinner(f'Fetching data for {ticker_symbol}...'):
81
  stock_data, info = fetch_stock_data(ticker_symbol)
82
  comparison, _ = compare_to_index(stock_data, sp500_averages)
83
 
84
  # Display the company name and ticker symbol
85
- st.write(f"**{info.get('longName')}** ({ticker_symbol})")
86
- st.write(info.get('longBusinessSummary'))
87
 
88
  # Display each financial ratio and its comparison result
89
  for ratio, status in comparison.items():
90
- st.write(f"{ratio}: {status}")
 
91
 
92
 
 
46
  scores.append({'Stock': ticker_symbol, 'Combined Score': score})
47
  return pd.DataFrame(scores)
48
 
49
+
50
  # User interface in Streamlit
51
  st.title('S&P 500 Stock Comparison Tool')
52
 
 
63
 
64
  with col1:
65
  st.subheader("Stock Overview")
66
+ # Convert 'Combined Score' to numeric if it's not already
67
  scores_df_sorted['Combined Score'] = pd.to_numeric(scores_df_sorted['Combined Score'], errors='coerce')
68
+ # Apply color based on 'Combined Score' value and display the DataFrame
69
+ st.dataframe(scores_df_sorted.style.applymap(color_combined_score, subset=['Combined Score']))
 
 
 
 
 
 
70
 
71
  with col2:
72
  st.subheader("Stock Details")
73
+ # Get the sorted list of ticker symbols based on the combined score
74
+ sorted_tickers = scores_df_sorted['Stock'].tolist()
75
+ ticker_symbol = st.selectbox('Select a stock for details', options=sorted_tickers)
76
  if ticker_symbol:
77
  with st.spinner(f'Fetching data for {ticker_symbol}...'):
78
  stock_data, info = fetch_stock_data(ticker_symbol)
79
  comparison, _ = compare_to_index(stock_data, sp500_averages)
80
 
81
  # Display the company name and ticker symbol
82
+ st.write(f"**{info.get('longName', 'N/A')}** ({ticker_symbol})")
83
+ st.write(info.get('longBusinessSummary', 'Description not available.'))
84
 
85
  # Display each financial ratio and its comparison result
86
  for ratio, status in comparison.items():
87
+ st.text(f"{ratio}: {status}")
88
+
89
 
90