Skip to content

Commit

Permalink
Support for pyodide 0.27.1, including pyarrow + pandas
Browse files Browse the repository at this point in the history
- Switch to using global-ish `add_compile_options` to ensure every
  target we need to link, including all Arrow components,
  is built with Emscripten exceptions support
- Add two regression tests to the pytest-pyodide suite for pyarrow
  and pandas examples which were previously crashing

Signed-off-by: Tom Jakubowski <tom@prospective.dev>
  • Loading branch information
tomjakubowski committed Jan 23, 2025
1 parent 9cfcd98 commit 5e5eb22
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmake/modules/FindInstallDependency.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function(psp_build_dep name cmake_file)
message(FATAL_ERROR "Build step for ${name} failed: ${result}")
endif()
endif()

if(${name} STREQUAL arrow)
set(ARROW_DEFINE_OPTIONS ON)
set(ARROW_BUILD_SHARED OFF)
Expand Down
15 changes: 9 additions & 6 deletions cpp/perspective/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,15 @@ elseif(PSP_CPP_BUILD OR PSP_PYTHON_BUILD)
target_include_directories(psp PRIVATE ${psp_INCLUDE_DIRS})
target_include_directories(psp SYSTEM PRIVATE ${all_deps_INCLUDE_DIRS})
target_compile_definitions(psp PRIVATE PSP_ENABLE_PYTHON=1 PSP_ENABLE_WASM=1)
# support for emscripten exceptions https://emscripten.org/docs/porting/exceptions.html#emscripten-javascript-based-exception-support
target_compile_options(psp PUBLIC -fexceptions -fvisibility=hidden)
target_compile_options(arrow_static PUBLIC -fexceptions -fvisibility=hidden)
target_compile_options(re2 PUBLIC -fexceptions -fvisibility=hidden)
target_compile_options(protos PUBLIC -fexceptions -fvisibility=hidden)
# Support for emscripten exceptions
# https://emscripten.org/docs/porting/exceptions.html#emscripten-javascript-based-exception-support
# We need these compile options on psp and all library targets
# which the perspective-server crate .so links to statically
# (including numerous small components like arrow_io). Rather than
# enumerate those, add_compile_options() applies the options to all
# targets in this directory and its subdirectories, including those
# from psp_build_dep
add_compile_options(-fexceptions -fvisibility=hidden)
else()
# Cpython
add_library(psp STATIC ${PYTHON_SOURCE_FILES})
Expand All @@ -631,7 +635,6 @@ elseif(PSP_CPP_BUILD OR PSP_PYTHON_BUILD)
target_compile_options(psp PRIVATE -fvisibility=hidden)
endif()

# Link against arrow static library
# Linking against arrow_static also links against its bundled dependencies
target_link_libraries(psp PRIVATE arrow_static re2 protos)
else()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "module",
"emscripten": "3.1.58",
"llvm": "17.0.6",
"pyodide": "0.26.2",
"pyodide": "0.27.1",
"engines": {
"node": ">=16"
},
Expand Down
31 changes: 31 additions & 0 deletions rust/perspective-python/pyodide-tests/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
# pyodide-tests

Smoke and integration tests for the perspective-python Pyodide wheel.
pytest-pyodide and Playwright

These tests require that a Pyodide wheel has been built to rust/target/wheels

## test setup

Create a virtual environment. Install perspective-python requirements and
special pyodide-only requirements:

```
pip install -r rust/perspective-python/requirements.txt
pip install -r rust/perspective-python/requirements-pyodide.txt
```

## running tests

Run setup, select `perspective-pyodide` target:

```
pnpm -w run setup
```

Then run tests:

```
pnpm -w test
```

If you are prompted to install playwright browsers, run this in your venv:

```
python -m playwright install
```
43 changes: 43 additions & 0 deletions rust/perspective-python/pyodide-tests/tests/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def psp_wheel_path(pytestconfig):

# Based on micropip's test server fixture:
# https://github.com/pyodide/micropip/blob/eb8c4497d742e515d24d532db2b9cc014328265b/tests/conftest.py#L64-L87
# Run web server serving the directory containing the perspective wheel,
# yielding the wheel's URL for the fixture value
@pytest.fixture()
def psp_wheel_url(psp_wheel_path):
from pathlib import Path
Expand Down Expand Up @@ -72,3 +74,44 @@ async def test_parsing_good_csv(selenium):
client = server.new_local_client()
abc123 = client.table("a,b,c\n1,2,3\n")
assert abc123.columns() == ["a", "b", "c"]


# Test that perspective can load pyarrow Table values
@run_in_pyodide
async def test_perspective_and_pyarrow_together_at_last(selenium):
import micropip

await micropip.install("pyarrow")

import perspective
import pyarrow as pa

n_legs = pa.array([2, 2, 4, 4, 5, 100])
animals = pa.array(
["Flamingo", "Parrot", "Dog", "Horse", "Brittle stars", "Centipede"]
)
names = ["n_legs", "animals"]
table = pa.Table.from_arrays([n_legs, animals], names=names)

server = perspective.Server()
client = server.new_local_client()

animals = client.table(table, name="animals")
assert animals.columns() == ["n_legs", "animals"]


# Test that perspective can load pandas DataFrame values
@run_in_pyodide
async def test_pandas_dataframes(selenium):
import micropip

await micropip.install("pandas")

import perspective
import pandas as pd

server = perspective.Server()
client = server.new_local_client()
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
ab = client.table(df, name="ab")
assert sorted(ab.columns()) == ["a", "b", "index"]
2 changes: 2 additions & 0 deletions rust/perspective-python/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ if (process.env.PSP_PYODIDE) {
"--runtime=chrome",
`--dist-dir=${pyodideDistDir}`,
`--perspective-emscripten-wheel=${emscriptenWheel}`,
"--verbose",
...process.argv.slice(2),
],
execOpts
);
Expand Down

0 comments on commit 5e5eb22

Please sign in to comment.