Autorun some code cells jupyterlab3

I have code cells with ipython widgets, whose input is hidden, and i want them to be running when opening. I found this here but it did not work on jupyterlab also after running it manually. Extension i found here is not compatible yet with jupyterlab 3.

3 Likes

@Oxana I might be misinterpreting your request, but it seems like this can be done in an extension. You can attach a handler to the Signal that fires when a notebook loads and then run cells using the functions in the NotebookActions namespace. If this sounds like what you need, let me know if I can assist.

@adpatter thanks a lot, that seems perfectly suitable if you could select via something only the hidden cells. I found under NotebookActions run(notebook: Notebook, sessionContext?: ISessionContext): Promise<boolean>, but not sure how to select all hidden cells and how to properly write an extension with the commands. So I really would appreciate your help.

This seems to be working:

const extension: JupyterFrontEndPlugin<object> = {
  id: "jupyterlab:plugin",
  autoStart: true,
  requires: [
    INotebookTracker
  ],
  activate: (
    app: JupyterFrontEnd,
    notebookTracker: INotebookTracker
  ) => {

    notebookTracker.widgetAdded.connect(async (tracker: INotebookTracker, notebookPanel: NotebookPanel) => {

      await notebookPanel.revealed;

      await notebookPanel.sessionContext.ready

      notebookPanel.content.widgets.forEach((cell: Cell<ICellModel>) => {
        if (!cell.isHidden) {
          CodeCell.execute((cell as CodeCell), notebookPanel.sessionContext);
        }
      })
    });

    return {};
  }
};

I followed the code from runCell in NotebookActions to find CodeCell.execute. I discovered that we need to wait for the sessionContext to become ready. It seems to work.

It assumes that the non-hidden cell is a CodeCell, which can be determined by looking at cell.type.

Presumably, cell.isHidden is true when the cell is hidden - but I didn’t test it.

2 Likes

Thanks for your work. I have put this in a textfile and dir autorun3. I have installed node.js and here it states, that one could do jupyter labextension install autorun3 for local extensions, but I get the error, that it is not a npm package. Maybe that’s out of the thread, that @adpatter has solved, but to be able to test it, could sb elaborate how to run that locally?

The process for putting together an extension is here: Extension Tutorial — JupyterLab 3.0.14 documentation

1 Like

I can throw the extension together for you real quick if it will be helpful. Let me know.

@adpatter I think that others would also profit from your extension, as I am working with it in a bigger Hub, i would be grateful if you could throw the extension together. Thank you very much for all your time.

Please have a look at this: GitHub - educational-technology-collective/etc-jupyterlab-cell-iterate-run

It isn’t a pip package, but you can install it into your current JL 3.0 instance by following these instructions: Extension Developer Guide — JupyterLab 3.0.14 documentation

Or, do a development install as shown in the README.

Please have a look here at how to put together extensions: GitHub - jupyterlab/extension-examples: JupyterLab Extensions by Examples

1 Like