ipywidgets
ipywidgets copied to clipboard
Accessing notebook from a widget
Problem
I'm trying to build a jupyter plugin with the ability to programmatically add cells, execute cells, and read a specific cell to write suggestions. Most features (e.g., code generation, intelligent suggestion) are already implemented in Python. In Jupyterlab, IIUC, there is no obvious way to control frontend from Python (like in nbclassic IPython.notebook.insert_cell_below). I looked into ipywidgets because it is recommended to be the "correct" place to implement frontend features.
I managed to run the ipyemail sample, and at the first glance it does satisfy what I need: the frontend listens to messages from the backend, it can also sends messages to backend, and the frontend is running in javascript. However, when I looked deeper, I found that the widget instance does not at all have the ability to get the current notebook instance it adheres to. I might miss things here, but the property list doesn't seem to contain a reference to the notebook.
Proposed Solution
My request is get the current notebook instance (something like this: https://github.com/jupyterlab/jupyterlab/blob/ac8f9e2f47b271785127757541a6fd8aa10c4d31/packages/notebook/src/widget.ts#L1263). Is it possible from ipywidget? If no, are there any other workarounds?
Thanks in advance.
Additional Context
Linking #2777 as it appears to be an opposite problem, which also has a workable solution with nbclassic, similar to my situation here.
I think that what you're looking for is a JupyterLab extension and not a widget. Although it's certainly possible to control the host notebook from a widget, it's not the primary objective of widgets and so there isn't a well defined API for this on the widget side.
Thanks for the clarification.
The problem with the REST API in JupyterLab extension is that I need a call from Python side to Javascript side, which is server to client. I can certainly poll. But that's definitely not an elegant solution.
The reason why I'm looking at jupyter-widgets is that it provides a clear API for the communication between notebook and the javascript side. This is what I'm struggling with during the previuos attempt with the pure JupyterLab extension.
I notice that customizing a widget is actually a special type of jupyter extension. My missing puzzle here is the communication between widget (javascript) and the rest functions jupyterlab has (e.g., Notebook in javascript). Any idea how these two can be connected? Or is there anything I can refer to?
You could probably use ipylab https://github.com/jtpio/ipylab to control the JupyterLab UI from the Python kernel
What we've (@mariobuikhuizen and I) done previously is to write a classic notebook extension and a jupyter lab extension . We made this API globally available (on the window object) and access it from a widget (in our case ipyvue or ipyvuetify).
You could probably use ipylab https://github.com/jtpio/ipylab to control the JupyterLab UI from the Python kernel
@martinRenou
From the examples of ipylab, I can see the ability to execute jupyterlab "commands" from Python kernel. In my case, I might need more customization. For example:
- Executing a "customized" command: inserting a certain code snippet in the next cell, focus on the next cell...
- Getting "feedbacks" from a command: get the content and the output (or error) of a previous cell.
I'm not sure about the effort needed with ipylab.
What we've (@mariobuikhuizen and I) done previously is to write a classic notebook extension and a jupyter lab extension . We made this API globally available (on the window object) and access it from a widget (in our case ipyvue or ipyvuetify).
@maartenbreddels Are you putting the widget implementation and the "global API" in the same lab extension? Sounds like a good example to me. May I see the code?