From 1d020e22f12e98377a5e536f5e91e37b54c71195 Mon Sep 17 00:00:00 2001 From: kp992 Date: Sun, 1 Dec 2024 13:07:59 -0800 Subject: [PATCH 1/5] Add wasm config for adding lectures that are compatible --- wasm_compatible.yml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 wasm_compatible.yml diff --git a/wasm_compatible.yml b/wasm_compatible.yml new file mode 100644 index 00000000..6ad1b971 --- /dev/null +++ b/wasm_compatible.yml @@ -0,0 +1,4 @@ +# Config file for adding lectures that are WASM compatible. +# Please add the lecture file names without `.md` extension. +lectures: + - cobweb \ No newline at end of file From 47968d99dca3c0841c71e13d0b20e9e2f83a9cb5 Mon Sep 17 00:00:00 2001 From: kp992 Date: Sun, 1 Dec 2024 13:11:38 -0800 Subject: [PATCH 2/5] Update environment file --- environment.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/environment.yml b/environment.yml index f453b6d5..4b154a9d 100644 --- a/environment.yml +++ b/environment.yml @@ -18,6 +18,8 @@ dependencies: - sphinxcontrib-youtube==1.1.0 - sphinx-togglebutton==0.3.1 - sphinx_reredirects==0.1.3 + - pyodide-py + - jupytext # Sandpit Requirements # - PuLP # - cvxpy From 5eb4dd39325f52883b6989e29b098394dd480407 Mon Sep 17 00:00:00 2001 From: kp992 Date: Sun, 1 Dec 2024 13:11:48 -0800 Subject: [PATCH 3/5] Add testing script in CI --- .github/workflows/ci.yml | 5 +++ testing/check_wasm.py | 68 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 testing/check_wasm.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 74d2957b..e7678893 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -71,6 +71,11 @@ jobs: run: | rm -r _build/.doctrees jb build lectures --path-output ./ -nW --keep-going + # Soft check on CI to check the WASM compatibility. + - name: Check WASM lectures + shell: bash -l {0} + run: | + python testing/check_wasm.py - name: Upload Execution Reports (HTML) uses: actions/upload-artifact@v4 if: failure() diff --git a/testing/check_wasm.py b/testing/check_wasm.py new file mode 100644 index 00000000..595b9ad2 --- /dev/null +++ b/testing/check_wasm.py @@ -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() From 0bb7cef645109c4ab49c0d516d4665b8c0a25388 Mon Sep 17 00:00:00 2001 From: kp992 Date: Sun, 8 Dec 2024 17:48:56 -0800 Subject: [PATCH 4/5] Setup WASM testing in new yml file --- .github/workflows/ci.yml | 5 ----- .github/workflows/wasm.yml | 21 +++++++++++++++++++++ environment.yml | 2 -- wasm_compatible.yml | 2 +- 4 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/wasm.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e7678893..74d2957b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -71,11 +71,6 @@ jobs: run: | rm -r _build/.doctrees jb build lectures --path-output ./ -nW --keep-going - # Soft check on CI to check the WASM compatibility. - - name: Check WASM lectures - shell: bash -l {0} - run: | - python testing/check_wasm.py - name: Upload Execution Reports (HTML) uses: actions/upload-artifact@v4 if: failure() diff --git a/.github/workflows/wasm.yml b/.github/workflows/wasm.yml new file mode 100644 index 00000000..a134c974 --- /dev/null +++ b/.github/workflows/wasm.yml @@ -0,0 +1,21 @@ +name: Build HTML [using jupyter-book] +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 diff --git a/environment.yml b/environment.yml index 4b154a9d..f453b6d5 100644 --- a/environment.yml +++ b/environment.yml @@ -18,8 +18,6 @@ dependencies: - sphinxcontrib-youtube==1.1.0 - sphinx-togglebutton==0.3.1 - sphinx_reredirects==0.1.3 - - pyodide-py - - jupytext # Sandpit Requirements # - PuLP # - cvxpy diff --git a/wasm_compatible.yml b/wasm_compatible.yml index 6ad1b971..7a3d5844 100644 --- a/wasm_compatible.yml +++ b/wasm_compatible.yml @@ -1,4 +1,4 @@ # Config file for adding lectures that are WASM compatible. # Please add the lecture file names without `.md` extension. lectures: - - cobweb \ No newline at end of file + - cobweb From 9b274075184c04de811228321c6a5f766ad695da Mon Sep 17 00:00:00 2001 From: kp992 Date: Sun, 8 Dec 2024 17:56:11 -0800 Subject: [PATCH 5/5] rename --- .github/workflows/wasm.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/wasm.yml b/.github/workflows/wasm.yml index a134c974..36b40e04 100644 --- a/.github/workflows/wasm.yml +++ b/.github/workflows/wasm.yml @@ -1,4 +1,4 @@ -name: Build HTML [using jupyter-book] +name: Check WASM on: [pull_request] jobs: preview: