os1187 commited on
Commit
c618439
·
verified ·
1 Parent(s): 4e9c027

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
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
- # Selection of stock from predefined list
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  ticker_symbol = st.selectbox('Select a stock', options=stocks)
103
 
104
- # Assuming 'stocks' is your list of stock ticker symbols
105
- if st.button('Calculate Combined Scores'):
106
- scores_df = calculate_combined_scores_for_stocks(stocks, sp500_averages)
107
-
108
- # Sort by 'Combined Score' to prioritize viewing
109
- scores_df.sort_values(by='Combined Score', ascending=False, inplace=True)
110
-
111
- # Display each stock and its combined score with color coding
112
- for _, row in scores_df.iterrows():
113
- color = "green" if row['Combined Score'] > 0 else "red" if row['Combined Score'] < 0 else "black"
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