-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: marimo edit --sandbox nb.py (#2134)
* feat: marimo edit --sandbox nb.py * add tests * fix * fix again * fix * Update marimo/_cli/cli.py * Update marimo/_cli/cli.py * fix --------- Co-authored-by: Akshay Agrawal <[email protected]>
- Loading branch information
Showing
4 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import re | ||
import subprocess | ||
from typing import Any, Dict, List, Optional, cast | ||
|
||
from marimo import _loggers | ||
from marimo._dependencies.dependencies import DependencyManager | ||
|
||
LOGGER = _loggers.marimo_logger() | ||
|
||
REGEX = ( | ||
r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$" | ||
) | ||
|
||
|
||
def run_in_sandbox( | ||
args: List[str], | ||
name: Optional[str] = None, | ||
) -> subprocess.CompletedProcess[Any]: | ||
import click | ||
|
||
if not DependencyManager.which("uv"): | ||
raise click.UsageError("uv must be installed to use --sandbox") | ||
|
||
cmd = ["marimo"] + args | ||
cmd.remove("--sandbox") | ||
|
||
# If name if a filepath, parse the dependencies from the file | ||
dependencies = [] | ||
if name is not None and os.path.isfile(name): | ||
with open(name) as f: | ||
dependencies = _get_dependencies(f.read()) or [] | ||
# Add marimo, if it's not already there | ||
if "marimo" not in dependencies and len(dependencies) > 0: | ||
dependencies.append("marimo") | ||
|
||
if dependencies: | ||
import tempfile | ||
|
||
with tempfile.NamedTemporaryFile( | ||
mode="w", delete=False, suffix=".txt" | ||
) as temp_file: | ||
temp_file.write("\n".join(dependencies)) | ||
temp_file_path = temp_file.name | ||
|
||
cmd = [ | ||
"uv", | ||
"run", | ||
"--isolated", | ||
"--with-requirements", | ||
temp_file_path, | ||
] + cmd | ||
|
||
# Clean up the temporary file after the subprocess has run | ||
import atexit | ||
|
||
atexit.register(lambda: os.unlink(temp_file_path)) | ||
else: | ||
cmd = ["uv", "run", "--isolated"] + cmd | ||
|
||
click.echo(f"Running in a sandbox: {' '.join(cmd)}") | ||
|
||
return subprocess.run(cmd) | ||
|
||
|
||
def _get_dependencies(script: str) -> List[str] | None: | ||
try: | ||
pyproject = _read_pyproject(script) or {} | ||
return cast(List[str], pyproject.get("dependencies", [])) | ||
except Exception as e: | ||
LOGGER.warning(f"Failed to parse dependencies: {e}") | ||
return None | ||
|
||
|
||
def _read_pyproject(script: str) -> Dict[str, Any] | None: | ||
""" | ||
Read the pyproject.toml file from the script. | ||
Adapted from https://peps.python.org/pep-0723/#reference-implementation | ||
""" | ||
name = "script" | ||
matches = list( | ||
filter(lambda m: m.group("type") == name, re.finditer(REGEX, script)) | ||
) | ||
if len(matches) > 1: | ||
raise ValueError(f"Multiple {name} blocks found") | ||
elif len(matches) == 1: | ||
content = "".join( | ||
line[2:] if line.startswith("# ") else line[1:] | ||
for line in matches[0].group("content").splitlines(keepends=True) | ||
) | ||
import tomlkit | ||
|
||
return tomlkit.parse(content) | ||
else: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from __future__ import annotations | ||
|
||
from marimo._cli.sandbox import _get_dependencies | ||
|
||
|
||
def test_get_dependencies(): | ||
SCRIPT = """ | ||
# Copyright 2024 Marimo. All rights reserved. | ||
# /// script | ||
# requires-python = ">=3.11" | ||
# dependencies = [ | ||
# "polars", | ||
# "marimo>=0.8.0", | ||
# "quak", | ||
# "vega-datasets", | ||
# ] | ||
# /// | ||
import marimo | ||
__generated_with = "0.8.2" | ||
app = marimo.App(width="medium") | ||
""" | ||
assert _get_dependencies(SCRIPT) == [ | ||
"polars", | ||
"marimo>=0.8.0", | ||
"quak", | ||
"vega-datasets", | ||
] | ||
|
||
|
||
def test_no_dependencies(): | ||
SCRIPT = """ | ||
import marimo | ||
__generated_with = "0.8.2" | ||
app = marimo.App(width="medium") | ||
""" | ||
assert _get_dependencies(SCRIPT) == [] |