Skip to content

Create rmd.yml #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
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