Skip to content

Utilities

Utilities for teaching with marimo notebooks.

localize_file(filename, url)

Download a file from the url, returning the local path.

Parameters:

Name Type Description Default
filename str

name for local copy of file

required
url str

URL of file to download

required

Returns:

Type Description
str

local file path

Raises:

Type Description
FileNotFoundError

if remote file not found

Source code in src/marimo_learn/utilities.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def localize_file(filename: str, url: str) -> str:
    """
    Download a file from the url, returning the local path.

    Args:
        filename: name for local copy of file
        url: URL of file to download

    Returns:
        local file path

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

    response = httpx.get(url)
    if response.status_code != 200:
        raise FileNotFoundError(f"unable to get file from '{url}'")

    local_path = mo.notebook_dir() / filename
    local_path.parent.mkdir(parents=True, exist_ok=True)
    with open(local_path, "wb") as writer:
        writer.write(response.content)

    return str(local_path)