Skip to content

Commit

Permalink
fix: allow editable installs when devloping locally with sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
mscolnick committed Jan 24, 2025
1 parent 854e641 commit 68e1d09
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
17 changes: 13 additions & 4 deletions marimo/_cli/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import subprocess
import sys
import tempfile
from pathlib import Path
from typing import Any, Dict, List, Optional, cast

import click
Expand All @@ -17,6 +18,7 @@
from marimo._cli.print import bold, echo, green, muted
from marimo._config.settings import GLOBAL_SETTINGS
from marimo._dependencies.dependencies import DependencyManager
from marimo._utils.versions import is_editable

LOGGER = _loggers.marimo_logger()

Expand Down Expand Up @@ -227,10 +229,9 @@ def _normalize_sandbox_dependencies(
# Find all marimo dependencies
marimo_deps = [d for d in dependencies if _is_marimo_dependency(d)]
if not marimo_deps:
# During development, you can comment this out to install an
# editable version of marimo assuming you are in the marimo directory
# DO NOT COMMIT THIS WHEN SUBMITTING PRs
# return dependencies + [f"marimo -e ."]
if is_editable("marimo"):
LOGGER.info("Using editable of marimo for sandbox")
return dependencies + [f"-e {get_marimo_dir()}"]

return dependencies + [f"marimo=={marimo_version}"]

Expand All @@ -241,13 +242,21 @@ def _normalize_sandbox_dependencies(
# Remove all marimo deps
filtered = [d for d in dependencies if not _is_marimo_dependency(d)]

if is_editable("marimo"):
LOGGER.info("Using editable of marimo for sandbox")
return filtered + [f"-e {get_marimo_dir()}"]

# Add version if not already versioned
if not _is_versioned(chosen):
chosen = f"{chosen}=={marimo_version}"

return filtered + [chosen]


def get_marimo_dir() -> Path:
return Path(__file__).parent.parent.parent


def construct_uv_command(args: list[str], name: str | None) -> list[str]:
cmd = ["marimo"] + args
if "--sandbox" in cmd:
Expand Down
13 changes: 10 additions & 3 deletions marimo/_server/templates/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from marimo._server.file_manager import read_css_file, read_html_head_file
from marimo._server.model import SessionMode
from marimo._server.tokens import SkewProtectionToken
from marimo._utils.versions import is_editable


def home_page_template(
Expand All @@ -30,7 +31,7 @@ def home_page_template(
html = html.replace("{{ user_config }}", json.dumps(user_config))
html = html.replace("{{ config_overrides }}", json.dumps(config_overrides))
html = html.replace("{{ server_token }}", str(server_token))
html = html.replace("{{ version }}", __version__)
html = html.replace("{{ version }}", get_version())

html = html.replace("{{ title }}", "marimo")
html = html.replace("{{ app_config }}", json.dumps({}))
Expand All @@ -54,7 +55,7 @@ def notebook_page_template(
html = html.replace("{{ user_config }}", json.dumps(user_config))
html = html.replace("{{ config_overrides }}", json.dumps(config_overrides))
html = html.replace("{{ server_token }}", str(server_token))
html = html.replace("{{ version }}", __version__)
html = html.replace("{{ version }}", get_version())

html = html.replace(
"{{ title }}",
Expand Down Expand Up @@ -123,7 +124,7 @@ def static_notebook_template(
)
html = html.replace("{{ config_overrides }}", json.dumps(config_overrides))
html = html.replace("{{ server_token }}", str(server_token))
html = html.replace("{{ version }}", __version__)
html = html.replace("{{ version }}", get_version())

html = html.replace(
"{{ title }}",
Expand Down Expand Up @@ -345,3 +346,9 @@ def _del_none_or_empty(d: Any) -> Any:
for key, value in d.items()
if value is not None and value != []
}


def get_version() -> str:
return (
f"{__version__} (editable)" if is_editable("marimo") else __version__
)
23 changes: 23 additions & 0 deletions marimo/_utils/versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from __future__ import annotations

import json
from importlib.metadata import Distribution


def is_editable(pkg_name: str) -> bool:
"""Check if a package is an editable install"""

try:
direct_url = Distribution.from_name(pkg_name).read_text(
"direct_url.json"
)
except Exception:
return False

if direct_url is None:
return False

pkg_is_editable = (
json.loads(direct_url).get("dir_info", {}).get("editable", False)
)
return pkg_is_editable

0 comments on commit 68e1d09

Please sign in to comment.