KKKMatthias commited on
Commit
a7bb0ed
·
verified ·
1 Parent(s): fb551cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -16
app.py CHANGED
@@ -1,7 +1,4 @@
1
  import os
2
- # 禁用 yfinance 的磁盘缓存
3
- os.environ['YF_NOCACHE'] = 'True'
4
-
5
  from fastapi import FastAPI
6
  import yfinance as yf
7
  import math
@@ -41,12 +38,8 @@ def get_stock(ticker: str):
41
  eps_est_current = None
42
  eps_est_next = None
43
 
44
- # --- 修复逻辑开始 ---
45
  if est is not None and not est.empty:
46
  try:
47
- # 打印一下索引看看结构 (调试用,生产环境可注释)
48
- # print(f"Index type: {est.index}")
49
-
50
  # 方法 A: 使用位置索引 iloc (更通用)
51
  # 假设数据按时间升序排列,第0行是当前年,第1行是明年
52
  # 注意:有时 yfinance 返回的顺序可能是反的,或者包含季度数据,
@@ -59,12 +52,10 @@ def get_stock(ticker: str):
59
 
60
  except Exception as e:
61
  print(f"Error parsing dataframe: {e}")
62
- # --- 修复逻辑结束 ---
63
 
64
  # 3. 如果从表格获取失败,使用 info 中的备用数据
65
- # forwardEps 通常代表下一财年的预测 EPS
66
- if eps_est_next is None and info:
67
- eps_est_next = info.get('forwardEps')
68
 
69
  if not info or len(info) < 5:
70
  return {"error": "No data found", "ticker": ticker}
@@ -73,14 +64,10 @@ def get_stock(ticker: str):
73
  "ticker": ticker.upper(),
74
  "pe_static": clean_num(info.get('trailingPE')),
75
  "pe_dynamic": clean_num(info.get('forwardPE')),
76
-
77
- # 优先显示从表格获取的数据,如果没有则显示 info 中的数据
78
  "eps_est_avg_current_year": clean_num(eps_est_current),
79
  "eps_est_avg_next_year": clean_num(eps_est_next),
80
 
81
- # 调试信息:告诉你数据来源是 info 还是 table
82
- "debug_note": "Data from table" if eps_est_current is not None else "Data from info/fallback",
83
- "source": "hf_space_v1"
84
  }
85
  except Exception as e:
86
  return {"error": str(e)}
 
1
  import os
 
 
 
2
  from fastapi import FastAPI
3
  import yfinance as yf
4
  import math
 
38
  eps_est_current = None
39
  eps_est_next = None
40
 
 
41
  if est is not None and not est.empty:
42
  try:
 
 
 
43
  # 方法 A: 使用位置索引 iloc (更通用)
44
  # 假设数据按时间升序排列,第0行是当前年,第1行是明年
45
  # 注意:有时 yfinance 返回的顺序可能是反的,或者包含季度数据,
 
52
 
53
  except Exception as e:
54
  print(f"Error parsing dataframe: {e}")
 
55
 
56
  # 3. 如果从表格获取失败,使用 info 中的备用数据
57
+ #if eps_est_next is None and info:
58
+ # eps_est_next = info.get('forwardEps')
 
59
 
60
  if not info or len(info) < 5:
61
  return {"error": "No data found", "ticker": ticker}
 
64
  "ticker": ticker.upper(),
65
  "pe_static": clean_num(info.get('trailingPE')),
66
  "pe_dynamic": clean_num(info.get('forwardPE')),
67
+
 
68
  "eps_est_avg_current_year": clean_num(eps_est_current),
69
  "eps_est_avg_next_year": clean_num(eps_est_next),
70
 
 
 
 
71
  }
72
  except Exception as e:
73
  return {"error": str(e)}