Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get cell by ID? #267

Open
krassowski opened this issue Aug 30, 2024 · 3 comments
Open

Get cell by ID? #267

krassowski opened this issue Aug 30, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@krassowski
Copy link
Collaborator

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

@krassowski krassowski added the enhancement New feature or request label Aug 30, 2024
@krassowski
Copy link
Collaborator Author

Since self._ycells is a list this may require holding a hashmap/dict from cell id to cell index. This may be hard, but ultimately may be worth it.

@davidbrochart
Copy link
Collaborator

Would something like that be enough?

def get_cell_by_id(self, id):
    for ycell in self.ycells:
        _id = ycell.get("id")
        if _id == id:
            return ycell

@krassowski
Copy link
Collaborator Author

This is what I am doing right now, this request is more about having a way to fetch it in O(1) rather than O(n).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants