Set up the development environment and print to the console.
- The template folder structure
- A minimal extension that prints to the browser console
- Building and Installing an Extension
Writing a JupyterLab extension usually starts from a configurable template. It
can be downloaded with the cookiecutter tool and the following command:
cookiecutter https://github.com/jupyterlab/extension-cookiecutter-tscookiecutter asks for some basic information that could for example be setup
like this:
author_name []: tuto
extension_name [myextension]: hello-world
project_short_description [A JupyterLab extension.]: minimal lab example
repository [https://github.com/my_name/myextension]:The cookiecutter creates the directory hello-world [or your extension name]
that looks like this:
hello-world/
│ .eslintignore
│ .eslintrc.js
│ .gitignore
│ .prettierignore
│ .prettierrc
│ LICENSE
│ package.json
│ README.md
│ tsconfig.json
│
├───.github
│ └───workflows
│ build.yml
│
├───src
│ index.ts
│
└───style
index.cssThose files can be separated in 3 groups:
- Information about the extension:
README.mdcontains some instructionsLICENSEcontains your extension code license; BSD-3 Clause by default (but you can change it).
- Extension code (those files are mandatory):
package.jsoncontains information about the extension such as dependenciestsconfig.jsoncontains information for the typescript compilationsrc/index.tsthis contains the actual code of your extensionstyle/index.csscontains style elements that you can use
- Validation:
.prettierrcand.prettierignorespecify the code formatterprettierconfiguration.eslintrc.jsand.eslintignorespecify the code lintereslintconfiguration.github/workflows/build.ymlsets the continuous integration tests of the code using GitHub Actions
The following sections will walk you through the extension code files.
Start with the file src/index.ts. This typescript file contains the main
logic of the extension. It begins with the following import section:
// src/index.ts#L1-L4
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';JupyterFrontEnd is the main Jupyterlab application class. It allows you to
access and modify some of its main components. JupyterFrontEndPlugin is the class
of the extension that you are building. Both classes are imported from a package
called @jupyterlab/application. The dependency of your extension on this
package is declared in the file package.json:
// package.json#L36-L38
"dependencies": {
"@jupyterlab/application": "^2.0.0"
},With this basic import setup, you can move on to construct a new instance
of the JupyterFrontEndPlugin class:
// src/index.ts#L9-L12
const extension: JupyterFrontEndPlugin<void> = {
id: 'hello-world',
autoStart: true,
activate: (app: JupyterFrontEnd) => {// src/index.ts#L13-L13
console.log('the JupyterLab main application:', app);// src/index.ts#L17-L17
export default extension;A JupyterFrontEndPlugin contains a few attributes:
id: the unique id of the extensionautoStart: a flag to start the extension automatically or notactivate: a function (() => {}notation) that takes one argumentappof typeJupyterFrontEndand will be called by the main application to activate the extension.
app is simply the main JupyterLab application. The activate function acts as an entry
point into the extension. In this example, it calls the console.log function to output
something into the browser developer tools console.
Your new JupyterFrontEndPlugin instance has to be finally exported to be visible to
JupyterLab, which is done with the line export default extension.
Now that the extension code is ready, you need to install it within JupyterLab.
These are the instructions on how your extension can be installed for development:
The
jlpmcommand is JupyterLab's pinned version of yarn that is installed with JupyterLab. You may useyarnornpmin lieu ofjlpmbelow.
# Clone the repo to your local environment
# Move to hello-world directory
# Install dependencies
jlpm
# Build Typescript source
jlpm build
# Link your development version of the extension with JupyterLab
jupyter labextension link .The first command installs the dependencies that are specified in
package.json. Among the dependencies are also all of the JupyterLab
components that you want to use in your project.
The second step runs the build script. In this step, the TypeScript code gets
converted to javascript using the compiler tsc and stored in a lib
directory. Finally, the module is linked to JupyterLab.
After all of these steps are done, running jupyter labextension list should
show something like:
local extensions:
@jupyterlab-examples/hello-world: [...]/basics/hello-worldNow let's check inside of JupyterLab if it works. Run [can take a while]:
jupyter lab --watchYour extension writes something to the browser console. In most web browsers you can
open the console pressing the F12 key. You should see something like:
JupyterLab extension hello-world is activated
Your extension works but it is not doing much. Let's modify the source code
a bit. Simply replace the activate function with the following lines:
// src/index.ts#L12-L14
activate: (app: JupyterFrontEnd) => {
console.log('the JupyterLab main application:', app);
}To update the module, simply go to the extension directory and run
jlpm build again. Since you used the --watch option when starting
JupyterLab, you just have to refresh the JupyterLab website in the browser
and should see in the browser console:
the JupyterLab main application:
Object { _started: true, _pluginMap: {…}, _serviceMap: Map(...), _delegate: {…}, commands: {…}, contextMenu: {…}, shell: {…}, registerPluginErrors: [], _dirtyCount: 0, _info: {…}, … }
This is the main application JupyterLab object and you will see how to interact with it in the other examples.
Checkout how the core packages of JupyterLab are defined on this page. Each package is structured similarly to the extension that you are writing. This modular structure makes JupyterLab very adaptable.
An overview of the classes and their attributes and methods can be found in the
JupyterLab documentation. The @jupyterlab/application module documentation is
here
and here is the JupyterFrontEnd class documentation.
JupyterLab is built on top of three major concepts. It is advised to look through the corresponding examples in the following order:
- command: Function to be executed from UI elements. See the commands example
- widget: UI based brick. See the widgets example
- signal: Observer pattern between JupyterLab elements. See the signals example
