Skip to content
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

[WASM] Add WASM lectures config and CI testing #560

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Check WASM
on: [pull_request]
jobs:
preview:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install required libraries
shell: bash -l {0}
run: |
pip install pyodide-py jupytext PyYAML
# Soft check on CI to check the WASM compatibility.
- name: Check WASM lectures
shell: bash -l {0}
run: |
python testing/check_wasm.py
68 changes: 68 additions & 0 deletions testing/check_wasm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Testing script to check whether the lectures added in
# wasm_compatible.yml are compatible with WASM.
# This is a soft-check check based on the imports present.
# It is still recommended to test the lecture on the
# https://github.com/QuantEcon/project.lecture-wasm

import os
import yaml
import jupytext

from pyodide.code import find_imports

# This is the list of imports (libraries) that are not supported
# WASM pyodide kernel in Jupyterlite.
UNSUPPORTED_LIBS = {
'quantecon',
'numba',
}

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(CURRENT_DIR)
LECTURES_DIR = os.path.join(ROOT_DIR, "lectures")
CONFIG_FILE = os.path.join(ROOT_DIR, "wasm_compatible.yml")


def get_wasm_compatible_files():
"""
Get the list of lectures names from the config file.
"""
# Load the YAML configuration
with open(CONFIG_FILE, "r") as file:
config = yaml.safe_load(file)

return config.get("lectures", [])


def convert_md_to_py_string(md_file_path):
"""
Convert a .md(myst) file to Python code string without creating a .py file.

Args:
md_file_path (str): Path to the Markdown file (.md)

Returns:
str: Python code as a string
"""
# Read the markdown file as a Jupytext notebook
with open(md_file_path, 'r', encoding='utf-8') as md_file:
notebook = jupytext.read(md_file)

# Convert the notebook to Python script format
py_code = jupytext.writes(notebook, fmt="py")

return py_code


def test_compatibility():
file_names = get_wasm_compatible_files()
for file_name in file_names:
py_string = convert_md_to_py_string(os.path.join(LECTURES_DIR, file_name + ".md"))
file_imports = find_imports(py_string)
for file_import in file_imports:
if file_import in UNSUPPORTED_LIBS:
error = 'import `{}` in lecture `{}` is not supported by WASM'.format(file_import, file_name)
raise ValueError(error)

if __name__ == "__main__":
test_compatibility()
4 changes: 4 additions & 0 deletions wasm_compatible.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Config file for adding lectures that are WASM compatible.
# Please add the lecture file names without `.md` extension.
lectures:
- cobweb
Loading