Skip to content

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.

Complete runs triggered by package installation (#11433)

Trevor Manz06a2a1fb2a80
diff --git a/marimo/_runtime/callbacks/packages.py b/marimo/_runtime/callbacks/packages.py
index c35cfb9cc7..92df68cf05 100644
--- a/marimo/_runtime/callbacks/packages.py
+++ b/marimo/_runtime/callbacks/packages.py
@@ -9,7 +9,6 @@ from marimo._config.settings import GLOBAL_SETTINGS
from marimo._dependencies.errors import ManyModulesNotFoundError
from marimo._messaging.context import is_code_mode_request
from marimo._messaging.notification import (
- CompletedRunNotification,
MissingPackageAlertNotification,
PackageStatusType,
)
@@ -36,7 +35,6 @@ from marimo._runtime.packages.utils import (
from marimo._runtime.runner import hook_context
if TYPE_CHECKING:
- from marimo._runtime.request_router import RequestRouter
from marimo._runtime.runtime import Kernel
LOGGER = _loggers.marimo_logger()
@@ -58,9 +56,6 @@ class PackagesCallbacks:
self._kernel = kernel
self.package_manager: PackageManager | None = None
- def register(self, router: RequestRouter) -> None:
- router.register(InstallPackagesCommand, self._handle_install)
-
def _notebook_index_urls(self) -> list[str]:
"""Read PEP 723 index config from the current notebook.
@@ -92,10 +87,6 @@ class PackagesCallbacks:
urls.append(url)
return urls
- async def _handle_install(self, request: InstallPackagesCommand) -> None:
- await self.install_missing_packages(request)
- broadcast_notification(CompletedRunNotification())
-
def update_package_manager(self, package_manager: str) -> None:
if GLOBAL_SETTINGS.SANDBOX_MODE is not None:
from marimo._environments.backends import current_backend
diff --git a/marimo/_runtime/kernel_request_handlers.py b/marimo/_runtime/kernel_request_handlers.py
index d614d31a45..d65d2dd3a6 100644
--- a/marimo/_runtime/kernel_request_handlers.py
+++ b/marimo/_runtime/kernel_request_handlers.py
@@ -31,6 +31,7 @@ from marimo._runtime.commands import (
ExecuteCellsCommand,
ExecuteScratchpadCommand,
ExecuteStaleCellsCommand,
+ InstallPackagesCommand,
InvokeFunctionCommand,
ModelCommand,
RenameNotebookCommand,
@@ -88,6 +89,7 @@ class KernelRequestHandlers:
ExecuteScratchpadCommand, self._handle_execute_scratchpad
)
router.register(ExecuteStaleCellsCommand, self._handle_execute_stale)
+ router.register(InstallPackagesCommand, self._handle_install)
router.register(InvokeFunctionCommand, self._handle_function_call)
router.register(DebugCellCommand, self._handle_pdb_request)
router.register(RenameNotebookCommand, self._handle_rename)
@@ -213,6 +215,12 @@ class KernelRequestHandlers:
with self._run_context(), http_request_context(request.request):
await self._kernel.run_stale_cells()
+ async def _handle_install(self, request: InstallPackagesCommand) -> None:
+ with self._run_context():
+ await self._kernel.packages_callbacks.install_missing_packages(
+ request
+ )
+
async def _handle_set_ui_element_value(
self, request: UpdateUIElementCommand
) -> None:
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 58c5e72c90..da3a743a2e 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -588,7 +588,6 @@ class Kernel:
self._callbacks: list[KernelCallback] = [
self.secrets_callbacks,
self.datasets_callbacks,
- self.packages_callbacks,
self.sql_callbacks,
self.cache_callbacks,
self.external_storage_callbacks,
diff --git a/tests/_runtime/test_kernel_request_handlers.py b/tests/_runtime/test_kernel_request_handlers.py
index bb6cd7eb9a..1f5348c029 100644
--- a/tests/_runtime/test_kernel_request_handlers.py
+++ b/tests/_runtime/test_kernel_request_handlers.py
@@ -9,6 +9,7 @@ import pytest
from marimo._runtime.commands import (
ExecuteCellsCommand,
ExecuteScratchpadCommand,
+ InstallPackagesCommand,
ModelCommand,
ModelUpdateMessage,
)
@@ -58,6 +59,38 @@ async def test_cell_runs_carry_the_requested_run_id(
assert {m["run_id"] for m in messages if m["op"] == "cell-op"} == {"run-1"}
+async def test_package_install_rerun_completes_its_run(
+ mocked_kernel: MockedKernel,
+) -> None:
+ kernel = mocked_kernel.k
+ kernel._maybe_register_cell("a", "import math", stale=False)
+
+ async def install(request: InstallPackagesCommand) -> None:
+ del request
+ await kernel.maybe_autorun_cells({CellId_t("a")})
+
+ with patch.object(
+ kernel.packages_callbacks,
+ "install_missing_packages",
+ side_effect=install,
+ ):
+ await kernel.router.dispatch(
+ InstallPackagesCommand(manager="pip", versions={"math": ""})
+ )
+
+ messages = MockStream(mocked_kernel.stream).operations
+ planned = [
+ m
+ for m in messages
+ if m["op"] == "execution" and m["phase"] == "planned"
+ ]
+ completed = [m for m in messages if m["op"] == "completed-run"]
+ assert len(planned) == len(completed) == 1
+ assert planned[0]["run_id"] is not None
+ assert completed[0]["run_id"] == planned[0]["run_id"]
+ assert messages[-1]["op"] == "completed-run"
+
+
async def test_a_failed_command_flushes_before_completing(
mocked_kernel: MockedKernel,
) -> None:
diff --git a/tests/_runtime/test_request_router.py b/tests/_runtime/test_request_router.py
index c77c896194..1964a77341 100644
--- a/tests/_runtime/test_request_router.py
+++ b/tests/_runtime/test_request_router.py
@@ -11,7 +11,6 @@ from marimo._runtime.callbacks import (
DatasetCallbacks,
ExternalStorageCallbacks,
KernelCallback,
- PackagesCallbacks,
SecretsCallbacks,
SqlCallbacks,
)
@@ -37,7 +36,6 @@ ALL_CALLBACKS: list[type] = [
SqlCallbacks,
CacheCallbacks,
ExternalStorageCallbacks,
- PackagesCallbacks,
]
# Commands that are part of the CommandMessage dispatch surface but are