Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4ecc0ce
Create rmd.yml
tschm Apr 4, 2025
645f2b6
adding make instructions
jonathan-taylor Apr 9, 2025
ccdfb18
Merge branch 'intro-stat-learning:main' into rmd_action
tschm Apr 10, 2025
574d168
Prepare README for pointer to branch with Rmd files
tschm Apr 10, 2025
66ee42d
Remove RMD files
tschm Apr 10, 2025
3f22e58
link to branch (will only exist once the workflow did run once)
tschm Apr 10, 2025
d51c56b
docs: add tschm as a contributor for code (#54)
allcontributors[bot] Apr 10, 2025
bc4aab2
Merge branch 'intro-stat-learning:main' into rmd_action
tschm Apr 11, 2025
15f5dda
towards one data folder
tschm Apr 11, 2025
e0823da
towards one data folder
tschm Apr 11, 2025
fdd1992
Upload data folder
tschm Apr 11, 2025
f7d7153
Fix refs (#59)
jonathan-taylor Apr 11, 2025
9d46999
change metadata for controversial cell
tschm Apr 13, 2025
2f142fc
notebook with metadata for nbmake
tschm Apr 13, 2025
6e170a1
change MetaData for controversial cells following nbmake instructions
tschm Apr 13, 2025
9baac23
Merge remote-tracking branch 'origin/Jinja' into rmd_action
tschm Apr 13, 2025
9a62932
data/book_images problem
tschm Apr 13, 2025
7dfc3b4
data folder
tschm Apr 14, 2025
4600590
folder for rendered files
tschm Apr 15, 2025
fb15e0f
render files during ci/cd
tschm Apr 15, 2025
c0bd048
install fire
tschm Apr 15, 2025
0fa4d93
use latest nbformat
tschm Apr 15, 2025
c484ce4
install jinja2
tschm Apr 15, 2025
1a51eb2
test with notebook
tschm Apr 15, 2025
9469f34
notebooks test
tschm Apr 15, 2025
d774267
comments in Makefile
tschm May 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
"code",
"content"
]
},
{
"login": "tschm",
"name": "Thomas Schmelzer",
"avatar_url": "https://avatars.githubusercontent.com/u/2046079?v=4",
"profile": "https://github.com/tschm",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
84 changes: 84 additions & 0 deletions .github/scripts/render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
Jupyter Notebook Batch Renderer

Usage examples:
python render_notebook.py render config.json
python render_notebook.py render config.json --output_dir rendered
"""

from jinja2 import Environment, FileSystemLoader
import json
import fire
from pathlib import Path
from glob import glob


class NotebookRenderer:
def render(self, config_file, output_dir="rendered"):
"""
Render all Jupyter notebooks in current directory using configuration

Args:
config_file: Path to JSON configuration file
output_dir: Output directory path (default: "rendered")
"""
root = Path(__file__).parent.parent.parent
print(f"Root directory: {root}")

# Setup paths
config_path = root / config_file
output_path = root / output_dir

# Create output directory if it doesn't exist
output_path.mkdir(exist_ok=True)

# Validate config file exists
if not config_path.exists():
raise FileNotFoundError(f"Config file '{config_path}' not found")

# Load configuration
try:
with open(config_path, 'r') as f:
context = json.load(f)
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON in config file: {e}")
except Exception as e:
raise IOError(f"Error loading config file: {e}")

print(f"Loaded configuration: {context.keys()}")

# Setup Jinja2 environment
env = Environment(
loader=FileSystemLoader(root),
autoescape=False
)

# Find all .ipynb files in current directory
ipynb_files = glob(str(root / "*.ipynb"))
print(f"Found {len(ipynb_files)} notebook files to process")

# Process each file
success_count = 0
for file_path in ipynb_files:
try:
file_name = Path(file_path).name
print(f"\nProcessing: {file_name}")

template = env.get_template(file_name)
rendered_content = template.render(context)

output_file = output_path / file_name
with open(output_file, 'w') as f:
f.write(rendered_content)

print(f"Successfully rendered to: {output_file}")
success_count += 1
except Exception as e:
print(f"Error processing {file_name}: {str(e)}")
continue

print(f"\nFinished. Successfully rendered {success_count}/{len(ipynb_files)} notebooks")


if __name__ == '__main__':
fire.Fire(NotebookRenderer)
67 changes: 67 additions & 0 deletions .github/workflows/rmd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Convert Notebooks to RMarkdown
on:
push:
# paths:
# - '**.ipynb'
workflow_dispatch: # Allow manual triggering

permissions:
contents: write

jobs:
convert:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install jupyter, jupytext, fire, loguru, jinja2
run: |
python -m pip install --upgrade pip
pip install nbformat jupytext fire loguru jinja2

- name: Create artifact directory
run: |
mkdir -p artifacts/rmd
rm -rf artifacts/rmd/* # Clean any existing files

- name: Render all notebooks
run: |
python .github/scripts/render.py render config_rmd.json .

- name: Convert notebooks to RMarkdown
run: |
# Find all notebooks in the repository
NOTEBOOKS=$(find . -name "*.ipynb" -not -path "*/\.*" -not -path "*/artifacts/*")

# Convert each notebook
for nb in $NOTEBOOKS; do
echo "Converting $nb to Rmd..."
# Get the base name without path and extension
base_name=$(basename "$nb" .ipynb)

# Convert to Rmd in the artifacts directory
jupytext --to rmarkdown "$nb" --output "artifacts/rmd/${base_name}.Rmd"
done

- name: Copy data folder
run: |
cp -r data artifacts/rmd

#- name: Upload conversion results
# if: always()
# uses: actions/upload-artifact@v4
# with:
# name: rmd-files
# path: artifacts/rmd/
# retention-days: 1

- name: GitHub Pages action
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: rmd-files # The branch the action should deploy to.
folder: artifacts/rmd # The folder the action should deploy.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# path for rendered notebooks
rendered

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
Loading