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

fix: support esm define when running under virtual kernel #668

Merged
merged 1 commit into from
Jun 6, 2024
Merged
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
2 changes: 2 additions & 0 deletions solara/server/esm.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def define_module(name, module: Union[str, Path]):
if name in _modules:
old_module, dependencies = _modules[name]
_modules[name] = (module, dependencies)
if kernel_context.has_current_context():
create_modules()
return None


Expand Down
22 changes: 14 additions & 8 deletions tests/integration/esm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
HERE = Path(__file__).parent


ipyreact.define_module(
"solara-test",
"""
test_js_code = """
import * as React from "react";

export default function({value, setValue, ...children}) {
return React.createElement("button", {onClick: () => setValue(value + 1), children: `clicked ${value || 0}`})
return React.createElement("button", {onClick: () => setValue(value + 1), children: `clicked ${value || 0}`})
}
""",
)
"""
ipyreact.define_module("solara-test", test_js_code)


@solara.component
Expand All @@ -32,9 +30,17 @@ def Page():
ipyreact.ValueWidget.element(_module="solara-test", value=0, on_value=value.set)


@solara.component
def PageDefineDuringRun():
ipyreact.define_module("solara-test-dynamic", test_js_code)
value = solara.use_reactive(0)
ipyreact.ValueWidget.element(_module="solara-test-dynamic", value=0, on_value=value.set)


@pytest.mark.skipif(sys.version_info < (3, 7, 0), reason="ipyreact requires python 3.7 or higher")
def test_ipyreact(browser: playwright.sync_api.Browser, page_session: playwright.sync_api.Page, solara_server, solara_app, extra_include_path):
with extra_include_path(HERE), solara_app("esm_test:Page"):
@pytest.mark.parametrize("app", ["esm_test:Page", "esm_test:PageDefineDuringRun"])
def test_ipyreact(browser: playwright.sync_api.Browser, page_session: playwright.sync_api.Page, solara_server, solara_app, extra_include_path, app):
with extra_include_path(HERE), solara_app(app):
page_session.goto(solara_server.base_url)
page_session.locator("text=clicked 0").first.click()
page_session.locator("text=clicked 1").first.click()
Expand Down
Loading