Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -116,11 +116,13 @@ def get_7day_stock_plot(symbol):
|
|
| 116 |
try:
|
| 117 |
yf_symbol = adjust_symbol_for_yfinance(symbol)
|
| 118 |
end_date = datetime.datetime.today()
|
| 119 |
-
start_date = end_date - datetime.timedelta(days=14) #
|
| 120 |
df = yf.download(yf_symbol, start=start_date.strftime('%Y-%m-%d'), end=end_date.strftime('%Y-%m-%d'), progress=False)
|
| 121 |
-
|
|
|
|
| 122 |
return "<p class='text-danger'>📊 無法取得股價趨勢圖。</p>"
|
| 123 |
-
|
|
|
|
| 124 |
dates = df.index.strftime('%Y-%m-%d').tolist()
|
| 125 |
closes = df['Close'].round(2).tolist()
|
| 126 |
|
|
@@ -128,16 +130,17 @@ def get_7day_stock_plot(symbol):
|
|
| 128 |
fig.add_trace(go.Scatter(x=dates, y=closes, mode='lines+markers', name='Close Price'))
|
| 129 |
fig.update_layout(
|
| 130 |
title=f"📉 {symbol} 最近7日收盤價走勢 / 7-Day Closing Price Trend",
|
| 131 |
-
xaxis_title="日期",
|
| 132 |
-
yaxis_title="
|
| 133 |
template="plotly_white",
|
| 134 |
height=400
|
| 135 |
)
|
| 136 |
-
return fig.to_html(full_html=False)
|
| 137 |
except Exception as e:
|
| 138 |
-
print(f"Error
|
| 139 |
return "<p class='text-danger'>📊 無法取得股價趨勢圖。</p>"
|
| 140 |
|
|
|
|
| 141 |
@app.route("/", methods=["GET", "POST"])
|
| 142 |
def index():
|
| 143 |
result = {}
|
|
|
|
| 116 |
try:
|
| 117 |
yf_symbol = adjust_symbol_for_yfinance(symbol)
|
| 118 |
end_date = datetime.datetime.today()
|
| 119 |
+
start_date = end_date - datetime.timedelta(days=14) # account for weekends
|
| 120 |
df = yf.download(yf_symbol, start=start_date.strftime('%Y-%m-%d'), end=end_date.strftime('%Y-%m-%d'), progress=False)
|
| 121 |
+
|
| 122 |
+
if df.empty or 'Close' not in df.columns:
|
| 123 |
return "<p class='text-danger'>📊 無法取得股價趨勢圖。</p>"
|
| 124 |
+
|
| 125 |
+
df = df.tail(7) # recent 7 trading days
|
| 126 |
dates = df.index.strftime('%Y-%m-%d').tolist()
|
| 127 |
closes = df['Close'].round(2).tolist()
|
| 128 |
|
|
|
|
| 130 |
fig.add_trace(go.Scatter(x=dates, y=closes, mode='lines+markers', name='Close Price'))
|
| 131 |
fig.update_layout(
|
| 132 |
title=f"📉 {symbol} 最近7日收盤價走勢 / 7-Day Closing Price Trend",
|
| 133 |
+
xaxis_title="日期 / Date",
|
| 134 |
+
yaxis_title="收盤價 (USD)",
|
| 135 |
template="plotly_white",
|
| 136 |
height=400
|
| 137 |
)
|
| 138 |
+
return fig.to_html(full_html=False, include_plotlyjs='cdn', default_height="400px", default_width="100%")
|
| 139 |
except Exception as e:
|
| 140 |
+
print(f"[Plot Error] Failed to generate 7-day stock plot for {symbol}: {e}")
|
| 141 |
return "<p class='text-danger'>📊 無法取得股價趨勢圖。</p>"
|
| 142 |
|
| 143 |
+
|
| 144 |
@app.route("/", methods=["GET", "POST"])
|
| 145 |
def index():
|
| 146 |
result = {}
|