Faham commited on
Commit
79476b6
Β·
1 Parent(s): 523ea93

FIX: issue with ui re redering

Browse files
Files changed (2) hide show
  1. Home.py +10 -22
  2. stock_data_server.py +2 -1
Home.py CHANGED
@@ -408,6 +408,7 @@ def calculate_rsi(data, window):
408
  return rsi
409
 
410
 
 
411
  def create_stock_chart(ticker: str):
412
  """Create an interactive stock price chart with Ridge Regression predictions for the given ticker."""
413
  try:
@@ -961,6 +962,7 @@ def create_stock_chart(ticker: str):
961
  return create_basic_stock_chart(ticker)
962
 
963
 
 
964
  def create_basic_stock_chart(ticker: str):
965
  """Create a basic stock price chart without Prophet predictions."""
966
  try:
@@ -1209,6 +1211,7 @@ async def run_agent(user_query, selected_ticker):
1209
  return "Please try again with a different question."
1210
 
1211
 
 
1212
  def display_top_news(ticker: str):
1213
  """Display top news headlines for the given ticker with clickable links."""
1214
  try:
@@ -1468,32 +1471,17 @@ def main():
1468
 
1469
  with col1:
1470
  st.subheader("πŸ“ˆ Stock Price Chart")
1471
- # Cache the chart to prevent rerendering
1472
- chart_key = f"chart_{selected_ticker}"
1473
- if chart_key not in st.session_state:
1474
- with st.spinner(f"πŸ“Š Loading chart for {selected_ticker}..."):
1475
- chart_fig = create_stock_chart(selected_ticker)
1476
- if chart_fig:
1477
- st.session_state[chart_key] = chart_fig
1478
- else:
1479
- st.session_state[chart_key] = None
1480
-
1481
- # Display the cached chart
1482
- if st.session_state[chart_key]:
1483
- st.plotly_chart(st.session_state[chart_key], use_container_width=True)
1484
  else:
1485
  st.warning(f"Could not load chart for {selected_ticker}")
1486
 
1487
  with col2:
1488
  st.subheader("πŸ“° Top News")
1489
- # Cache the news to prevent rerendering
1490
- news_key = f"news_{selected_ticker}"
1491
- if news_key not in st.session_state:
1492
- st.session_state[news_key] = True # Mark as loaded
1493
- display_top_news(selected_ticker)
1494
- else:
1495
- # Re-display cached news without reloading
1496
- display_top_news(selected_ticker)
1497
 
1498
  # Chat Section
1499
  st.header("πŸ’¬ Chat with Financial Agent")
@@ -1541,7 +1529,7 @@ def main():
1541
  {"role": "assistant", "content": response}
1542
  )
1543
 
1544
- # Rerun to display the new message (charts and news are cached)
1545
  st.rerun()
1546
 
1547
 
 
408
  return rsi
409
 
410
 
411
+ @st.cache_data(ttl=3600) # Cache for 1 hour
412
  def create_stock_chart(ticker: str):
413
  """Create an interactive stock price chart with Ridge Regression predictions for the given ticker."""
414
  try:
 
962
  return create_basic_stock_chart(ticker)
963
 
964
 
965
+ @st.cache_data(ttl=3600) # Cache for 1 hour
966
  def create_basic_stock_chart(ticker: str):
967
  """Create a basic stock price chart without Prophet predictions."""
968
  try:
 
1211
  return "Please try again with a different question."
1212
 
1213
 
1214
+ @st.cache_data(ttl=1800) # Cache for 30 minutes
1215
  def display_top_news(ticker: str):
1216
  """Display top news headlines for the given ticker with clickable links."""
1217
  try:
 
1471
 
1472
  with col1:
1473
  st.subheader("πŸ“ˆ Stock Price Chart")
1474
+ # Always create the chart - it's cached by the function itself
1475
+ chart_fig = create_stock_chart(selected_ticker)
1476
+ if chart_fig:
1477
+ st.plotly_chart(chart_fig, use_container_width=True)
 
 
 
 
 
 
 
 
 
1478
  else:
1479
  st.warning(f"Could not load chart for {selected_ticker}")
1480
 
1481
  with col2:
1482
  st.subheader("πŸ“° Top News")
1483
+ # Display news - it's cached by the function
1484
+ display_top_news(selected_ticker)
 
 
 
 
 
 
1485
 
1486
  # Chat Section
1487
  st.header("πŸ’¬ Chat with Financial Agent")
 
1529
  {"role": "assistant", "content": response}
1530
  )
1531
 
1532
+ # Rerun to display the new message - the chart and news are cached in session state
1533
  st.rerun()
1534
 
1535
 
stock_data_server.py CHANGED
@@ -23,7 +23,8 @@ def get_historical_stock_data(ticker: str) -> str:
23
  return f"No stock data found for ticker {ticker}."
24
 
25
  # Return the last 5 days of data as a clean string
26
- return f"Recent stock data for {ticker}:\n{hist_data.tail().to_string()}"
 
27
  except Exception as e:
28
  return f"An error occurred while fetching stock data: {e}"
29
 
 
23
  return f"No stock data found for ticker {ticker}."
24
 
25
  # Return the last 5 days of data as a clean string
26
+ # return f"Recent stock data for {ticker}:\n{hist_data.tail().to_string()}"
27
+ return f"Recent stock data for {ticker}:\n{hist_data.to_string()}"
28
  except Exception as e:
29
  return f"An error occurred while fetching stock data: {e}"
30