Skip to content

Best practice inserting code into next cell #140

Open
@kolibril13

Description

@kolibril13

Here's a snippet to add a new cell and insert the text i=1 into that next cell.

from ipylab import JupyterFrontEnd
import time

app = JupyterFrontEnd()
app.commands.execute("notebook:insert-cell-below")
time.sleep(0.1)
app.commands.execute("notebook:replace-selection", {"text": "i = 1"})

It also works without time.sleep(0.1), but then it happens that in about 1 out of 10 cases it only inserts the cell, but does not add the text. Probably because the notebook:replace-selection is called before the insert-cell-below is finished. With time.sleep(0.1) I did not experience any issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions