QuantumLearner commited on
Commit
eb54a66
·
verified ·
1 Parent(s): 70f761a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -20
app.py CHANGED
@@ -50,6 +50,22 @@ with st.sidebar.expander("Configuration", expanded=True):
50
 
51
  run_button = st.sidebar.button("Run Analysis")
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  # ---- HELPER FUNCTION: VALUE FORMATTING ----
54
  def format_value(x):
55
  if abs(x) >= 1e9:
@@ -80,11 +96,10 @@ def fetch_data(ticker, period_api, period_count, API_KEY):
80
  def main():
81
  st.title("Analyst Forecasts & Estimates")
82
  st.write("This tool fetches historical financial data and analyst forecasts. "
83
- "It helps you see past trends and future estimates over your selected period.")
84
-
85
- # Use session_state to hold results across pages
86
  if run_button:
87
- # Run analysis and store results in session state.
88
  if not ticker.strip():
89
  st.error("Please enter a valid ticker.")
90
  return
@@ -116,30 +131,28 @@ def main():
116
  if "date" in forecast_df.columns:
117
  forecast_df = forecast_df[forecast_df["date"] >= cutoff_date]
118
 
119
- # Save the processed data along with metadata in session state.
120
  st.session_state.analysis_results = {
121
  "ticker": ticker,
122
  "historical_df": historical_df,
123
  "forecast_df": forecast_df,
124
- "xaxis_title": xaxis_title,
125
- "tickformat": tickformat,
126
- "dtick": dtick,
127
- "data_period": data_period,
128
  }
129
- elif "analysis_results" not in st.session_state:
130
- st.info("Set your inputs in the sidebar, then click **Run Analysis**.")
 
131
  return
132
 
133
- # Retrieve stored results if available.
 
134
  res = st.session_state.analysis_results
135
  ticker_stored = res["ticker"]
136
  historical_df = res["historical_df"]
137
  forecast_df = res["forecast_df"]
138
- xaxis_title = res["xaxis_title"]
139
- tickformat = res["tickformat"]
140
- dtick = res["dtick"]
141
 
142
- # --- Define Metrics Mapping ---
143
  metrics = {
144
  "Revenue": {
145
  "historical": "revenue",
@@ -217,9 +230,8 @@ def main():
217
  name=f"Forecast {label}"
218
  ))
219
 
220
- # Pick the appropriate analyst count field.
221
  analyst_field = "numberAnalystsEstimatedEps" if metric_name == "EPS" else "numberAnalystEstimatedRevenue"
222
-
223
  if analyst_field in forecast_df.columns and not forecast_df.empty:
224
  analysts_count = int(round(forecast_df[analyst_field].mean()))
225
  else:
@@ -255,14 +267,13 @@ def main():
255
  for metric, mapping in metrics.items():
256
  st.subheader(metric)
257
  st.write(
258
- f"This chart shows {metric} over the selected time periods. "
259
  "Bars represent historical data and lines represent forecast ranges. "
260
  "Hover over markers for details."
261
  )
262
  fig = create_plot(metric, mapping["historical"], mapping["forecast"])
263
  st.plotly_chart(fig, use_container_width=True)
264
 
265
- # Data display expander.
266
  with st.expander(f"View {metric} Data", expanded=False):
267
  relevant_cols = []
268
  hc = mapping["historical"]
 
50
 
51
  run_button = st.sidebar.button("Run Analysis")
52
 
53
+ # Save current sidebar configuration in session_state.
54
+ if "sidebar_config" not in st.session_state:
55
+ st.session_state.sidebar_config = {
56
+ "xaxis_title": xaxis_title,
57
+ "tickformat": tickformat,
58
+ "dtick": dtick,
59
+ "data_period": data_period,
60
+ }
61
+ else:
62
+ st.session_state.sidebar_config.update({
63
+ "xaxis_title": xaxis_title,
64
+ "tickformat": tickformat,
65
+ "dtick": dtick,
66
+ "data_period": data_period,
67
+ })
68
+
69
  # ---- HELPER FUNCTION: VALUE FORMATTING ----
70
  def format_value(x):
71
  if abs(x) >= 1e9:
 
96
  def main():
97
  st.title("Analyst Forecasts & Estimates")
98
  st.write("This tool fetches historical financial data and analyst forecasts. "
99
+ "It shows past trends and future estimates over your selected period.")
100
+
101
+ # If the user has clicked Run, fetch new data and store in session state.
102
  if run_button:
 
103
  if not ticker.strip():
104
  st.error("Please enter a valid ticker.")
105
  return
 
131
  if "date" in forecast_df.columns:
132
  forecast_df = forecast_df[forecast_df["date"] >= cutoff_date]
133
 
134
+ # Store results in session state.
135
  st.session_state.analysis_results = {
136
  "ticker": ticker,
137
  "historical_df": historical_df,
138
  "forecast_df": forecast_df,
 
 
 
 
139
  }
140
+ # If Run Analysis was not clicked and no stored result exists, display info.
141
+ elif "analysis_results" not in st.session_state or st.session_state.analysis_results == {}:
142
+ st.info("Set your inputs in the sidebar and click **Run Analysis**.")
143
  return
144
 
145
+ # Retrieve stored results.
146
+ config = st.session_state.sidebar_config
147
  res = st.session_state.analysis_results
148
  ticker_stored = res["ticker"]
149
  historical_df = res["historical_df"]
150
  forecast_df = res["forecast_df"]
151
+ xaxis_title = config["xaxis_title"]
152
+ tickformat = config["tickformat"]
153
+ dtick = config["dtick"]
154
 
155
+ # Mapping for metrics.
156
  metrics = {
157
  "Revenue": {
158
  "historical": "revenue",
 
230
  name=f"Forecast {label}"
231
  ))
232
 
233
+ # Select analyst count field.
234
  analyst_field = "numberAnalystsEstimatedEps" if metric_name == "EPS" else "numberAnalystEstimatedRevenue"
 
235
  if analyst_field in forecast_df.columns and not forecast_df.empty:
236
  analysts_count = int(round(forecast_df[analyst_field].mean()))
237
  else:
 
267
  for metric, mapping in metrics.items():
268
  st.subheader(metric)
269
  st.write(
270
+ f"This chart shows {metric} over the selected time period. "
271
  "Bars represent historical data and lines represent forecast ranges. "
272
  "Hover over markers for details."
273
  )
274
  fig = create_plot(metric, mapping["historical"], mapping["forecast"])
275
  st.plotly_chart(fig, use_container_width=True)
276
 
 
277
  with st.expander(f"View {metric} Data", expanded=False):
278
  relevant_cols = []
279
  hc = mapping["historical"]