2025年12月24日 星期三

K 線圖套件 kbar 升版 v0.1.5

這兩天在樹莓派 Pi 3B 與 3A+ 這兩塊主機的 Bulleye OS 上安裝套件時都在安裝 kbar 的依賴套件 pyarrow 時失敗, 再次詢問 ChatGPT 才知道根本不需要將 pyarrow 列入依賴套件, 因為 kbar 原始碼既沒有 import pyarrow, 用來處理資料的 pandas 與畫 K 線的 mplfinance 也沒用到, 為何當時 ChatGPT 幫我寫  project.toml 時會列入 pyarrow? 為了拿掉 pyarrow 依賴只好升版為 v0.1.5 (同時也把版本上限加回去避免依賴套件大升版時出問題). 本系列之前文張參考 :



1. 修改 project.toml :  

黃底色部分為修改之處 : 

[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "kbar"
version = "0.1.5"
description = "A lightweight K-line (candlestick) plotting tool with matplotlib and mplfinance."
readme = "README.md"
requires-python = ">=3.9"
license = { file = "LICENSE" }
authors = [
  { name = "Tony Y.H. Huang" }
]
keywords = ["finance", "candlestick", "mplfinance", "k-line", "stock"]
classifiers = [
  "Development Status :: 3 - Alpha",
  "Intended Audience :: Developers",
  "Intended Audience :: Financial and Insurance Industry",
  "Programming Language :: Python :: 3",
  "License :: OSI Approved :: MIT License",
  "Operating System :: OS Independent"
]


dependencies = [
  "numpy>=1.23,<3.0",
  "pandas>=2.0,<3.0",
  "matplotlib>=3.7,<4.0",
  "mplfinance>=0.12.10b0,<1.0",
]

[project.optional-dependencies]
dev = [
  "pytest>=8.0",
  "coverage>=7.0"
]

[tool.pytest.ini_options]
addopts = "-v --maxfail=3 --disable-warnings"
testpaths = ["tests"]

[tool.coverage.run]
branch = true
source = ["kbar"]

[tool.coverage.report]
show_missing = true
skip_covered = true

[tool.setuptools.packages.find]
where = ["src"]  

[project.urls]
"Homepage" = "https://github.com/tony1966/kbar/"
"Bug Tracker" = "https://github.com/tony1966/kbar/issues"
"Documentation" = "https://github.com/tony1966/kbar/#readme"


2. 修改 src/kbar/__init__.py :

修改版本號碼 :

"""
KBar: A lightweight stock candlestick plotting utility with Chinese font auto-detection.
"""

from .kbar import KBar, detect_font

__all__ = ["KBar", "detect_font"]
__version__ = "0.1.5"


3. 修改版本異動記錄檔 CHANGELOG.md : 

修改為 :

# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),  
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

---

## [0.1.5] - 2025-12-24

### Changed
- 更新依賴,增加上限限制:
  - `numpy>=1.23,<3.0`
  - `pandas>=2.0,<3.0`
  - `matplotlib>=3.7,<4.0`
  - `mplfinance>=0.12.10b0,<1.0`
- 移除對 `pyarrow` 的依賴。
- 修正 `addplot()` 測試錯誤。
- 更新 `README.md` 說明與範例。

---

## [0.1.4] - 2025-11-22

### Changed
- 初版限制:
  - `numpy<2`
  - `pandas<3.0`
  - `matplotlib<4.0`
  - `pyarrow>=14.0.2`

---


4. 清理專案資料夾 & Build 專案 : 

刪除專案資料夾下前次 Build 子目錄 :
  • __pycache__ : 存放 Python 產生的 bytecode 暫存檔的目錄
  • build : 執行 python -m build 生成的中間建構資料夾
  • dist : 上次打包生成的 wheel 與 sdist
  • *.egg-info : setuptools 生成的套件資訊資料夾 (在 src 資料夾下)
用 tree 指令查看是否乾淨 :

D:\PyPi>tree kbar /f  
列出磁碟區 新增磁碟區 的資料夾 PATH
磁碟區序號為 1258-16B8
D:\PYPI\KBAR
│  .gitignore
│  CHANGELOG.md
│  LICENSE
│  pyproject.toml
│  README.md
├─src
│  └─kbar
│          kbar.py
│          __init__.py
└─tests
        test_kbar.py
        __init__.py


5. 打包專案 : 

用 python -m build 指令打包專案 : 

D:\PyPi>cd kbar

D:\PyPi\kbar>python -m build
* Creating isolated environment: venv+pip...
* Installing packages in isolated environment:
  - setuptools>=61.0
  - wheel
* Getting build dependencies for sdist...
C:\Users\tony1\AppData\Local\Temp\build-env-zqe2az72\lib\site-packages\setuptools\config\_apply_pyprojecttoml.py:82: SetuptoolsDeprecationWarning: `project.license` as a TOML table is deprecated
!!
... (略) ...

running install
running install_lib
creating build\bdist.win-amd64\wheel
creating build\bdist.win-amd64\wheel\kbar
copying build\lib\kbar\kbar.py -> build\bdist.win-amd64\wheel\.\kbar
copying build\lib\kbar\__init__.py -> build\bdist.win-amd64\wheel\.\kbar
running install_egg_info
Copying src\kbar.egg-info to build\bdist.win-amd64\wheel\.\kbar-0.1.5-py3.10.egg-info
running install_scripts
creating build\bdist.win-amd64\wheel\kbar-0.1.5.dist-info\WHEEL
creating 'D:\PyPi\kbar\dist\.tmp-j3a6fidi\kbar-0.1.5-py3-none-any.whl' and adding 'build\bdist.win-amd64\wheel' to it
adding 'kbar/__init__.py'
adding 'kbar/kbar.py'
adding 'kbar-0.1.5.dist-info/licenses/LICENSE'
adding 'kbar-0.1.5.dist-info/METADATA'
adding 'kbar-0.1.5.dist-info/WHEEL'
adding 'kbar-0.1.5.dist-info/top_level.txt'
adding 'kbar-0.1.5.dist-info/RECORD'
removing build\bdist.win-amd64\wheel
Successfully built kbar-0.1.5.tar.gz and kbar-0.1.5-py3-none-any.whl

用 tree 指令檢視專案目錄 : 

D:\PyPi\kbar>cd ..  
D:\PyPi>tree kbar /f     
列出磁碟區 新增磁碟區 的資料夾 PATH
磁碟區序號為 1258-16B8
D:\PYPI\KBAR
│  .gitignore
│  CHANGELOG.md
│  LICENSE
│  pyproject.toml
│  README.md
├─dist
│      kbar-0.1.5-py3-none-any.whl
│      kbar-0.1.5.tar.gz
├─src
│  ├─kbar
│  │      kbar.py
│  │      __init__.py
│  │
│  └─kbar.egg-info
│          dependency_links.txt
│          PKG-INFO
│          requires.txt
│          SOURCES.txt
│          top_level.txt
└─tests
        test_kbar.py
        __init__.py

可見已產生打包好的 whl 檔與 gz 原始碼壓縮檔. 


6. 安裝 whl 檔進行發佈前測試 :  

先解除安裝舊版 kbar :

D:\PyPi>pip uninstall kbar   
Found existing installation: kbar 0.1.4
Uninstalling kbar-0.1.4:
  Would remove:
    c:\users\tony1\appdata\local\programs\thonny\lib\site-packages\kbar-0.1.4.dist-info\*
    c:\users\tony1\appdata\local\programs\thonny\lib\site-packages\kbar\*
Proceed (Y/n)? y
  Successfully uninstalled kbar-0.1.4

從 whl 檔安裝新版 v0.1.5 版 kbar : 

D:\PyPi>cd kbar  

D:\PyPi\kbar>pip install dist\kbar-0.1.5-py3-none-any.whl  
Processing d:\pypi\kbar\dist\kbar-0.1.5-py3-none-any.whl
Requirement already satisfied: numpy<3.0,>=1.23 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from kbar==0.1.5) (1.26.4)
Requirement already satisfied: pandas<3.0,>=2.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from kbar==0.1.5) (2.3.1)
Requirement already satisfied: matplotlib<4.0,>=3.7 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from kbar==0.1.5) (3.10.5)
Requirement already satisfied: mplfinance<1.0,>=0.12.10b0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from kbar==0.1.5) (0.12.10b0)
Requirement already satisfied: contourpy>=1.0.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.5) (1.3.2)
Requirement already satisfied: cycler>=0.10 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.5) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.5) (4.59.1)
Requirement already satisfied: kiwisolver>=1.3.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.5) (1.4.9)
Requirement already satisfied: packaging>=20.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.5) (25.0)
Requirement already satisfied: pillow>=8 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.5) (11.3.0)
Requirement already satisfied: pyparsing>=2.3.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.5) (3.2.3)
Requirement already satisfied: python-dateutil>=2.7 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.5) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from pandas<3.0,>=2.0->kbar==0.1.5) (2025.2)
Requirement already satisfied: tzdata>=2022.7 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from pandas<3.0,>=2.0->kbar==0.1.5) (2025.2)
Requirement already satisfied: six>=1.5 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from python-dateutil>=2.7->matplotlib<4.0,>=3.7->kbar==0.1.5) (1.17.0)
Installing collected packages: kbar
Successfully installed kbar-0.1.5

進入 REPL 檢視 kbar 的 __version__ 屬性 : 

d:\PyPi\kbar>python     
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec  7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import kbar     
>>> kbar.__version__    
'0.1.5'

用 kbar 繪製 K 線圖 : 

# kbar_test.py
from kbar import KBar   
import yfinance as yf   
df=yf.download('0050.TW', start='2024-07-01', end='2024-08-21', auto_adjust=False)   
df.columns=df.columns.map(lambda x: x[0])    # 改成舊版單層索引
kb=KBar(df)    # 未傳 font 參數預設使用正黑體
kb.plot(title='台灣五十(0050.TW)', volume=True)




7. 發佈到 PyPI 進行升版 :  

利用 twine 套件將專案上傳到 PyPI : 

D:\PyPi\kbar>python -m twine upload dist/*    
Uploading distributions to https://upload.pypi.org/legacy/
Enter your API token:   (按 Ctrl+V 貼上 PyPI API Token 按 Enter )
Uploading kbar-0.1.5-py3-none-any.whl
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.2/17.2 kB • 00:00 • ?
Uploading kbar-0.1.5.tar.gz
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 19.2/19.2 kB • 00:00 • ?

View at:




然後到 Pi 3 主機安裝 kbar : 

pi@kaopi3:~ $ pip install kbar
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting kbar
  Using cached kbar-0.1.5-py3-none-any.whl.metadata (6.1 kB)
Requirement already satisfied: numpy<3.0,>=1.23 in ./.local/lib/python3.9/site-packages (from kbar) (1.26.4)
Requirement already satisfied: pandas<3.0,>=2.0 in ./.local/lib/python3.9/site-packages (from kbar) (2.0.3)
Requirement already satisfied: matplotlib<4.0,>=3.7 in ./.local/lib/python3.9/site-packages (from kbar) (3.9.4)
Requirement already satisfied: mplfinance<1.0,>=0.12.10b0 in ./.local/lib/python3.9/site-packages (from kbar) (0.12.10b0)
Requirement already satisfied: contourpy>=1.0.1 in ./.local/lib/python3.9/site-packages (from matplotlib<4.0,>=3.7->kbar) (1.3.0)
Requirement already satisfied: cycler>=0.10 in ./.local/lib/python3.9/site-packages (from matplotlib<4.0,>=3.7->kbar) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in ./.local/lib/python3.9/site-packages (from matplotlib<4.0,>=3.7->kbar) (4.60.2)
Requirement already satisfied: kiwisolver>=1.3.1 in ./.local/lib/python3.9/site-packages (from matplotlib<4.0,>=3.7->kbar) (1.4.7)
Requirement already satisfied: packaging>=20.0 in ./.local/lib/python3.9/site-packages (from matplotlib<4.0,>=3.7->kbar) (25.0)
Requirement already satisfied: pillow>=8 in /usr/lib/python3/dist-packages (from matplotlib<4.0,>=3.7->kbar) (8.1.2)
Requirement already satisfied: pyparsing>=2.3.1 in ./.local/lib/python3.9/site-packages (from matplotlib<4.0,>=3.7->kbar) (3.3.1)
Requirement already satisfied: python-dateutil>=2.7 in ./.local/lib/python3.9/site-packages (from matplotlib<4.0,>=3.7->kbar) (2.9.0.post0)
Requirement already satisfied: importlib-resources>=3.2.0 in ./.local/lib/python3.9/site-packages (from matplotlib<4.0,>=3.7->kbar) (6.5.2)
Requirement already satisfied: pytz>=2020.1 in ./.local/lib/python3.9/site-packages (from pandas<3.0,>=2.0->kbar) (2025.2)
Requirement already satisfied: tzdata>=2022.1 in ./.local/lib/python3.9/site-packages (from pandas<3.0,>=2.0->kbar) (2025.3)
Requirement already satisfied: zipp>=3.1.0 in ./.local/lib/python3.9/site-packages (from importlib-resources>=3.2.0->matplotlib<4.0,>=3.7->kbar) (3.23.0)
Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.7->matplotlib<4.0,>=3.7->kbar) (1.16.0)
Using cached kbar-0.1.5-py3-none-any.whl (7.9 kB)
Installing collected packages: kbar
Successfully installed kbar-0.1.5

少了不需要的 pyarrow 很快就把 kbar 裝好了. 

升版完成收工. 

沒有留言 :