Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
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 |
-
#
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
st.
|
124 |
-
|
125 |
-
|
126 |
-
|
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 |
|