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.

marimo convert: from github url (#384)

Akshay Agrawal9546355d562f
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.py
index e7c5495947..652a50d460 100644
--- a/marimo/_cli/cli.py
+++ b/marimo/_cli/cli.py
@@ -382,6 +382,9 @@ def tutorial(
def convert(ipynb: str) -> None:
"""Convert a Jupyter notebook to a marimo notebook.
+ The argument may be either a path to a local .ipynb file,
+ or an .ipynb file hosted on GitHub.
+
Example usage:
marimo convert your_nb.ipynb > your_app.py
diff --git a/marimo/_cli/file_path.py b/marimo/_cli/file_path.py
index 595c9a9c46..b01c2459b1 100644
--- a/marimo/_cli/file_path.py
+++ b/marimo/_cli/file_path.py
@@ -15,6 +15,26 @@ import click
from marimo._utils.url import is_url
+def is_github_src(url: str, ext: str) -> bool:
+ if not is_url(url):
+ return False
+
+ hostname = urllib.parse.urlparse(url).hostname
+ if hostname != "github.com" and hostname != "raw.githubusercontent.com":
+ return False
+ path: str = urllib.parse.urlparse(url).path
+ if not path.endswith(ext):
+ return False
+ return True
+
+
+def get_github_src_url(url: str) -> str:
+ # Change hostname to raw.githubusercontent.com
+ path = urllib.parse.urlparse(url).path
+ path = path.replace("/blob/", "/", 1)
+ return f"https://raw.githubusercontent.com{path}"
+
+
def validate_name(
name: str, allow_new_file: bool
) -> tuple[str, Optional[TemporaryDirectory[str]]]:
@@ -42,9 +62,9 @@ def validate_name(
if path.suffix != ".py":
raise click.UsageError("Invalid NAME - %s is not a Python file" % name)
- if _is_github_py(name):
+ if is_github_src(name, ext=".py"):
temp_dir = TemporaryDirectory()
- return _handle_github_py(name, temp_dir), temp_dir
+ return _handle_github_src(name, temp_dir), temp_dir
if is_url(name):
return _create_tmp_file_from_url(name, temp_dir), temp_dir
@@ -91,24 +111,8 @@ def _find_python_code_in_github_issue(body: str) -> str:
return body.split("```python")[1].rsplit("```", 1)[0]
-def _is_github_py(url: str) -> bool:
- if not is_url(url):
- return False
-
- hostname = urllib.parse.urlparse(url).hostname
- if hostname != "github.com" and hostname != "raw.githubusercontent.com":
- return False
- path: str = urllib.parse.urlparse(url).path
- if not path.endswith(".py"):
- return False
- return True
-
-
-def _handle_github_py(url: str, temp_dir: TemporaryDirectory[str]) -> str:
- # Change hostname to raw.githubusercontent.com
- path = urllib.parse.urlparse(url).path
- path = path.replace("/blob/", "/", 1)
- url = f"https://raw.githubusercontent.com{path}"
+def _handle_github_src(url: str, temp_dir: TemporaryDirectory[str]) -> str:
+ url = get_github_src_url(url)
path_to_app = _create_tmp_file_from_url(url, temp_dir)
return path_to_app
diff --git a/marimo/_cli/ipynb_to_marimo.py b/marimo/_cli/ipynb_to_marimo.py
index 24181af913..4c05cd3470 100644
--- a/marimo/_cli/ipynb_to_marimo.py
+++ b/marimo/_cli/ipynb_to_marimo.py
@@ -2,14 +2,23 @@
from __future__ import annotations
import json
+import urllib.request
from marimo._ast import codegen
from marimo._ast.cell import CellConfig
+from marimo._cli.file_path import get_github_src_url, is_github_src
def convert(ipynb_path: str) -> str:
- with open(ipynb_path, "r") as f:
- notebook = json.loads(f.read())
+ if is_github_src(ipynb_path, ext=".ipynb"):
+ notebook = json.loads(
+ urllib.request.urlopen(get_github_src_url(ipynb_path))
+ .read()
+ .decode("utf-8")
+ )
+ else:
+ with open(ipynb_path, "r") as f:
+ notebook = json.loads(f.read())
sources = []
has_markdown = False
for cell in notebook["cells"]:
diff --git a/marimo/_cli/test_file_path.py b/marimo/_cli/test_file_path.py
index eb621e4787..02954c3054 100644
--- a/marimo/_cli/test_file_path.py
+++ b/marimo/_cli/test_file_path.py
@@ -12,7 +12,7 @@ from marimo._cli.file_path import (
_find_python_code_in_github_issue,
_handle_github_issue,
_is_github_issue_url,
- _is_github_py,
+ is_github_src,
validate_name,
)
@@ -57,12 +57,12 @@ def test_is_github_issue_url_with_valid_url() -> None:
assert _is_github_issue_url(invalid_url) is False
-def test_is_github_py_with_valid_url() -> None:
+def test_is_github_src_with_valid_url() -> None:
valid_url = "https://github.com/marimo-team/marimo/blob/main/example.py"
- assert _is_github_py(valid_url) is True
+ assert is_github_src(valid_url, ext=".py") is True
invalid_url = "https://github.com/marimo-team/marimo/blob/main/example.txt"
- assert _is_github_py(invalid_url) is False
+ assert is_github_src(invalid_url, ext=".py") is False
@patch("urllib.request.urlopen")