os1187 commited on
Commit
7cf670a
1 Parent(s): c618439

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -21
app.py CHANGED
@@ -98,33 +98,44 @@ sp500_averages = load_sp500_averages(sp500_averages_path)
98
  # User interface in Streamlit
99
  st.title('S&P 500 Stock Comparison Tool')
100
 
101
- # Get combined scores and show overview first
102
  if 'scores_df' not in st.session_state:
103
  st.session_state['scores_df'] = calculate_combined_scores_for_stocks(stocks, sp500_averages)
104
 
105
  # Sort the DataFrame by combined score for the overview
106
  scores_df_sorted = st.session_state['scores_df'].sort_values(by='Combined Score', ascending=False)
107
 
108
- # Display the overview with color coding
109
- for _, row in scores_df_sorted.iterrows():
110
- color = "green" if row['Combined Score'] > 0 else "red" if row['Combined Score'] < 0 else "black"
111
- score_msg = f"**{row['Stock']}**: <span style='color: {color};'>{row['Combined Score']}</span>"
112
- st.markdown(score_msg, unsafe_allow_html=True)
113
-
114
- # Selection of stock from the dropdown
115
- ticker_symbol = st.selectbox('Select a stock', options=stocks)
116
-
117
- # Fetch and display details for the selected stock
118
- if ticker_symbol:
119
- with st.spinner(f'Fetching data for {ticker_symbol}...'):
120
- stock_data = fetch_stock_data(ticker_symbol)
121
- comparison, _ = compare_to_index(stock_data, sp500_averages)
122
-
123
- st.write(f"Financial Ratios for {ticker_symbol}:")
124
- for ratio, result in comparison.items():
125
- if ratio != 'Combined Score': # Avoid repeating the combined score in the loop
126
- st.write(f"{ratio}: {result}")
127
-
 
 
 
 
 
 
 
 
 
 
 
128
 
129
 
130
 
 
98
  # User interface in Streamlit
99
  st.title('S&P 500 Stock Comparison Tool')
100
 
101
+ # Fetch combined scores and overview if not already in session state
102
  if 'scores_df' not in st.session_state:
103
  st.session_state['scores_df'] = calculate_combined_scores_for_stocks(stocks, sp500_averages)
104
 
105
  # Sort the DataFrame by combined score for the overview
106
  scores_df_sorted = st.session_state['scores_df'].sort_values(by='Combined Score', ascending=False)
107
 
108
+ # Use columns for side-by-side layout
109
+ col1, col2 = st.columns([2, 3])
110
+
111
+ # First column for the sorted overview
112
+ with col1:
113
+ st.subheader("Stock Overview")
114
+ for _, row in scores_df_sorted.iterrows():
115
+ color = "green" if row['Combined Score'] > 0 else "red" if row['Combined Score'] < 0 else "grey"
116
+ st.markdown(f"<span style='color: {color};'>{row['Stock']}: {row['Combined Score']}</span>", unsafe_allow_html=True)
117
+
118
+ # Second column for detailed financial ratios and company information
119
+ with col2:
120
+ st.subheader("Stock Details")
121
+ ticker_symbol = st.selectbox('Select a stock for details', options=scores_df_sorted['Stock'].tolist())
122
+ if ticker_symbol:
123
+ with st.spinner(f'Fetching data for {ticker_symbol}...'):
124
+ stock_data = fetch_stock_data(ticker_symbol)
125
+ comparison, _ = compare_to_index(stock_data, sp500_averages)
126
+
127
+ # Fetch additional information such as company name and description
128
+ company_info = yf.Ticker(ticker_symbol).info
129
+ company_name = company_info.get('longName')
130
+ company_description = company_info.get('longBusinessSummary')
131
+
132
+ st.write(f"**{company_name}**")
133
+ st.write(company_description)
134
+
135
+ st.write(f"**Financial Ratios for {ticker_symbol}:**")
136
+ for ratio, result in comparison.items():
137
+ if ratio != 'Combined Score': # Avoid repeating the combined score
138
+ st.write(f"{ratio}: {result}")
139
 
140
 
141