ipywidgets icon indicating copy to clipboard operation
ipywidgets copied to clipboard

Accessing notebook from a widget

Open ultmaster opened this issue 2 years ago • 6 comments
trafficstars

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.

image image

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.

ultmaster avatar Aug 25 '23 07:08 ultmaster

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.

ibdafna avatar Aug 29 '23 13:08 ibdafna

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?

ultmaster avatar Aug 30 '23 16:08 ultmaster

You could probably use ipylab https://github.com/jtpio/ipylab to control the JupyterLab UI from the Python kernel

martinRenou avatar Aug 30 '23 18:08 martinRenou

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 avatar Aug 30 '23 18:08 maartenbreddels

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:

  1. Executing a "customized" command: inserting a certain code snippet in the next cell, focus on the next cell...
  2. 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.

ultmaster avatar Aug 30 '23 22:08 ultmaster

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?

ultmaster avatar Aug 30 '23 22:08 ultmaster