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

Best practice inserting code into next cell #140

Open
kolibril13 opened this issue May 29, 2024 · 2 comments
Open

Best practice inserting code into next cell #140

kolibril13 opened this issue May 29, 2024 · 2 comments

Comments

@kolibril13
Copy link

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.

@paddymul
Copy link

I'm trying to do similar things. Is there any way to determine the current cell-cursor position? I want to update the text of a single cell repeatedly from a widget

@jtpio
Copy link
Owner

jtpio commented Oct 23, 2024

Is there any way to determine the current cell-cursor position? I want to update the text of a single cell repeatedly from a widget

You could try using the notebook:replace-selection command directly, as it applies to the current cell.

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

No branches or pull requests

3 participants