labapi.tree.notebook.Notebook#
- class labapi.tree.notebook.Notebook(
- init: NotebookInit,
- user: User,
- notebooks: Notebooks,
Bases:
AbstractTreeContainerRepresents a LabArchives notebook, acting as the root of a tree structure.
A notebook is a specialized
AbstractTreeContainerthat holds directories and pages.- __init__(
- init: NotebookInit,
- user: User,
- notebooks: Notebooks,
Initialize a notebook.
- Parameters:
init – Initial data for the notebook.
user – The authenticated user.
notebooks – The collection of notebooks this notebook belongs to.
Methods
__init__(init, user, notebooks)Initialize a notebook.
Return
(name, child)pairs in container order, preserving duplicates.all_keys()Return child names in container order, preserving duplicates.
Return child nodes in container order, preserving duplicates.
as_dir()Return this node cast to
AbstractTreeContainer.as_page()Return this node cast to
NotebookPage.backup(destination, *[, ...])Download this notebook's native LabArchives backup archive.
create(cls, name, *[, parents, if_exists])Create a child page or directory in this container.
dir(name)Ensure a directory exists at
nameand return it.enumerate_all(*[, depth, timeout])Enumerate descendant directory and page paths.
enumerate_dirs(*[, depth, timeout])Enumerate descendant directory paths.
enumerate_nodes(*[, depth, timeout])Enumerate descendant paths paired with their concrete node objects.
enumerate_pages(*[, depth, timeout])Enumerate descendant page paths.
export(destination, *[, source, overwrite])Export this notebook to a local directory tree.
get(k[,d])is_dir()Return
Truebecause containers are directories.is_parent_of(other)Return whether this container is a strict ancestor of
other.items()Return a mapping-compatible view of
(name, child)pairs.keys()Return a mapping-compatible view of child names.
page(name)Ensure a page exists at
nameand return it.refresh()Refresh this container by clearing its cached children.
search(query, *[, page_size])Search entries in this notebook.
traverse(path)Traverse the notebook tree and return the node at
path.values()Return a mapping-compatible view of child nodes.
Attributes
Return a snapshot of this container's direct children.
Return the notebook identifier.
Return whether this notebook is the user's default notebook.
Return the tree node name.
Return this node's parent container.
Return the cached absolute path for this node.
Return the root node of the tree.
Return the node identifier within the LabArchives tree.
Return the authenticated user associated with this node.
- property is_default: bool#
Return whether this notebook is the user’s default notebook.
- Returns:
True if the notebook is the default, False otherwise.
- search( ) EntrySearch[source]#
Search entries in this notebook.
Search itself is read-only. The returned pages contain normal entry objects, so setting
entry.contentstill uses the existing entry update behavior.- Parameters:
query – LabArchives search query expression.
page_size – Number of entries to request per result page.
- Returns:
A lazy iterable over search result pages.
- export(
- destination: str | PathLike[str],
- *,
- source: Literal['auto', 'walk', 'backup'] = 'auto',
- overwrite: bool = False,
Export this notebook to a local directory tree.
Reconstructs the notebook as a folder mirror under
destination: one directory per folder/page (named<order>_<name>), with each page’s content written as ordered files (NN_text.htmlfor rich text,NN_text.txtfor plain text,NN_<filename>for attachments).- Parameters:
destination – Output directory for the exported tree. Its parent directories are created if needed.
source – Where to read the notebook from.
"walk"reads the live API tree;"backup"downloads and unpacks the native backup archive (requires the notebook owner’s sign-in and the optionalpy7zrdependency:pip install 'labapi[export]');"auto"uses the backup archive when available and falls back to the walk.overwrite – When
destinationexists and is not empty, raise unlessoverwriteisTrue.
- Returns:
A completed
NotebookExport. Itspathis the export directory.
- backup( ) Path[source]#
Download this notebook’s native LabArchives backup archive.
LabArchives returns the backup as a 7-Zip (
.7z) archive in a proprietary format;labapisaves it verbatim. This format may change at any time, but should remain interpretable and restorable by LabArchives.- Parameters:
destination – A filesystem path to write the archive to; its parent directories are created if needed.
include_attachments – When
False, request a backup without attachment payloads (the APIno_attachmentsoption).as_json – When
True, request the notebook data in JSON format (the APIjsonoption).
- Returns:
The
Paththe archive was written to.- Raises:
ApiError – If LabArchives rejects the request. Error code
4547means the currently authenticated account is not the notebook owner. Check the owner in the LabArchives web UI, then retry with that owner’s credentials.
- all_items() Sequence[tuple[str, AbstractBaseTreeNode]]#
Return
(name, child)pairs in container order, preserving duplicates.
- all_values() Sequence[AbstractBaseTreeNode]#
Return child nodes in container order, preserving duplicates.
- as_dir() AbstractTreeContainer#
Return this node cast to
AbstractTreeContainer.Use after checking
is_dir()to obtain a container-typed reference for static type checking.- Returns:
The node cast to an
AbstractTreeContainer.- Raises:
TypeError – If the node is not a directory (i.e., is_dir() returns False).
- as_page() NotebookPage#
Return this node cast to
NotebookPage.Use after checking
is_dir()to obtain a page-typed reference for static type checking.- Returns:
The node cast to a
NotebookPage.- Raises:
TypeError – If the node is not a page (i.e., is_dir() returns True).
- property children: Sequence[AbstractTreeNode]#
Return a snapshot of this container’s direct children.
- Returns:
An immutable point-in-time sequence of
AbstractTreeNodeobjects.
- create(
- cls: type[T],
- name: str | NotebookPath,
- *,
- parents: bool = False,
- if_exists: InsertBehavior = InsertBehavior.Raise,
Create a child page or directory in this container.
- Parameters:
cls – The class of the node to create (e.g.,
NotebookPageorNotebookDirectory).name – The name of the new node.
parents – If True, intermediate directories in the path will be created using InsertBehavior.Retain if they don’t exist.
if_exists – The behavior to take if a node with the same name and type already exists. Defaults to
InsertBehavior.Raise.
- Returns:
The newly created (or existing) node of type cls.
- Raises:
NodeExistsError – If
if_existsisInsertBehavior.Raiseand the node already exists.
- dir(name: str | NotebookPath) NotebookDirectory#
Ensure a directory exists at
nameand return it.Shorthand for
create()withcls=NotebookDirectory,if_exists=InsertBehavior.Retain, andparents=True.- Parameters:
name – The name or path of the directory.
- Returns:
The ensured
NotebookDirectory.
- enumerate_all( ) Sequence[str]#
Enumerate descendant directory and page paths.
Returns relative path strings from the current container for all descendant nodes, including both directories and pages. Each path is relative to this container (e.g., “Folder/Page” or “Folder/Subfolder/Page”).
- Parameters:
depth – The maximum depth to traverse. Default is 1 (only immediate children).
timeout – The maximum time to spend enumerating children. Defaults to 5 seconds.
- Returns:
A sequence of relative path strings for all descendants.
- enumerate_dirs( ) Sequence[str]#
Enumerate descendant directory paths.
Returns relative path strings from the current container for all descendant directories (excluding pages). Each path is relative to this container.
- Parameters:
depth – The maximum depth to traverse. Default is 1 (only immediate children).
timeout – The maximum time to spend enumerating children. Defaults to 5 seconds.
- Returns:
A sequence of relative path strings for all descendant directories.
- enumerate_nodes( ) Sequence[tuple[str, AbstractTreeNode]]#
Enumerate descendant paths paired with their concrete node objects.
Returns relative path strings from the current container for all descendant nodes, including both directories and pages, paired with the exact in-memory node instance each path came from.
- Parameters:
depth – The maximum depth to traverse. Default is 1 (only immediate children).
timeout – The maximum time to spend enumerating children. Defaults to 5 seconds.
- Returns:
A sequence of
(relative_path, node)pairs for all descendants.
- enumerate_pages( ) Sequence[str]#
Enumerate descendant page paths.
Returns relative path strings from the current container for all descendant pages (excluding directories). Each path is relative to this container.
- Parameters:
depth – The maximum depth to traverse. Default is 1 (only immediate children).
timeout – The maximum time to spend enumerating children. Defaults to 5 seconds.
- Returns:
A sequence of relative path strings for all descendant pages.
- get(
- k[,
- d,]
- is_parent_of(
- other: AbstractBaseTreeNode,
Return whether this container is a strict ancestor of
other.This method returns
Truewhenotheris a descendant of this container at any depth (direct child or deeper). A node is not considered a parent of itself.Nodes from different notebook roots are always considered unrelated, even if their relative paths happen to match.
- Parameters:
other – The node to test as a potential descendant.
- Returns:
Trueif this container is an ancestor ofother, otherwiseFalse.
- items() ItemsView[str, AbstractBaseTreeNode]#
Return a mapping-compatible view of
(name, child)pairs.
- page(name: str | NotebookPath) NotebookPage#
Ensure a page exists at
nameand return it.Shorthand for
create()withcls=NotebookPage,if_exists=InsertBehavior.Retain, andparents=True.- Parameters:
name – The name or path of the page.
- Returns:
The ensured
NotebookPage.
- property parent: AbstractTreeContainer#
Return this node’s parent container.
- Returns:
The parent tree container.
- property path: NotebookPath#
Return the cached absolute path for this node.
- refresh() Self#
Refresh this container by clearing its cached children.
This method clears the internal children cache, forcing the container to re-fetch its children from the LabArchives API on the next access.
- property root: AbstractTreeContainer#
Return the root node of the tree.
- Returns:
The root tree container.
- traverse(
- path: str | NotebookPath,
Traverse the notebook tree and return the node at
path.String path segments should be separated by ‘/’. Each segment is treated as a name to look up in the current container. Paths starting with ‘/’ are absolute (relative to the notebook root), while paths without a leading ‘/’ are relative to the current container.
Special path segments: - ‘..’ navigates to the parent container
Note
When multiple children have the same name, this method returns the first match.
Warning
Nodes with names that are literally ‘..’ cannot be accessed via this method, as ‘..’ is reserved for parent navigation.
- Parameters:
path – The slash-separated path to the desired node (e.g., “My Folder/My Page” or “/Folder/Subfolder/Page”).
- Returns:
The
AbstractBaseTreeNodefound at the specified path.- Raises:
TraversalError – If traversal cannot continue through a segment.
- property tree_id: str#
Return the node identifier within the LabArchives tree.
Equal to id for all nodes except the Notebook root, whose tree_id is the sentinel “0” while id is the notebook ID.
- Returns:
The tree ID of the node.
- property user: User#
Return the authenticated user associated with this node.
- Returns:
The user object.
- values() ValuesView[AbstractBaseTreeNode]#
Return a mapping-compatible view of child nodes.