diff --git a/solara/server/esm.py b/solara/server/esm.py index bbae33036..6e753cdfb 100644 --- a/solara/server/esm.py +++ b/solara/server/esm.py @@ -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 diff --git a/tests/integration/esm_test.py b/tests/integration/esm_test.py index b371409bd..2b9164f66 100644 --- a/tests/integration/esm_test.py +++ b/tests/integration/esm_test.py @@ -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 @@ -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()