Creating Pages and Entries#
The examples use a notebook object from Your First Entry.
Notebook Structure#
A notebook contains directories and pages, a directory contains other directories and pages, and a page contains entries arranged from top to bottom.
Create Directories and Pages#
Use create() to add new
NotebookDirectories or
NotebookPages.
from labapi import NotebookDirectory, NotebookPage
my_folder = notebook.create(NotebookDirectory, "Experiments")
experiment_page = my_folder.create(NotebookPage, "Experiment 1")
subfolder = my_folder.create(NotebookDirectory, "2024 Results")
Handle Existing Nodes#
By default, create() raises
NodeExistsError if a node with the same name and
type already exists. Use if_exists to choose a different behavior:
from labapi import InsertBehavior, NotebookPage
page = notebook.create(NotebookPage, "Existing Page", if_exists=InsertBehavior.Raise)
page = notebook.create(NotebookPage, "Existing Page", if_exists=InsertBehavior.Ignore)
page = notebook.create(NotebookPage, "Existing Page", if_exists=InsertBehavior.Retain)
page = notebook.create(NotebookPage, "Existing Page", if_exists=InsertBehavior.Replace)
Create Entries#
Use create() to add entries to a
page:
from labapi import Attachment, AttachmentEntry, HeaderEntry, PlainTextEntry, TextEntry
page.entries.create(HeaderEntry, "Experiment Results")
page.entries.create(TextEntry, "<p>This is <b>bold</b> text.</p>")
page.entries.create(PlainTextEntry, "Simple unformatted text")
page.entries.create(AttachmentEntry, Attachment.from_file("results.csv"))
Entry Types#
Entry Class |
Data Type |
Description |
|---|---|---|
|
Section headers and titles. |
|
|
Rich text with HTML formatting. |
|
|
Plain text. |
|
|
An uploaded file. |
Inspect What You Created#
page.entries can be iterated and indexed:
for entry in page.entries:
print(f"Type: {entry.content_type}")
print(f"Content: {entry.content}")
first_entry = page.entries[0]