A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: function calling with nested apps (#1797)
Akshay Agrawale2007d6d7676
diff --git a/marimo/_ast/app.py b/marimo/_ast/app.pyindex 30c044cbc7..0f2d946960 100644--- a/marimo/_ast/app.py+++ b/marimo/_ast/app.py@@ -35,11 +35,16 @@ from marimo._runtime.context.types import (get_context,runtime_context_installed,)-from marimo._runtime.requests import SetUIElementValueRequest+from marimo._runtime.requests import (+ FunctionCallRequest,+ SetUIElementValueRequest,+)if TYPE_CHECKING:from collections.abc import Sequence+ from marimo._messaging.ops import HumanReadableStatus+ from marimo._plugins.core.web_component import JSONTypefrom marimo._runtime.context.types import ExecutionContextLOGGER = _loggers.marimo_logger()@@ -299,6 +304,12 @@ class App:app_kernel_runner = self._get_kernel_runner()return await app_kernel_runner.set_ui_element_value(request)+ async def _function_call(+ self, request: FunctionCallRequest+ ) -> tuple[HumanReadableStatus, JSONType, bool]:+ app_kernel_runner = self._get_kernel_runner()+ return await app_kernel_runner.function_call(request)+@mddocasync def embed(self) -> AppEmbedResult:"""Embed a notebook into another notebook.@@ -623,3 +634,8 @@ class InternalApp:self, request: SetUIElementValueRequest) -> bool:return await self._app._set_ui_element_value(request)++ async def function_call(+ self, request: FunctionCallRequest+ ) -> tuple[HumanReadableStatus, JSONType, bool]:+ return await self._app._function_call(request)diff --git a/marimo/_runtime/app/kernel_runner.py b/marimo/_runtime/app/kernel_runner.pyindex 6f97551653..114bdb5ebc 100644--- a/marimo/_runtime/app/kernel_runner.py+++ b/marimo/_runtime/app/kernel_runner.py@@ -12,12 +12,15 @@ from marimo._runtime.patches import create_main_modulefrom marimo._runtime.requests import (AppMetadata,ExecutionRequest,+ FunctionCallRequest,SetUIElementValueRequest,)from marimo._runtime.runner import cell_runnerif TYPE_CHECKING:from marimo._ast.app import InternalApp+ from marimo._messaging.ops import HumanReadableStatus+ from marimo._plugins.core.web_component import JSONTypeclass AppKernelRunner:@@ -115,3 +118,9 @@ class AppKernelRunner:) -> bool:with self._runtime_context.install():return await self._kernel.set_ui_element_value(request)++ async def function_call(+ self, request: FunctionCallRequest+ ) -> tuple[HumanReadableStatus, JSONType, bool]:+ with self._runtime_context.install():+ return await self._kernel.function_call_request(request)diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex d08446016f..ad78175681 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -1269,7 +1269,13 @@ class Kernel:async def function_call_request(self, request: FunctionCallRequest- ) -> tuple[HumanReadableStatus, JSONType]:+ ) -> tuple[HumanReadableStatus, JSONType, bool]:+ """Execute a function call.++ If the function is not found, children contexts are also searched.+ Returns a status, payload, and a bool which is True if the function was+ found, False otherwise.+ """ctx = get_context()function = ctx.function_registry.get_function(request.namespace, request.function_name@@ -1280,18 +1286,30 @@ class Kernel:LOGGER.debug("%s: %s", title, message)if function is None:+ for child_context in ctx.children:+ if child_context.app is not None:+ (+ status,+ response,+ found,+ ) = await child_context.app.function_call(request)+ if found:+ return status, response, found+ found = Falseerror_title = "Function not found"error_message = ("Could not find function given request: %s" % request)debug(error_title, error_message)elif function.cell_id is None:+ found = Trueerror_title = "Function not associated with cell"error_message = ("Attempted to call a function without a cell id: %s" % request)debug(error_title, error_message)else:+ found = Truewith self._install_execution_context(cell_id=function.cell_id), ctx.provide_ui_ids(str(uuid4())):@@ -1308,12 +1326,14 @@ class Kernel:# TODO(akshayka): Do UI elements created in function calls# get cleared from the FE registry? This could be a leak.try:- response = function(request.args)+ response = cast(JSONType, function(request.args))if asyncio.iscoroutine(response):response = await response- return HumanReadableStatus(code="ok"), response- return HumanReadableStatus(code="ok"), cast(- JSONType, response+ return HumanReadableStatus(code="ok"), response, found+ return (+ HumanReadableStatus(code="ok"),+ response,+ found,)except MarimoInterrupt:error_title = "Interrupted"@@ -1336,6 +1356,7 @@ class Kernel:code="error", title=error_title, message=error_message),None,+ found,)async def instantiate(self, request: CreationRequest) -> None:@@ -1470,7 +1491,7 @@ class Kernel:await self.set_ui_element_value(request)CompletedRun().broadcast()elif isinstance(request, FunctionCallRequest):- status, ret = await self.function_call_request(request)+ status, ret, _ = await self.function_call_request(request)FunctionCallResult(function_call_id=request.function_call_id,return_value=ret,