I’m writing a Jupyter Lab extension. I would like to have a notebook which acts as a kind of template for some visualizations. The notebook would have all the Python code needed to produce a visualization, except without the data. I would like to dynamically add an initial cell that makes a pandas dataframe available. The idea would be, the user will interact with the Jupyter lab extension, select a dataset to visualize, at which point the extension would open the notebook file and insert the selected pandas dataframe as the first cell. How can this be done? Is there a better way to do it?
jupyterlab-snippets extension can serve as an example?
It inserts a new cell in the notebook when the user clicks on a snippet, using
For your use case, we could imagine that the top cell of the notebook is selected first, and then a new cell is inserted above. Something like this:
const current = notebookTracker.currentWidget; const notebook = current.content; notebook.activeCellIndex = 0; NotebookActions.insertAbove(notebook);
I have used the Jupyter specific percentage sign commands to import Jupyter Notebooks that served as libraries for that purpose. The imported functions used plenty of display invocations to create the long output. Such a solution is maybe less beautiful but it gets its work done.