How to create a new command that combines existing commands?

Hello, I’m a data scientist, and am looking for some guidance on my first jupyterlab extension.

I’m looking to create a new command (called restart-kernel-and-run-all-and-git-add) that would save me a few clicks by combining a few steps together in a single menu item.

The hope was to combine restartAndRunAll, an existing command in the RunMenu, with git add.

Is there a way to take the commandID for Runmenu:restart-and-run-all and combine it with the git add command?

Many thanks in advance!

1 Like

Welcome!

Before slinging some TS, to get a feel for the commands, I recommend trying out ipylab. This might get you what you need!

Once you’re ready to do it the hard way:

conda install -c conda-forge nodejs cookiecutter
cookiecutter https://github.com/jupyterlab/extension-cookiecutter-ts

Then in your shiny new package, you want your activate function to look something like:

activate: (app: JupyterFrontEnd) => {
  const {commands} = app;
  app.commands.register("restart-kernel-and-run-all-and-git-add", {
    execute: async (args) => {
      await commands.execute('runmenu:restart-and-run-all');
      //... wait for the kernel to be idle... kinda tricky!
      //... find the right command, or use `fetch`
    },
  });
};

A quick look didn’t reveal anything in jupyterlab-git on the client side… but maybe you can ask!

Going a little deeper: I think a user-serviceable macro system would be lovely. It’s probably a (needfully) loose JSON schema. This is a thing I’ve never actually sat down to write, but have wanted so many times. Even had a clever name in mind, comacho

I’ve got a 3 point plan that’s going to fix EVERYTHING.

3 Likes

There is a command that takes as arguments a list of other commands to run, and it goes through and runs the first enabled command: apputils:run-first-enabled (see JupyterLab Changelog — JupyterLab 3.0.14 documentation for an example of using it). It is defined at jupyterlab/index.ts at f241c2021fdfdcbcaaa8cbf098e229a37ea739dd · jupyterlab/jupyterlab · GitHub . I could imagine having another command defined there that would similarly take a list of commands and run all of the enabled ones. Then you could just call that command with a list of other commands and you’d have your sequence.

1 Like

Thanks so much @jasongrout and @bollwyvl for the guidance.

Based on the example @jasongrout shared, I put this together.

Works fine w/ the two test commands I tried, but maybe there’s a more elegant solution that includes the existing apputils:run-first-enabled command?

1 Like

This would be actually useful to have it in the JupyterLab by default. Maybe it would be worth contributing as a new feature?

I can imagine it could speed up our (lsp) interface tests too.

Happy to do so if it’s useful. Looks like I should start by opening up an issue, and then creating a PR.

1 Like

The PR is here, though I’m not sure how to add reviewers

1 Like