jupyter / jupyter_client

Jupyter protocol client APIs
https://jupyter-client.readthedocs.io
BSD 3-Clause "New" or "Revised" License
390 stars 283 forks source link

I'm trying to figure out a couple of cell operations #1041

Closed paddymul closed 1 month ago

paddymul commented 1 month ago

I work on a widget that does DataFrame visualization and codegen. I want the widget to be able create a cell immediately below it, and inject then edit code in that cell.

I want to create a new cell, then get a reference to that created cell (by id or something else) then be able to edit that cell.

I have found a code snippet to edit a cell, but I don't know how to edit a cell, or find the cell I just created.

from IPython.core.getipython import get_ipython

def create_new_cell(contents):
    shell = get_ipython()
    payload = dict(
        source='set_next_input',
        text=contents,
        replace=False,
    )
    return shell.payload_manager.write_payload(payload, single=False)

I have reviewed the docs here, and I don't obviously see how to do this? https://jupyter-client.readthedocs.io/en/latest/messaging.html

Any suggestions?

davidbrochart commented 1 month ago

jupyter-client doesn't know anything about notebooks. You can probably do that using JupyterLab's notebook API.

paddymul commented 1 month ago

Thank you. There are so many packages around jupyter that it's not obvious where to search for code or even help.

I started a discourse thread here https://discourse.jupyter.org/t/im-trying-to-figure-out-a-couple-of-cell-operations-from-python/29276