jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.59k stars 4.86k forks source link

Javascript front end API for Notebook v7 #6949

Open kiyoon opened 1 year ago

kiyoon commented 1 year ago

Problem

In the neovim plugin jupynium.nvim it uses Notebook's front-end APIs to interact with the Notebook, allowing users to edit on vim and the changes are made to Notebook in real time.

Here is the list of functions that is used.

Jupyter.notebook.ncells()
Jupyter.notebook.cells_to_code([0])
Jupyter.notebook.cells_to_markdown([0])
Jupyter.notebook.get_cell(0).set_text(arguments[0])
Jupyter.notebook.get_cell(0).render()
Jupyter.notebook.get_cells()
Jupyter.notebook.insert_cell_below()
Jupyter.notebook.delete_cell(-1)
Jupyter.notebook.scroll_cell_percent(arguments[0], arguments[1], 0)
Jupyter.notebook.toJSON()    // this is to download notebook
Jupyter.notebook.scroll_manager.is_cell_visible(Jupyter.notebook.get_cell(arguments[0]))

Jupyter.notebook.kernel.name
Jupyter.kernelselector.kernelspecs
Jupyter.kernel_list.kernelspecs
Jupyter.kernelselector.set_kernel(arguments[0])
Jupyter.notebook.kernel.interrupt()
Jupyter.notebook.kernel.restart()
Jupyter.notebook.kernel.is_connected()
Jupyter.notebook.kernel.complete(arguments[0], arguments[1], completeCallback)
Jupyter.notebook.kernel.inspect(arguments[0], arguments[1], inspectCallback)

Jupyter.notebook.clear_output()
Jupyter.notebook.clear_cells_outputs(Jupyter.notebook.get_selected_cells_indices())
Jupyter.notebook.toggle_cells_outputs_scroll(Jupyter.notebook.get_selected_cells_indices())
Jupyter.notebook.execute_selected_cells()
Jupyter.notebook.save_checkpoint()
Jupyter.notebook.save_notebook()
Jupyter.notebook.scroll_manager.animation_speed = 0; Jupyter.notebook.scroll_manager.scroll_some(arguments[0])
Jupyter.notebook.rename(arguments[0])

Proposed Solution

If Notebook v7 provides a similar front-end API without needing to write extensions, it would be much easier to migrate.

Additional context

fecet commented 1 year ago

Jupynium is the only tool I know of that is used for serious production purposes with Jupyter Notebook, rather than just for tutorials or demonstrations. It connects the notebook with a fully featured editor (Neovim), allowing it to serve as the frontend for the editor.

I am eagerly looking forward to seeing this feature implemented in Notebook v7!

jtpio commented 1 year ago

Thanks @kiyoon and @fecet for reporting the issue and commenting :+1:

without needing to write extensions

So does that mean jupynium does not even use the classic notebook extension system at the moment? And instead execute these commands directly via the Selenium driver? Probably because jupynium is in full control of the browser as opposed to regular Jupyter use cases where a user opens Jupyter Notebook in the browser?

Notebook 7 provides a JavaScript / TypeScript API and it's the same as JupyterLab. But the recommended way would indeed be to write an extension and use the JupyterLab API. There is some documentation about this here and in related links on the page: https://jupyterlab.readthedocs.io/en/latest/extension/extension_dev.html

Probably the jupynium functionalities could be implemented as a set of commands, but that would still require writing an extension to add the commands. But writing a JupyterLab / Notebook 7 extension could also have the benefit of supporting JupyterLab as well and not just the Notebook interface.

To achieve the same without writing an extension might be a bit trickier at the moment. You could use the --expose-app-in-browser flag when starting Notebook 7 so it makes window.jupyterapp available in the browser. And then register commands and perform actions on jupyterapp directly.

jtpio commented 1 year ago

For reference there has also been some discussions about having a small compatibility layer for classic notebook extensions (as a separate package): https://github.com/jupyter/notebook/issues/6394

This could help with some of this, but not sure all the classic notebook APIs would be supported.

kiyoon commented 1 year ago

Hi @jtpio,

So does that mean jupynium does not even use the classic notebook extension system at the moment? And instead execute these commands directly via the Selenium driver?

Yes, this is correct. There are multiple benefits by going on this route.

  1. I don't know JS, but I could still write the whole plugin using Python only and just a few lines of JS.
  2. jupynium runs locally, and Jupyter Notebook can potentially run anywhere (e.g. a remote machine). If we write an extension, then updating the plugin requires matching the version of the extension on all the machines you are using. Currently, jupynium only interacts through front-end, making it possible to be compatible with every Notebook installed without worrying about extensions.
  3. Users don't need extra JS dependencies to manually install the plugin. The more dependency it's required, the more complicated the initial setup will be.
kiyoon commented 1 year ago

Is there any documentation for --expose-app-in-browser? I tried it but I don't know how I should find the equivalent commands.

For example, is there an equivalent of Jupyter.notebook.get_cell(0).set_text(arguments[0]) with this method?