Refer to jupyterlab extension example: hello world, but with a different project name: jet_helloworld
Set up the development environment and print to the console.
- Only for reference.
PS D:\codes\Othersjupyterlab_extension_tutorial> jupyter --version
Selected Jupyter core packages...
IPython : 8.4.0
ipykernel : 6.15.1
ipywidgets : 8.1.1
jupyter_client : 8.6.0
jupyter_core : 5.7.1
jupyter_server : 2.12.5
jupyterlab : 4.0.10
nbclient : 0.9.0
nbconvert : 7.14.2
nbformat : 5.9.2
notebook : 7.0.6
qtconsole : 5.5.1
traitlets : 5.13.0
PS D:\codes\Others\jupyterlab_extension_tutorial> python --version
Python 3.10.5
PS D:\codes\Others\jupyterlab_extension_tutorial> node --version
v18.19.0
PS D:\codes\Others\jupyterlab_extension_tutorial> npm --version
10.2.3
PS D:\codes\Others\jupyterlab_extension_tutorial> yarn --version
1.22.19
PS D:\codes\Others\jupyterlab_extension_tutorial> hatch --version
Hatch, version 1.9.3
PS D:\codes\Others\jupyterlab_extension_tutorial> jlpm --version
3.5.0
PS D:\codes\Others\jupyterlab_extension_tutorial> pip --version
pip 23.3.2 from D:\Program Files (x86)\Python\Python310\lib\site-packages\pip (python 3.10)
(The old version can be found in the last part of the README.md
.)
We directly clone from this repo and the structure is like:
(Not containing .github/*
, .binder/*
, src/__tests__/*
)
.
├── babel.config.js
├── CHANGELOG.md
├── .copier-answers.yml
├── .gitignore
├── jet_helloworld
│ └── __init__.py
├── install.json
├── jest.config.js
├── LICENSE
├── package.json
├── .prettierignore
├── pyproject.toml
├── README.md
├── RELEASE.md
├── setup.py
├── src
│ ├── index.ts
├── style
│ ├── base.css
│ ├── index.css
│ └── index.js
├── tsconfig.json
├── tsconfig.test.json
├── ui-tests
│ ├── jupyter_server_test_config.py
│ ├── package.json
│ ├── playwright.config.js
│ ├── README.md
│ ├── tests
│ │ └── jet_helloworld.spec.ts
│ └── yarn.lock
└── .yarnrc.yml
Those files can be separated in 5 groups:
- Extension code (those files are mandatory):
src/index.ts
this contains the actual code of your extensionstyle/
folder contains style elements used by your extension
- Information about the extension:
CHANGELOG.md
will be populated automatically by changes when using the suggested release tool..copier-answers.yml
contains the answers given when generating the directory from the extension template.LICENSE
contains your extension code license; BSD-3 Clause by default (but you can change it).package.json
contains the JavaScript package metadata and configuration.README.md
contains some instructions to install and use the extension.RELEASE.md
contains instructions to release the extension.
- Configuration:
.gitignore
files to be ignored by Git (the recommended version control tool)..prettierignore
files to be ignored by JavaScript code formatterprettier
.package.json
contains the JavaScript configuration for the linters:eslint
,prettier
andstylelint
.tsconfig.json
contains the typescript compilation configuration..yarnrc.yml
contains the configuration of the JavaScript package managerjlpm
.
- Tests:
jest.config.js
,babel.config.js
andtsconfig.test.json
configure the JavaScript test Frameworkjest
.ui-tests/
sets up integration tests usingplaywright
.
- Packaging as a Python package:
pyproject.toml
contains the configuration to create the Python package (usinghatch
) and to release it (usingjupyter-releaser
).install.json
contains information retrieved by JupyterLab to help users know how to manage the packagejet_helloworld/
folder contains the final code to be distributedsetup.py
is present for backward compatibility with tools not supportingpyproject.toml
.
The following sections will walk you through the extension code files.
- Open VsCode
- Replace all
jupyterlab_examples_hello_world
withjet_helloworld
(that is, your python package name) - Replace all
@jupyterlab-examples/hello-world
with@yunfzhou/jet_helloworld
(that is, your javascript package name) - Replace all
https://github.com/jupyterlab/extension-examples
withhttps://github.com/yunfzhou/jupyterlab_extension_tutorial
(that is, your repo website)
- Replace all
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#L54-L56
"dependencies": {
"@jupyterlab/application": "^4.0.0"
},
With this basic import setup, you can move on to construct a new instance
of the JupyterFrontEndPlugin
class:
// src/index.ts#L9-L13
const plugin: JupyterFrontEndPlugin<void> = {
id: '@yunfzhou/jet_helloworld:plugin',
description: 'Minimal JupyterLab extension.',
autoStart: true,
activate: (app: JupyterFrontEnd) => {
console.log('The JupyterLab main application:', app);
// src/index.ts#L15-L18
}
};
export default plugin;
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 argumentapp
of typeJupyterFrontEnd
and will be called by the main application to activate the extension.
app
is 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 plugin
.
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:
You will need NodeJS to build the extension package.
# Install package in development mode
pip install -e .
# Link your development version of the extension with JupyterLab
jupyter labextension develop . --overwrite
# Rebuild extension Typescript source after making changes
jlpm run build
The
jlpm
command is JupyterLab's pinned version of yarn that is installed with JupyterLab. You may useyarn
ornpm
in lieu ofjlpm
below.
-
The first command:
-
Installs the dependencies that are specified in the
setup.py
file and inpackage.json
. Among the dependencies are also all of theJupyterLab
components that you want to use in your project. -
It then runs the build script. In that step, the TypeScript code gets converted to javascript using the compiler
tsc
and stored in alib
directory. And a condensed form of the Javascript is copied in the Python package (in the folderjet_helloworld/labextension
). This is the code that would be installed by the user in JupyterLab. -
And it will also generate
.yarn/*
,node_modules
,tsconfig.tsbuildinfo
,yarn.lock
andjet_helloworld/_version.py
-
-
The second command create a symbolic link to the folder
hello_world/labextension
so that extension is installed in development mode in JupyterLab. -
The third command allows you to update the Javascript code each time you modify your extension code.
After all of these steps are done, running jupyter labextension list
should
show something like:
JupyterLab v4.0.10
D:\Program Files (x86)\Python\Python310\share\jupyter\labextensions
......
@yunfzhou/jet_helloworld v0.1.0 enabled ok
Other labextensions (built into JupyterLab)
......
Your 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:
The JupyterLab main application:
Object { _started: true, _pluginMap: {…}, _serviceMap: Map(...), _delegate: {…}, commands: {…}, contextMenu: {…}, shell: {…}, registerPluginErrors: [], _dirtyCount: 0, _info: {…}, … }
Let's modify the source code a bit. You can stop the jupyterlab and open 2 terminals.
# terminal 1. If you wish to avoid running `jlpm run build` after each change, executes `jlpm run watch` command from your extension directory. That command will automatically compile the TypeScript files as they are changed and saved.
jlpm run watch
# terminal 2
jupyter lab
Simply replace the activate
function with the following lines:
// src/index.ts#L13-L15
activate: (app: JupyterFrontEnd) => {
console.log('@yunfzhou: The JupyterLab main application:', app);
}
You have to refresh the JupyterLab website in the browser and should see in the browser console:
@yunfzhou: The JupyterLab main application:
Object { _started: true, _pluginMap: {…}, _serviceMap: Map(...), _delegate: {…}, commands: {…}, contextMenu: {…}, shell: {…}, registerPluginErrors: [], _dirtyCount: 0, _info: {…}, … }
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.
# labextension: disable. No need to uninstall or unlink.
jupyter labextension disable @yunfzhou/jet_helloworld
# Directly uninstall via pip
pip uninstall jet_helloworld
# Clean `lib/*`, `jet_helloworld/labextension/*`, `jet_helloworld/_version.py` and `tsconfig.tsbuildinfo`
jlpm run clean:all
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
Writing a JupyterLab extension usually starts from a configurable template. It
can be downloaded with the copier
tool and the following command:
pip install "copier~=7.2" jinja2-time "pydantic<2.0.0"
mkdir my_extension
cd my_extension
copier copy https://github.com/jupyterlab/extension-template .
You will be asked for some basic information that could for example be setup like this:
🎤 What is your extension kind?
frontend
🎤 Extension author name
tuto
🎤 Extension author email
[email protected]
🎤 JavaScript package name
hello-world
🎤 Python package name
hello_world
🎤 Extension short description
Minimal JupyterLab example.
🎤 Does the extension have user settings?
No
🎤 Do you want to set up Binder example?
Yes
🎤 Do you want to set up tests for the extension?
Yes
🎤 Git remote repository URL
https://github.com/github_username/hello-world
The python name must be a valid Python module name (characters such
-
,@
or/
are not allowed). It is nice for user to test your extension online, so the set up Binder was set to Yes.
The template creates creates files in the current director that looks like this:
.
├── babel.config.js
├── binder
│ ├── environment.yml
│ └── postBuild
├── CHANGELOG.md
├── .copier-answers.yml
├── .github
│ └── workflows
│ ├── build.yml
│ ├── check-release.yml
│ ├── enforce-label.yml
│ ├── prep-release.yml
│ ├── publish-release.yml
│ └── update-integration-tests.yml
├── .gitignore
├── hello_world
│ └── __init__.py
├── install.json
├── jest.config.js
├── LICENSE
├── package.json
├── .prettierignore
├── pyproject.toml
├── README.md
├── RELEASE.md
├── setup.py
├── src
│ ├── index.ts
│ └── __tests__
│ └── hello_world.spec.ts
├── style
│ ├── base.css
│ ├── index.css
│ └── index.js
├── tsconfig.json
├── tsconfig.test.json
├── ui-tests
│ ├── jupyter_server_test_config.py
│ ├── package.json
│ ├── playwright.config.js
│ ├── README.md
│ ├── tests
│ │ └── hello_world.spec.ts
│ └── yarn.lock
└── .yarnrc.yml
Those files can be separated in 5 groups:
- Extension code (those files are mandatory):
src/index.ts
this contains the actual code of your extensionstyle/
folder contains style elements used by your extension
- Information about the extension:
CHANGELOG.md
will be populated automatically by changes when using the suggested release tool..copier-answers.yml
contains the answers given when generating the directory from the extension template.LICENSE
contains your extension code license; BSD-3 Clause by default (but you can change it).package.json
contains the JavaScript package metadata and configuration.README.md
contains some instructions to install and use the extension.RELEASE.md
contains instructions to release the extension.
- Configuration:
binder/
contains configuration to test online your extension using Binder..gitignore
files to be ignored by Git (the recommended version control tool)..prettierignore
files to be ignored by JavaScript code formatterprettier
.package.json
contains the JavaScript configuration for the linters:eslint
,prettier
andstylelint
.tsconfig.json
contains the typescript compilation configuration..yarnrc.yml
contains the configuration of the JavaScript package managerjlpm
.
- Tests:
.github/workflows/build.yml
sets the continuous integration tests of the code using GitHub Actions.github/workflows/update-integration-tests.yml
sets up a GitHub action to update integration test snapshots..github/workflows/<others>.yml
set GitHub actions to check and handle release of the extension.jest.config.js
,babel.config.js
andtsconfig.test.json
configure the JavaScript test Frameworkjest
.ui-tests/
sets up integration tests usingplaywright
.
- Packaging as a Python package:
pyproject.toml
contains the configuration to create the Python package (usinghatch
) and to release it (usingjupyter-releaser
).install.json
contains information retrieved by JupyterLab to help users know how to manage the packagehello_world/
folder contains the final code to be distributedsetup.py
is present for backward compatibility with tools not supportingpyproject.toml
.
The following sections will walk you through the extension code files.