Skip to content

Marimo learn

marimo learning utilities

is_pyodide()

Is this notebook running in pyodide?

Source code in src/marimo_learn/__init__.py
15
16
17
18
def is_pyodide():
    """Is this notebook running in pyodide?"""

    return "pyodide" in sys.modules

localize_file(filepath)

Download a file from the 'public' directory, returning the local path.

Parameters:

Name Type Description Default
filepath str

path relative to 'public' directory

required

Returns:

Type Description
str

local file path

Raises:

Type Description
FileNotFoundError

if remote file not found

Source code in src/marimo_learn/__init__.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def localize_file(filepath: str) -> str:
    """
    Download a file from the 'public' directory, returning the
    local path.

    Args:
        filepath: path relative to 'public' directory

    Returns:
        local file path

    Raises:
        FileNotFoundError: if remote file not found
    """

    if not is_pyodide():
        return str(Path("public") / filepath)

    url = mo.notebook_location() / "public" / filepath
    response = https.get(url)
    if response.status_code != 200:
        raise FileNotFoundError(f"unable to get {filepath} from {url}")

    local_path = Path("public") / filepath
    with open(mo.notebook_dir() / local_path, "wb") as writer:
        writer.write(response.content)

    return str(local_path)