Skip to content

Commit 1a63132

Browse files
authored
[MNT] test cookbook notebooks (#665)
Here we test all notebooks (e.g. we run them from top to bottom) with nbmake. nbmake is a package produced by our friend Alex Remedios, see https://github.com/treebeardtech/nbmake. Please note that we had to update the notebooks following a change in yfinance. We have replaced "Adj Close" with "Close".
1 parent da936af commit 1a63132

File tree

7 files changed

+10867
-10031
lines changed

7 files changed

+10867
-10031
lines changed

.github/actions/setup-project/action.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,11 @@ runs:
7979
else
8080
echo "No pyproject.toml found, skipping package installation"
8181
fi
82+
83+
- name: Show dependencies
84+
shell: bash
85+
run: uv pip list
86+
87+
- name: Show Python version
88+
shell: bash
89+
run: uv run python -c "import sys; print(sys.version)"

.github/workflows/notebooks.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: notebooks
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
15+
strategy:
16+
matrix:
17+
python-version: [ '3.10', '3.11', '3.12', '3.13', '3.14' ]
18+
fail-fast: false
19+
20+
steps:
21+
- uses: actions/checkout@v5
22+
23+
- name: Install uv
24+
uses: astral-sh/setup-uv@v7
25+
with:
26+
enable-cache: true
27+
28+
- name: Set up Python ${{ matrix.python-version }}
29+
uses: actions/setup-python@v6
30+
with:
31+
python-version: ${{ matrix.python-version }}
32+
33+
- name: Display Python version
34+
run: python -c "import sys; print(sys.version)"
35+
36+
- name: Install dependencies
37+
shell: bash
38+
run: uv pip install ".[dev,all_extras,notebook_test]" --no-cache-dir
39+
env:
40+
UV_SYSTEM_PYTHON: 1
41+
42+
- name: Show dependencies
43+
run: uv pip list
44+
45+
# Discover all notebooks
46+
- name: Collect notebooks
47+
id: notebooks
48+
shell: bash
49+
run: |
50+
NOTEBOOKS=$(find cookbook -name '*.ipynb' -print0 | xargs -0 echo)
51+
echo "notebooks=$NOTEBOOKS" >> $GITHUB_OUTPUT
52+
53+
# Run all discovered notebooks with nbmake
54+
- name: Test notebooks
55+
shell: bash
56+
run: |
57+
uv run pytest --reruns 3 --nbmake --nbmake-timeout=3600 -vv ${{ steps.notebooks.outputs.notebooks }}

cookbook/2-Mean-Variance-Optimisation.ipynb

Lines changed: 7371 additions & 6883 deletions
Large diffs are not rendered by default.

cookbook/3-Advanced-Mean-Variance-Optimisation.ipynb

Lines changed: 1363 additions & 1338 deletions
Large diffs are not rendered by default.

cookbook/4-Black-Litterman-Allocation.ipynb

Lines changed: 1286 additions & 1117 deletions
Large diffs are not rendered by default.

cookbook/5-Hierarchical-Risk-Parity.ipynb

Lines changed: 776 additions & 693 deletions
Large diffs are not rendered by default.

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ dev = [
6969
"isort",
7070
]
7171

72+
# notebook tests
73+
notebook_test = [
74+
"nbmake",
75+
"pytest-rerunfailures",
76+
]
77+
7278
[project.urls]
7379
Documentation = "https://pyportfolioopt.readthedocs.io/en/latest/"
7480
Download = "https://pypi.org/project/pyportfolioopt/#files"

0 commit comments

Comments
 (0)