Skip to content

Commit

Permalink
feat(bindgen): Python emscripten support
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Apr 22, 2023
1 parent 46dd690 commit f5ac4d9
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 54 deletions.
41 changes: 37 additions & 4 deletions .github/workflows/python-wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: Python Wasm

on: [push,pull_request]

env:
pyodide-version: 0.23.1

jobs:
test-itkwasm:
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -41,7 +44,7 @@ jobs:
shell: bash
working-directory: ./packages/core/python/itkwasm
run: |
curl -L https://github.com/pyodide/pyodide/releases/download/0.23.1/pyodide-0.23.1.tar.bz2 -o pyodide.tar.bz2
curl -L https://github.com/pyodide/pyodide/releases/download/${{ env.pyodide-version }}/pyodide-${{ env.pyodide-version }}.tar.bz2 -o pyodide.tar.bz2
tar xjf pyodide.tar.bz2
rm -rf dist
mv pyodide dist
Expand Down Expand Up @@ -88,17 +91,47 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install hatch pytest
- name: Test compress-stringify wasi
- name: Install pytest-pyodide
if: ${{ matrix.python-minor-version > 9 }}
shell: bash
run: |
python -m pip install pytest-pyodide
- uses: pyodide/pyodide-actions/install-browser@main
if: ${{ matrix.python-minor-version > 9 }}
with:
runner: selenium
browser: chrome
browser-version: latest
- name: Test compress-stringify wasi native
if: ${{ matrix.python-minor-version < 10 }}
working-directory: ./packages/compress-stringify/python/itkwasm-compress-stringify-wasi
run: |
python -m pip install -e .
pytest
hatch run test
- name: Test compress-stringify
- name: Test compress-stringify native
if: ${{ matrix.python-minor-version < 10 }}
working-directory: ./packages/compress-stringify/python/itkwasm-compress-stringify
run: |
python -m pip install -e .
pytest
- name: Test compress-stringify wasi chrome
if: ${{ matrix.python-minor-version > 9 }}
working-directory: ./packages/compress-stringify/python/itkwasm-compress-stringify-wasi
shell: bash
run: |
hatch run download-pyodide
hatch build -t wheel
ls dist
pytest --dist-dir=./dist --rt=chrome
- name: Test compress-stringify chrome
if: ${{ matrix.python-minor-version > 9 }}
working-directory: ./packages/compress-stringify/python/itkwasm-compress-stringify
shell: bash
run: |
hatch run download-pyodide
hatch build -t wheel
ls dist
pytest --dist-dir=./dist --rt=chrome
build-sphinx-docs:
runs-on: ubuntu-22.04
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"build:emscripten": "node ./src/build-emscripten.js",
"build:emscripten:compress-stringify": "node ./src/itk-wasm-cli.js -s packages/compress-stringify -b emscripten-build build ",
"build:bindgen:typescript:compress-stringify": "./src/itk-wasm-cli.js -s packages/compress-stringify -b emscripten-build bindgen --package-name @itk-wasm/compress-stringify --package-description \"Zstandard compression and decompression and base64 encoding and decoding in WebAssembly.\" --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:bindgen:python:compress-stringify": "./src/itk-wasm-cli.js -s packages/compress-stringify -b wasi-build bindgen --language python --package-name itkwasm-compress-stringify --package-description \"Zstandard compression and decompression and base64 encoding and decoding in WebAssembly.\" --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:bindgen:python:compress-stringify": "./src/itk-wasm-cli.js -s packages/compress-stringify -b wasi-build bindgen --language python --package-name itkwasm-compress-stringify --package-description \"Zstandard compression and decompression and base64 encoding and decoding in WebAssembly.\" --package-version 0.4.2 --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:emscripten:dicom": "node ./src/itk-wasm-cli.js -s packages/dicom -b emscripten-build build ",
"build:bindgen:typescript:dicom": "./src/itk-wasm-cli.js -s packages/dicom -b emscripten-build bindgen --package-name @itk-wasm/dicom --package-description \"Read files and images related to DICOM file format.\" --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:emscripten:packages": "npm run build:emscripten:compress-stringify && npm run build:bindgen:typescript:compress-stringify && npm run build:emscripten:dicom && npm run build:bindgen:typescript:dicom",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.0"
__version__ = "0.4.2"
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ keywords = [

requires-python = ">=3.7"
dependencies = [
"itkwasm >= 1.0.b93",
"itkwasm >= 1.0.b97",
"importlib_resources",

]
Expand All @@ -46,9 +46,11 @@ dependencies = [
"pytest",
]


[tool.hatch.envs.default.scripts]
test = "pytest"


[tool.hatch.build]
exclude = [
"/examples",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.0"
__version__ = "0.4.2"
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ keywords = [

requires-python = ">=3.7"
dependencies = [
"itkwasm >= 1.0.b93",
"itkwasm >= 1.0.b97",
"itkwasm-compress-stringify-wasi; sys_platform != \"emscripten\"",
"itkwasm-compress-stringify-emscripten; sys_platform == \"emscripten\"",

Expand All @@ -47,9 +47,11 @@ dependencies = [
"pytest",
]


[tool.hatch.envs.default.scripts]
test = "pytest"


[tool.hatch.build]
exclude = [
"/examples",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/python/itkwasm/itkwasm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""itkwasm: Python interface to itk-wasm WebAssembly modules."""

__version__ = "1.0b96"
__version__ = "1.0b97"

from .interface_types import InterfaceTypes
from .image import Image, ImageType
Expand Down
7 changes: 5 additions & 2 deletions packages/core/python/itkwasm/itkwasm/pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def web_worker(self, value):

def to_py(js_proxy):
import pyodide
if isinstance(js_proxy, pyodide.ffi.JsArray):
if hasattr(js_proxy, 'constructor') and js_proxy.constructor.name == "Uint8Array":
return js_proxy.to_bytes()
elif isinstance(js_proxy, pyodide.ffi.JsArray):
return [to_py(value) for value in js_proxy]
elif hasattr(js_proxy, "imageType"):
image_dict = js_proxy.to_py()
Expand Down Expand Up @@ -139,7 +141,8 @@ def to_py(js_proxy):
binary_stream_dict = js_proxy.to_py()
binary_stream_dict['data'] = bytes(binary_stream_dict['data'])
return BinaryStream(**binary_stream_dict)
return js_proxy.to_py()
# Is not a JsProxy, int, etc
return js_proxy

def to_js(py):
import pyodide
Expand Down
25 changes: 25 additions & 0 deletions packages/core/python/itkwasm/test/test_pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,28 @@ def verify_text_file(text_file):

for text_file in text_files_py:
verify_text_file(text_file)

@run_in_pyodide(packages=['micropip', 'numpy'])
async def test_uint8array_conversion(selenium, package_wheel):
import micropip
await micropip.install(package_wheel)

from itkwasm.pyodide import to_js, to_py

data = bytes([222,173,190,239])

data_js = to_js(data)
data_py = to_py(data_js)

assert isinstance(data_py, bytes)

assert data_py[0], 222
assert data_py[1], 173
assert data_py[2], 190
assert data_py[3], 239

ivalue = 8
ivalue_js = to_js(ivalue)
ivalue_py = to_py(ivalue_js)

assert ivalue == ivalue_py
17 changes: 17 additions & 0 deletions src/bindgen/camelCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function camelCase(param) {
// make any alphabets that follows '-' an uppercase character, and remove the corresponding hyphen
let cameledParam = param.replace(/-([a-z])/g, (kk) => {
return kk[1].toUpperCase();
});

// remove all non-alphanumeric characters
const outParam = cameledParam.replace(/([^0-9a-z])/ig, '')

// check if resulting string is empty
if(outParam === '') {
console.error(`Resulting string is empty.`)
}
return outParam
}

export default camelCase
7 changes: 3 additions & 4 deletions src/bindgen/python-resources/template.pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,18 @@ keywords = [

requires-python = ">=3.7"
dependencies = [
"itkwasm >= 1.0.b93",@bindgenDependencies@
"itkwasm >= 1.0.b97",@bindgenDependencies@
]

[tool.hatch.version]
path = "@bindgenPyPackage@/_version.py"

[tool.hatch.envs.default]
dependencies = [
"pytest",
"pytest",@bindgenHatchEnvDependencies@
]

[tool.hatch.envs.default.scripts]
test = "pytest"
@bindgenHatchEnvScripts@

[tool.hatch.build]
exclude = [
Expand Down
Loading

0 comments on commit f5ac4d9

Please sign in to comment.