Skip to content
Draft
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
36 changes: 27 additions & 9 deletions deepnote_toolkit/set_notebook_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,36 @@ def set_notebook_path() -> None:
"""Set the notebook path and working directory based on the Jupyter session.

This function:
- Determines the notebook root directory from config or environment
- Finds the current notebook's path using the Jupyter API
- Sets DEEPNOTE_PROJECT_ID from runtime.project_id in detached or dev mode
- Returns early in detached mode outside dev mode when neither
paths.notebook_root nor paths.home_dir is set
- Otherwise determines the notebook root directory from config or environment
- Finds the current notebook's path using the Jupyter API; in detached or
dev mode, parses DEEPNOTE_PROJECT_ID from the session name if still unset
- Updates sys.path to include the notebook's directory
- Changes the working directory to the notebook's directory
- Sets DEEPNOTE_PROJECT_ID in detached mode
"""
try:
cfg = get_config()
rt = cfg.runtime
_detached = bool(rt.running_in_detached_mode or rt.dev_mode)
project_id = rt.project_id
# runtime-core starts Jupyter and the kernel in the directory it was given:
# the .deepnote file's folder, or the deepnote CLI's --cwd
_keep_kernel_cwd = (
rt.running_in_detached_mode
and not rt.dev_mode
and not cfg.paths.notebook_root
and not cfg.paths.home_dir
)
except Exception:
_detached, _keep_kernel_cwd, project_id = False, False, None

if _detached and project_id:
env.set_env("DEEPNOTE_PROJECT_ID", project_id)
if _keep_kernel_cwd:
return

root_path = resolve_notebook_root()

try:
Expand Down Expand Up @@ -48,14 +72,8 @@ def set_notebook_path() -> None:
if session["kernel"]["id"] == kernel_id
)

# If not provided as an environmental variable, use projectId parsed from session name
# ProjectID is used for integration functionality
# Expected session name format: "{schema_version}:{type}:{project_id}:{...additional segments}"
try:
rt = get_config().runtime
_detached = bool(rt.running_in_detached_mode or rt.dev_mode)
except Exception:
_detached = False
if _detached and not env.has_env("DEEPNOTE_PROJECT_ID"):
env.set_env("DEEPNOTE_PROJECT_ID", session["name"].split(":")[2])

Expand Down
4 changes: 2 additions & 2 deletions docs/user/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ At runtime, the Toolkit automatically loads `DEEPNOTE_CONFIG_FILE` if present.
- `installation.index_url`: base index URL for bundles
- `installation.bundle_path`: local bundle dir
- `installation.cache_path`: cache dir
- `runtime.running_in_detached_mode`: detached/direct mode toggle
- `runtime.running_in_detached_mode`: detached/direct mode toggle. Outside dev mode and without `paths.notebook_root` or `paths.home_dir`, kernels keep the working directory and `sys.path` they start with
- `runtime.dev_mode`: local dev toggles (e.g., default notebook root `/work`)
- `runtime.ci`: CI toggle for logging behavior
- `runtime.webapp_url`: base webapp URL (used in detached/dev mode)
- `runtime.project_id`: runtime UUID/project identifier
- `runtime.project_id`: runtime UUID/project identifier; in detached or dev mode, kernels see it as `DEEPNOTE_PROJECT_ID`
- `runtime.coerce_float`: controls pandas `read_sql_query(coerce_float=...)`
- `runtime.env_integration_enabled`: enables variable injection from integrations

Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_set_notebook_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def _cfg(jupyter_port=9999, notebook_root: str = "/tmp", detached=True, dev=Fals
return types.SimpleNamespace(
server=types.SimpleNamespace(jupyter_port=jupyter_port),
paths=types.SimpleNamespace(notebook_root=notebook_root, home_dir=None),
runtime=types.SimpleNamespace(running_in_detached_mode=detached, dev_mode=dev),
runtime=types.SimpleNamespace(
running_in_detached_mode=detached, dev_mode=dev, project_id=None
),
)


Expand Down
91 changes: 91 additions & 0 deletions tests/unit/test_set_notebook_path_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,97 @@
import json
import os
import sys
import types
from unittest import mock

import pytest

from deepnote_toolkit import env as dnenv
from deepnote_toolkit.set_notebook_path import set_notebook_path


@pytest.fixture
def kernel(tmp_path, monkeypatch):
"""Kernel whose Jupyter session opens ``folder/notebook.ipynb``."""
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.delenv("DEEPNOTE_PROJECT_ID", raising=False)
monkeypatch.setattr(
"ipykernel.connect.get_connection_file",
lambda: str(tmp_path / "kernel-1234.json"),
)
session = {"kernel": {"id": "1234"}, "path": "folder/notebook.ipynb", "name": ""}
monkeypatch.setattr(
"requests.get",
lambda url, headers: mock.MagicMock(text=json.dumps([session])),
)
chdirs = []
monkeypatch.setattr("os.chdir", chdirs.append)
monkeypatch.setattr(sys, "path", ["", "/opt/site-packages"])

yield types.SimpleNamespace(home=tmp_path, session=session, chdirs=chdirs)

# set_notebook_path exports to os.environ, which monkeypatch does not restore
dnenv.unset_env("DEEPNOTE_PROJECT_ID")


def test_detached_without_paths_keeps_kernel_cwd_and_exports_project_id(
kernel, monkeypatch
):
monkeypatch.setenv("DEEPNOTE_RUNTIME__RUNNING_IN_DETACHED_MODE", "true")
monkeypatch.setenv("DEEPNOTE_RUNTIME__PROJECT_ID", "proj-1")
kernel.session["name"] = "report-1234.deepnote"

set_notebook_path()

assert kernel.chdirs == []
assert sys.path == ["", "/opt/site-packages"]
assert os.environ.get("DEEPNOTE_PROJECT_ID") == "proj-1"


def test_detached_without_paths_or_project_id_ignores_session_name(kernel, monkeypatch):
monkeypatch.setenv("DEEPNOTE_RUNTIME__RUNNING_IN_DETACHED_MODE", "true")
monkeypatch.delenv("DEEPNOTE_RUNTIME__PROJECT_ID", raising=False)
kernel.session["name"] = "sales:q1:report-1234.deepnote"

set_notebook_path()

assert kernel.chdirs == []
assert sys.path == ["", "/opt/site-packages"]
assert os.environ.get("DEEPNOTE_PROJECT_ID") is None


@pytest.mark.parametrize(
"detached, dev_mode, notebook_dir",
[
("false", "false", "{home}/work/folder"),
("false", "true", "/work/folder"),
("true", "true", "/work/folder"),
],
ids=["attached", "dev-mode", "detached-dev-mode"],
)
def test_rewrites_sys_path_and_cwd_when_attached_or_in_dev_mode(
kernel, monkeypatch, detached, dev_mode, notebook_dir
):
monkeypatch.setenv("DEEPNOTE_RUNTIME__RUNNING_IN_DETACHED_MODE", detached)
monkeypatch.setenv("DEEPNOTE_RUNTIME__DEV_MODE", dev_mode)
notebook_dir = notebook_dir.format(home=kernel.home)
kernel.session["name"] = "1:type:proj-xyz:rest"

set_notebook_path()

assert kernel.chdirs == [notebook_dir]
assert sys.path == ["/opt/site-packages", notebook_dir]


def test_dev_mode_exports_configured_project_id_over_session_name(kernel, monkeypatch):
monkeypatch.setenv("DEEPNOTE_RUNTIME__DEV_MODE", "true")
monkeypatch.setenv("DEEPNOTE_RUNTIME__PROJECT_ID", "proj-1")
kernel.session["name"] = "1:type:other-id:rest"

set_notebook_path()

assert os.environ.get("DEEPNOTE_PROJECT_ID") == "proj-1"


def test_set_notebook_path_uses_config_home_and_port(tmp_path, monkeypatch):
# Prepare config: home_dir and jupyter_port, detached mode true
Expand Down
Loading