Skip to content

Get cell by ID? #267

Open
Open
@krassowski

Description

@krassowski

Problem

Currently the API supports getting the cell by index. Index is not a stable identifier in a notebook.

Proposed Solution

It would be perfect to be able to fetch a cell by ID, without the overhead of converting the entire notebook to Python objects

Additional context

def get_cell(self, index: int) -> Dict[str, Any]:
"""
Returns a cell.
:param index: The index of the cell.
:type index: int
:return: A cell.
:rtype: Dict[str, Any]
"""
meta = self._ymeta.to_py()
cell = self._ycells[index].to_py()
cell.pop("execution_state", None)
cast_all(cell, float, int) # cells coming from Yjs have e.g. execution_count as float
if "id" in cell and meta["nbformat"] == 4 and meta["nbformat_minor"] <= 4:
# strip cell IDs if we have notebook format 4.0-4.4
del cell["id"]
if (
"attachments" in cell
and cell["cell_type"] in ("raw", "markdown")
and not cell["attachments"]
):
del cell["attachments"]
return cell

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions