Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -98,21 +98,32 @@ 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 |
ticker_symbol = st.selectbox('Select a stock', options=stocks)
|
103 |
|
104 |
-
#
|
105 |
-
if
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
score_msg = f"**{row['Stock']}**: <span style='color: {color};'>{row['Combined Score']}</span>"
|
115 |
-
st.markdown(score_msg, unsafe_allow_html=True)
|
116 |
|
117 |
|
118 |
|
|
|
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 |
|