.. _entries: Working with Entries ==================== A LabArchives page contains entries for rich text, plain text, headers, file attachments, and widgets. Accessing Entries ----------------- A :class:`~labapi.tree.page.NotebookPage` exposes ``entries``, an :class:`~labapi.entry.collection.Entries` sequence. .. code-block:: python page = notebook.traverse("Project A/Experiment 1/Results") # Get the number of entries print(len(page.entries)) # Iterate over entries for entry in page.entries: print(f"ID: {entry.id}, Type: {entry.content_type}") # Access an entry by index first_entry = page.entries[0] Entry Metadata -------------- Every entry carries its LabArchives identifier and part type: :attr:`~labapi.entry.entries.base.Entry.id` and :attr:`~labapi.entry.entries.base.Entry.content_type`. Entries also have three read-only fields: :attr:`~labapi.entry.entries.base.Entry.created_at` and :attr:`~labapi.entry.entries.base.Entry.updated_at` are :class:`~datetime.datetime` objects parsed from the API's ISO-8601 timestamps, and :attr:`~labapi.entry.entries.base.Entry.version` is an integer. .. code-block:: python entry = page.entries[0] print(f"{entry.id} ({entry.content_type})") print(f"Version {entry.version}, last updated {entry.updated_at}") .. note:: These fields are set on entries loaded from a page and on search results. A field the response leaves out is ``None``. They are not set on an entry you have just made with :meth:`~labapi.entry.collection.Entries.create`; reload the page to get them. Attachment entries add a :attr:`~labapi.entry.entries.attachment.AttachmentEntry.caption`, and the :class:`~labapi.entry.attachment.Attachment` returned by their ``content`` carries :attr:`~labapi.entry.attachment.Attachment.filename`, :attr:`~labapi.entry.attachment.Attachment.mime_type`, and its own :attr:`~labapi.entry.attachment.Attachment.caption`. Searching Entries ----------------- Use :meth:`~labapi.tree.notebook.Notebook.search` to search entries in a notebook. It returns an :class:`~labapi.tree.search.EntrySearch`, an iterable of result pages. No API request is made until you iterate it or fetch a page. Each page contains the same entry classes used by ``page.entries``. .. code-block:: python from labapi import TextEntry for result_page in notebook.search('"brown rat"', page_size=100): for entry in result_page.entries: if isinstance(entry, TextEntry): entry.content = entry.content.replace( "brown rat", "Rattus norvegicus", ) Each result page is an :class:`~labapi.tree.search.EntrySearchPage`, which holds the matched entries and the result counts. To fetch one page without iterating, call :meth:`~labapi.tree.search.EntrySearch.page`: .. code-block:: python search = notebook.search("Rattus norvegicus") # page_size defaults to 25 first_page = search.page(0) # page numbers are zero-based print(f"{first_page.total_found} matching entries in the notebook") print(f"{first_page.total_returned} entries on this page") :meth:`~labapi.tree.search.EntrySearch.page` raises :class:`IndexError` for negative or out-of-range page numbers, and :meth:`~labapi.tree.notebook.Notebook.search` raises :class:`ValueError` if ``page_size`` is not positive. Entry Types ----------- Each entry type is a subclass of :class:`~labapi.entry.entries.base.Entry`. Text-based Entries ~~~~~~~~~~~~~~~~~~ Text-based entries store their content as strings. * **Rich Text Entry** (:class:`~labapi.entry.entries.text.TextEntry`): Formatted text, typically HTML. Content type: ``"text entry"``. * **Plain Text Entry** (:class:`~labapi.entry.entries.text.PlainTextEntry`): Unformatted text. Content type: ``"plain text entry"``. * **Header Entry** (:class:`~labapi.entry.entries.text.HeaderEntry`): A heading within a page. Content type: ``"heading"``. .. code-block:: python # Accessing text content text_entry = page.entries[0] print(text_entry.content) # Updating rich text content text_entry.content = "
Updated rich text content
" Attachment Entries ~~~~~~~~~~~~~~~~~~ The content of an :class:`~labapi.entry.entries.attachment.AttachmentEntry` is an :class:`~labapi.entry.attachment.Attachment` object. .. code-block:: python attachment_entry = page.entries[1] attachment = attachment_entry.content print(f"Filename: {attachment.filename}") print(f"MIME Type: {attachment.mime_type}") print(f"Caption: {attachment.caption}") # Read the file data data = attachment.read() .. warning:: ``attachment.filename`` comes from LabArchives unchecked and may contain path separators. Use ``pathlib.PureWindowsPath(attachment.filename).name`` before saving a file under it. Widget Entries ~~~~~~~~~~~~~~ Widget entries (:class:`~labapi.entry.entries.widget.WidgetEntry`) embed interactive content or external applications. Like text entries, their content is represented as a string (often JSON or HTML). .. note:: Widget entries are currently read-only in ``labapi``. :attr:`~labapi.tree.page.NotebookPage.entries` wraps part types it does not recognize as :class:`~labapi.entry.entries.unknown.UnknownEntry` and types it recognizes but does not implement as :class:`~labapi.entry.entries.unknown.UnimplementedEntry`, and emits a warning for each. Creating New Entries -------------------- Call :meth:`~labapi.entry.collection.Entries.create` to add an entry to a page. .. code-block:: python from labapi import TextEntry, HeaderEntry, PlainTextEntry # Create a rich text entry (HTML is rendered in LabArchives) page.entries.create(TextEntry, "Some formatted content...
") # Create a heading (displayed as a section label/divider) page.entries.create(HeaderEntry, "Experiment Notes") # Create a plain text entry (displayed literally) page.entries.create(PlainTextEntry, "