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.

chore: cleanup importfiles (#4212)

Myles Scolnick913a90380b13
diff --git a/marimo/_server/api/endpoints/assets.py b/marimo/_server/api/endpoints/assets.py
index 362c5021a3..98ab50447f 100644
--- a/marimo/_server/api/endpoints/assets.py
+++ b/marimo/_server/api/endpoints/assets.py
@@ -22,7 +22,7 @@ from marimo._server.templates.templates import (
inject_script,
notebook_page_template,
)
-from marimo._utils.paths import import_files
+from marimo._utils.paths import marimo_package_path
if TYPE_CHECKING:
from starlette.requests import Request
@@ -33,7 +33,7 @@ LOGGER = _loggers.marimo_logger()
router = APIRouter()
# Root directory for static assets
-root = Path(import_files("marimo").joinpath("_static")).resolve()
+root = (marimo_package_path() / "_static").resolve()
server_config = (
get_default_config_manager(current_path=None)
diff --git a/marimo/_server/export/exporter.py b/marimo/_server/export/exporter.py
index 1d91770a08..9b8f83b524 100644
--- a/marimo/_server/export/exporter.py
+++ b/marimo/_server/export/exporter.py
@@ -4,7 +4,6 @@ from __future__ import annotations
import base64
import io
import mimetypes
-import os
from pathlib import Path
from typing import (
TYPE_CHECKING,
@@ -46,12 +45,12 @@ from marimo._server.templates.templates import (
from marimo._server.tokens import SkewProtectionToken
from marimo._utils.data_uri import build_data_url
from marimo._utils.marimo_path import MarimoPath
-from marimo._utils.paths import import_files
+from marimo._utils.paths import marimo_package_path
LOGGER = _loggers.marimo_logger()
# Root directory for static assets
-root: str = os.path.realpath(str(import_files("marimo").joinpath("_static")))
+ROOT = (marimo_package_path() / "_static").resolve()
if TYPE_CHECKING:
from nbformat.notebooknode import NotebookNode # type: ignore
@@ -396,7 +395,7 @@ class Exporter:
import shutil
shutil.copytree(
- root,
+ ROOT,
dirpath,
dirs_exist_ok=True,
ignore=(
@@ -527,7 +526,7 @@ def get_html_contents() -> str:
with urllib.request.urlopen(url) as response:
return cast(str, response.read().decode("utf-8"))
- index_html = Path(root) / "index.html"
+ index_html = Path(ROOT) / "index.html"
return index_html.read_text(encoding="utf-8")
diff --git a/marimo/_server/lsp.py b/marimo/_server/lsp.py
index a815bdf146..29f7d010d9 100644
--- a/marimo/_server/lsp.py
+++ b/marimo/_server/lsp.py
@@ -14,7 +14,7 @@ from marimo._messaging.ops import Alert
from marimo._runtime.complete import _get_docstring
from marimo._server.utils import find_free_port
from marimo._tracer import server_tracer
-from marimo._utils.paths import import_files
+from marimo._utils.paths import marimo_package_path
LOGGER = _loggers.marimo_logger()
@@ -126,11 +126,8 @@ class CopilotLspServer(BaseLspServer):
)
def _lsp_bin(self) -> str:
- lsp_bin = os.path.join(
- str(import_files("marimo").joinpath("_lsp")),
- "index.js",
- )
- return lsp_bin
+ lsp_bin = marimo_package_path() / "_lsp" / "index.js"
+ return str(lsp_bin)
def get_command(self) -> list[str]:
lsp_bin = self._lsp_bin()
diff --git a/marimo/_server/start.py b/marimo/_server/start.py
index acde5af34e..051cdcd90e 100644
--- a/marimo/_server/start.py
+++ b/marimo/_server/start.py
@@ -24,7 +24,7 @@ from marimo._server.utils import (
)
from marimo._server.uvicorn_utils import initialize_signals
from marimo._tracer import LOGGER
-from marimo._utils.paths import import_files
+from marimo._utils.paths import marimo_package_path
DEFAULT_PORT = 2718
PROXY_REGEX = re.compile(r"^(.*):(\d+)$")
@@ -198,9 +198,7 @@ def start(
# reload=development_mode,
reload_dirs=(
[
- os.path.realpath(
- str(import_files("marimo").joinpath("_static"))
- )
+ str((marimo_package_path() / "_static").resolve()),
]
if development_mode
else None
diff --git a/marimo/_snippets/snippets.py b/marimo/_snippets/snippets.py
index b33ba0b9a9..4ba8be615e 100644
--- a/marimo/_snippets/snippets.py
+++ b/marimo/_snippets/snippets.py
@@ -9,7 +9,7 @@ from typing import Any, Optional
from marimo import _loggers
from marimo._ast.codegen import get_app
from marimo._config.manager import get_default_config_manager
-from marimo._utils.paths import import_files
+from marimo._utils.paths import marimo_package_path
LOGGER = _loggers.marimo_logger()
@@ -119,7 +119,7 @@ def read_snippet_filenames(
) -> Generator[str, Any, None]:
paths: list[Path] = []
if include_default_snippets:
- paths.append(import_files("marimo") / "_snippets" / "data")
+ paths.append(marimo_package_path() / "_snippets" / "data")
if custom_paths:
paths.extend([Path(p) for p in custom_paths])
for root_path in paths:
diff --git a/marimo/_utils/paths.py b/marimo/_utils/paths.py
index 565b17558b..c327c9fc31 100644
--- a/marimo/_utils/paths.py
+++ b/marimo/_utils/paths.py
@@ -2,18 +2,23 @@
from __future__ import annotations
import os
-from typing import TYPE_CHECKING, Any
+from pathlib import Path
+from typing import TYPE_CHECKING
if TYPE_CHECKING:
- from pathlib import Path
+ from importlib.abc import Traversable
-def import_files(filename: str) -> Any:
+def import_files(filename: str) -> Traversable:
from importlib.resources import files as importlib_files
return importlib_files(filename)
+def marimo_package_path() -> Path:
+ return Path(str(import_files("marimo")))
+
+
def pretty_path(filename: str) -> str:
"""
If it's an absolute path, shorten to relative path if