Skip to content

Commit

Permalink
fix: support esm define when running under virtual kernel
Browse files Browse the repository at this point in the history
When we define a esm top level in a module, we don't need to create
the module widget. However, if we do this during the running of the
app under the context of a virtual kernel, we should directly
create the esm widget.

Should fix the failing of widgetti/ipyantd#4
  • Loading branch information
maartenbreddels committed Jun 6, 2024
1 parent 03e6776 commit 47a8b6b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
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

0 comments on commit 47a8b6b

Please sign in to comment.