Skip to content

iorw

The iorw module provides the handlers for registration with papermill to read/write Notebooks

NoteableHandler #

Defines a class which implements the interface papermill needs to pull and push content from Noteable using notebook ids, version ids or noteable file URLs.

Source code in papermill_origami/iorw.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class NoteableHandler:
    """Defines a class which implements the interface papermill needs to pull and push content
    from Noteable using notebook ids, version ids or noteable file URLs.
    """

    def __init__(self, client: NoteableClient):
        """Tracks the noteable client to be used with the handler"""
        self.client = client

    @_ensure_client
    def read(self, path):
        """Reads a file from the noteable client by either version id, file id or file url"""
        id = parse_noteable_file_id(path)
        # Wrap the async call since we're in a blocking method
        file_version: FileVersion = run_sync(self.client.get_version_or_none)(id)
        if file_version is not None:
            resp = httpx.get(file_version.content_presigned_url)
            resp.raise_for_status()
            file_contents = resp.json()
        else:
            # Contents of this file is the last saved version (in-flight deltas are not squashed)
            file = run_sync(self.client.get_notebook)(id)
            file_contents = file.content
        # Should be a string but the type check also accept JSON
        return file_contents if isinstance(file_contents, str) else json.dumps(file_contents)

    def listdir(self, path):
        """Lists available files in a given path relative to the file's project"""
        from papermill.exceptions import (  # avoid circular imports due to papermill handler registration
            PapermillException,
        )

        raise PapermillException('listdir is not supported by NoteableHandler yet')

    def write(self, buf, path):
        """Writes a notebook file back to Noteable"""
        from papermill.exceptions import (  # avoid circular imports due to papermill handler registration
            PapermillException,
        )

        raise PapermillException('write is not supported by NoteableHandler yet')

    @classmethod
    def pretty_path(cls, path):
        """Used for logging"""
        return path

__init__(client) #

Tracks the noteable client to be used with the handler

Source code in papermill_origami/iorw.py
43
44
45
def __init__(self, client: NoteableClient):
    """Tracks the noteable client to be used with the handler"""
    self.client = client

listdir(path) #

Lists available files in a given path relative to the file's project

Source code in papermill_origami/iorw.py
64
65
66
67
68
69
70
def listdir(self, path):
    """Lists available files in a given path relative to the file's project"""
    from papermill.exceptions import (  # avoid circular imports due to papermill handler registration
        PapermillException,
    )

    raise PapermillException('listdir is not supported by NoteableHandler yet')

pretty_path(path) classmethod #

Used for logging

Source code in papermill_origami/iorw.py
80
81
82
83
@classmethod
def pretty_path(cls, path):
    """Used for logging"""
    return path

read(path) #

Reads a file from the noteable client by either version id, file id or file url

Source code in papermill_origami/iorw.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@_ensure_client
def read(self, path):
    """Reads a file from the noteable client by either version id, file id or file url"""
    id = parse_noteable_file_id(path)
    # Wrap the async call since we're in a blocking method
    file_version: FileVersion = run_sync(self.client.get_version_or_none)(id)
    if file_version is not None:
        resp = httpx.get(file_version.content_presigned_url)
        resp.raise_for_status()
        file_contents = resp.json()
    else:
        # Contents of this file is the last saved version (in-flight deltas are not squashed)
        file = run_sync(self.client.get_notebook)(id)
        file_contents = file.content
    # Should be a string but the type check also accept JSON
    return file_contents if isinstance(file_contents, str) else json.dumps(file_contents)

write(buf, path) #

Writes a notebook file back to Noteable

Source code in papermill_origami/iorw.py
72
73
74
75
76
77
78
def write(self, buf, path):
    """Writes a notebook file back to Noteable"""
    from papermill.exceptions import (  # avoid circular imports due to papermill handler registration
        PapermillException,
    )

    raise PapermillException('write is not supported by NoteableHandler yet')