Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,17 @@
|
|
| 1 |
# 系統套件
|
| 2 |
import os
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
-
|
| 5 |
-
# 數據處理
|
| 6 |
import pandas as pd
|
| 7 |
import numpy as np
|
| 8 |
import yfinance as yf
|
| 9 |
-
|
| 10 |
-
# Dash & Plotly
|
| 11 |
from dash import Dash, dcc, html, callback
|
| 12 |
import dash
|
| 13 |
import plotly.express as px
|
| 14 |
import plotly.graph_objects as go
|
| 15 |
from plotly.subplots import make_subplots
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# 台股代號對應表 (移除台指期,因為它現在是獨立區塊)
|
| 18 |
TAIWAN_STOCKS = {
|
|
@@ -216,12 +215,44 @@ def calculate_volume_profile(df, num_bins=50):
|
|
| 216 |
|
| 217 |
return bin_edges, hist, price_centers
|
| 218 |
|
| 219 |
-
#
|
| 220 |
def get_business_climate_data():
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
# 新增: 模擬的PMI數據獲取函式
|
| 227 |
def get_pmi_data():
|
|
@@ -1025,7 +1056,7 @@ def update_business_climate_chart(selected_stock):
|
|
| 1025 |
fig = go.Figure()
|
| 1026 |
fig.add_annotation(
|
| 1027 |
x=0.5, y=0.5,
|
| 1028 |
-
text="無法載入景氣燈號資料<br
|
| 1029 |
xref="paper", yref="paper",
|
| 1030 |
showarrow=False,
|
| 1031 |
font=dict(size=14)
|
|
|
|
| 1 |
# 系統套件
|
| 2 |
import os
|
| 3 |
from datetime import datetime, timedelta
|
|
|
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
import numpy as np
|
| 6 |
import yfinance as yf
|
|
|
|
|
|
|
| 7 |
from dash import Dash, dcc, html, callback
|
| 8 |
import dash
|
| 9 |
import plotly.express as px
|
| 10 |
import plotly.graph_objects as go
|
| 11 |
from plotly.subplots import make_subplots
|
| 12 |
+
import re
|
| 13 |
+
from bs4 import BeautifulSoup
|
| 14 |
+
import requests
|
| 15 |
|
| 16 |
# 台股代號對應表 (移除台指期,因為它現在是獨立區塊)
|
| 17 |
TAIWAN_STOCKS = {
|
|
|
|
| 215 |
|
| 216 |
return bin_edges, hist, price_centers
|
| 217 |
|
| 218 |
+
# === 修正後的 get_business_climate_data 函式 ===
|
| 219 |
def get_business_climate_data():
|
| 220 |
+
"""從國發會網站抓取真實景氣燈號資料"""
|
| 221 |
+
try:
|
| 222 |
+
url = 'https://www.ndc.gov.tw/Content_List.aspx?n=84A4AFB5532512F5'
|
| 223 |
+
# 發送 GET 請求
|
| 224 |
+
response = requests.get(url, timeout=10)
|
| 225 |
+
response.raise_for_status() # 如果請求失敗,會拋出 HTTPError
|
| 226 |
+
|
| 227 |
+
# 使用 pandas 讀取 HTML 表格
|
| 228 |
+
df_list = pd.read_html(response.text, match='景氣對策信號綜合判斷分數及構成項目')
|
| 229 |
+
|
| 230 |
+
if not df_list:
|
| 231 |
+
print("未在網頁中找到景氣對策信號表格。")
|
| 232 |
+
return pd.DataFrame()
|
| 233 |
+
|
| 234 |
+
df = df_list[0]
|
| 235 |
+
|
| 236 |
+
# 數據清理
|
| 237 |
+
df.columns = df.iloc[0] # 將第一行作為標題
|
| 238 |
+
df = df.iloc[1:]
|
| 239 |
+
df = df.reset_index(drop=True)
|
| 240 |
+
|
| 241 |
+
# 重新命名欄位以便操作
|
| 242 |
+
df = df.rename(columns={'年月': 'Date', '綜合判斷分數': 'Index'})
|
| 243 |
+
|
| 244 |
+
# 格式化日期
|
| 245 |
+
df['Date'] = df['Date'].apply(lambda x: datetime.strptime(str(x), '%Y.%m'))
|
| 246 |
+
|
| 247 |
+
# 轉換數值
|
| 248 |
+
df['Index'] = pd.to_numeric(df['Index'], errors='coerce')
|
| 249 |
+
df = df.dropna(subset=['Index'])
|
| 250 |
+
|
| 251 |
+
return df.sort_values('Date', ascending=True)
|
| 252 |
+
|
| 253 |
+
except Exception as e:
|
| 254 |
+
print(f"Error fetching business climate data: {e}")
|
| 255 |
+
return pd.DataFrame()
|
| 256 |
|
| 257 |
# 新增: 模擬的PMI數據獲取函式
|
| 258 |
def get_pmi_data():
|
|
|
|
| 1056 |
fig = go.Figure()
|
| 1057 |
fig.add_annotation(
|
| 1058 |
x=0.5, y=0.5,
|
| 1059 |
+
text="無法載入景氣燈號資料<br>請確認網路連線或國發會網站是否可存取",
|
| 1060 |
xref="paper", yref="paper",
|
| 1061 |
showarrow=False,
|
| 1062 |
font=dict(size=14)
|