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
I looked around here, but it’s not obvious how to perform the actions I’m thinking of. The notebook has all of the cell state available to it.
Any suggestions?
Payload manager was deprecated a long time ago. It has support for replace=True
but it was not updated to support concept of cell IDs which were added long after it was deprecated. You may have some luck with ipylab or modifying the notebook using shared models (see jupyter-server-ydoc in GitHub - jupyterlab/jupyter-collaboration: A Jupyter Server Extension Providing Support for Y Documents ).
Control JupyterLab from Python Notebooks with Jupyter Widgets 🧪 ☢️ 🐍
opened 08:48PM - 12 Sep 22 UTC
enhancement
status:Needs Discussion
tag:Extension Idea
[[
<!--
Thanks for thinking of a way to improve JupyterLab. If this solves a… problem for you, then it probably solves that problem for lots of people! So the whole community will benefit from this request.
Before creating a new feature request please search the issues for relevant feature requests.
-->
This is a follow-up on https://github.com/ipython/ipython/issues/4983, more appropriately located hopefully.
Also relevant: https://github.com/jupyter-widgets/ipywidgets/issues/242
### Problem
* I would like to create a cell in a notebook that contains some prewritten code.
* I'd probably like to trigger this via a widget, but I assume as long as the kernel can do it in some way, there is a way.
### Use Cases
* Exercises. I teach a lot of workshops, and they have assignments that come with solutions. For the last 8 years or so, I have put them into a single python file which I `%load`. If I walk through the solutions, I then make ample use of the cell splitting shortcut to run parts of it at a time. Being able to insert multiple cells at once would be amazing here.
* Providing code suggestions from a library. Sometimes a library might have a suggestion on how to do something, but doesn't want to actually do it for you, and instead provide code for you to show how it's done.
This came up multiple times in the last couple of years, so I assume other people have other use-cases.
### Proposed Solution
* Write a jupyterlab plug-in. My understanding is that this should be feasible (and I'll probably go ahead and try it). The problem is that this requires a plugin installation, which is tricky for beginner users. Also, it only applies to jupyterlab and needs to be re-written for each notebook interface.
* Make it a feature of the Notebook spec. I'm actually not sure in how far there's a common spec right now, but this seems to be outside the scope of the general kernel spec, as it only applies to notebooks? I'd still like it to work on multiple frontends, say jupyterlab, colab and VSCode. That seems like a big endeavor, though?
opened 01:44AM - 20 Dec 18 UTC
tag:Extension Idea
I would like to use jupyter lab to create an educational environment where stude… nts are able to be guided through content in a lab, using a jlab extension to drive the experience.
An example desired flow is for the student to be going through a notebook and take an action (e.g. click a button rendered inline within the notebook) that would open an editor panel with a specific file open, and also a terminal panel with a specific command running (to transition from working in the notebook to working on a project, for instance). Another example would be to click on some specially demarcated text within the notebook and have that automatically get sent to a running terminal panel as input (to give example commands next to a terminal the students could interact with directly).
Ideally it would be possible to do this entirely from a single jlab extension to make it easy to version edit and deploy the code, instead of a separate notebook extension that interfaces with a jlab extension. However, I'm not entirely sure what the best approach would be - would really appreciate some guidance, thank you!
1 Like