Navigating the Tree#
The examples below use a Notebook named
notebook, obtained as in Your First Entry.
Warning
Several nodes can share a name. See Accessing Items with Index for how lookup by name handles that.
Traversing the Tree with Paths#
Use traverse(), which accepts a
slash-separated path string.
page = notebook.traverse("Experiments/Project A/Results")
experiments = notebook.traverse("Experiments")
project_a = experiments.traverse("Project A")
Fluent Navigation with dir() and page()#
dir() and
page() return the named node,
creating it if it is missing, so calls can be chained.
page = notebook.dir("Experiments").dir("Project A").page("Results")
page = notebook.dir("Experiments/Project A").page("Results")
To control what happens when the node already exists or a parent is missing,
use create().
Accessing Children by Name#
Use dictionary-style indexing to get the first child with a matching name:
experiments = notebook["Experiments"]
project_a = experiments["Project A"]
Note
To be sure of getting one specific node, look it up by ID with
Id.
Accessing Children by ID or Name#
Use Index when you need to choose ID or name
lookup explicitly.
Access by ID#
from labapi import Index
page = notebook[Index.Id:"123.45"]
Access by Name#
from labapi import Index
results_pages = notebook[Index.Name:"Results"]
Enumerating Children#
Notebook and
NotebookDirectory have methods that list their
children.
List All Children#
all_items = notebook.enumerate_all()
all_items = notebook.enumerate_all(depth=3)
experiments = notebook["Experiments"]
experiment_items = experiments.enumerate_all(depth=2)
depth limits how many levels of the tree are walked. Given this tree
structure:
Notebook/
|-- Experiments/
| |-- 2024/
| | `-- Results
| `-- Archive
`-- Notes
depth=1returns["Experiments", "Notes"].depth=2returns["Experiments", "Experiments/2024", "Experiments/Archive", "Notes"].depth=3returns["Experiments", "Experiments/2024", "Experiments/2024/Results", "Experiments/Archive", "Notes"].
List Only Directories#
directories = notebook.enumerate_dirs()
directories = notebook.enumerate_dirs(depth=2)
experiments = notebook["Experiments"]
subdirs = experiments.enumerate_dirs()
List Only Pages#
pages = notebook.enumerate_pages()
pages = notebook.enumerate_pages(depth=2)
experiments = notebook["Experiments"]
experiment_pages = experiments.enumerate_pages(depth=2)
Accessing Parent and Root#
Every node except the root has a
parent. Any node can also
reach the notebook root through
root.
page = notebook.traverse("Experiments/Project A/Results")
project_a = page.parent
notebook_root = page.root
Type-Safe Directory Access#
Use as_dir() when you need to
tell a type checker that a node is a directory:
from labapi import NotebookPage
node = notebook.traverse("Experiments/Project A")
if node.is_dir():
directory = node.as_dir()
directory.create(NotebookPage, "New Page")
Related Pages#
Your First Entry for getting the initial
notebookobject.Working with Paths for how paths are parsed and resolved.
Accessing Items with Index for duplicate-name and explicit lookup behavior.