Skip to content

Commit

Permalink
Update workflow to use local notebooks repo
Browse files Browse the repository at this point in the history
add mypy checker
  • Loading branch information
Max Shkutnyk committed Jan 26, 2025
1 parent af65cb5 commit 93d7667
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 37 deletions.
49 changes: 24 additions & 25 deletions .github/workflows/build-cookbooks.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
name: generate-mdx-files

on:
workflow_dispatch:
inputs:
branch:
description: 'Branch to run the workflow on'
required: true
default: 'main'
pull_request:
branches:
- main
paths:
- 'fern/pages/cookbooks/**/*.mdx'

jobs:
run:
Expand All @@ -20,30 +19,40 @@ jobs:
with:
ref: ${{ github.event.inputs.branch }}

- name: Clone Notebooks Repository
shell: bash
run: |
git clone git@github.com:cohere-ai/notebooks.git notebooks
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install Poetry
shell: bash
run: |
pip install poetry # Using pip to install Poetry
- name: Bootstrap poetry
uses: snok/install-poetry@v1
with:
version: 1.5.1
virtualenvs-in-project: false

- name: Install Project Dependencies with Poetry
shell: bash
run: |
poetry install
- name: Check for mypy in pyproject.toml
id: check_mypy
run: |
if grep -q 'mypy' pyproject.toml; then
echo "mypy is specified in pyproject.toml, running..."
poetry run mypy .
echo "::set-output name=mypy_run::true"
else
echo "mypy is not specified in pyproject.toml, exiting."
echo "::set-output name=mypy_run::false"
fi
- name: Build MDX Files
if: steps.check_mypy.outputs.mypy_run == 'true'
shell: bash
run: poetry run python .github/scripts/build_cookbooks.py


- name: Check for Changes in build-mdx Folder
id: check_changes
shell: bash
Expand All @@ -60,13 +69,3 @@ jobs:
git config user.email "actions@github.com"
git commit -m "Update cookbooks"
git push origin HEAD
- name: Create Pull Request
if: steps.check_changes.outputs.changes_detected == 'true'
uses: peter-evans/create-pull-request@v5
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: "Update Cookbooks files"
branch: "update-mdx-from-cookbooks"
title: "Update Cookbooks"
body: "This PR updates the Cookbooks MDX files generated from the latest notebooks."
60 changes: 59 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ nbconvert = "^7.16.4"
jinja2 = "^3.1.4"
pyyaml = "^6.0.2"
python-frontmatter = "^1.1.0"
mypy = "^1.14.1"


[build-system]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from base64 import b64encode


BASE_PATH = Path(__file__).resolve().parent
NOTEBOOKS_REPO_PATH = BASE_PATH / "notebooks"
COOKBOOKS_PATH = BASE_PATH / "../../fern/pages/cookbooks"
REGISTRY_FILE = BASE_PATH / "registry.yaml"
AUTHORS_FILE = BASE_PATH / "authors.yaml"
TEMPLATES_PATH = BASE_PATH / "templates"
BASE_PATH = Path(__file__).resolve().parents[2]
COOKBOOKS_PATH = BASE_PATH / "fern/pages/cookbooks"
BUILD_COOKBOOKS_PATH = BASE_PATH / "scripts/build-cookbooks"
REGISTRY_FILE = BUILD_COOKBOOKS_PATH / "registry.yaml"
AUTHORS_FILE = BUILD_COOKBOOKS_PATH / "authors.yaml"
TEMPLATES_PATH = BUILD_COOKBOOKS_PATH / "templates"
MARKDOWN_IMAGE_IMPORT_PATTERN = re.compile(r"!\[(.*?)\]\((.*?)\)")
TITLE_PATTERN = re.compile(r"(?m)^#\s.*\n", re.MULTILINE)
SCRIPT_PATTERN = re.compile(r"<script.*?</script>", re.DOTALL)
Expand All @@ -22,9 +22,8 @@
def _format_bytes_as_base64(data: bytes) -> str:
return b64encode(data).decode("utf-8")


env = Environment(loader=FileSystemLoader(TEMPLATES_PATH))
template = env.get_template('cookbook.md') # Assuming the template name is cookbook.md
template = env.get_template('cookbook.md')

def _post_process(body: str, resources: dict[str]) -> str:
"""Perform any transformations to the generated body Markdown."""
Expand Down Expand Up @@ -69,7 +68,6 @@ def build_cookbooks():
continue

author_email_list = entry.get("authors", list())
print(author_email_list)
authors = [
dict(
name=_authors[email]["name"],
Expand All @@ -80,7 +78,7 @@ def build_cookbooks():
if email in _authors
]

notebook_path = NOTEBOOKS_REPO_PATH / entry["path"]
notebook_path = BASE_PATH / entry["path"]
with open(notebook_path, 'r') as f:
notebook_content = nbformat.read(f, as_version=4)
body, resources = md_exporter.from_notebook_node(notebook_content)
Expand Down Expand Up @@ -119,7 +117,6 @@ def build_cookbooks():
# Write to file
with open(output_file_path, 'w', encoding='utf-8') as file:
file.write(content)
print(f"Updated or Created {output_file_path}")

if __name__ == "__main__":
build_cookbooks()
File renamed without changes.
File renamed without changes.

0 comments on commit 93d7667

Please sign in to comment.