A JupyterLab extension to facilitate integration with a host page via an iframe
Warning
This project is still in an early development stage.
- JupyterLab >= 4.0.0 or Jupyter Notebook >= 7.0.0
Try out the extension in your browser:
This repository provides two packages:
- The extension package:
jupyter-iframe-commands
. This is the JupyterLab extension that provides the API to execute JupyterLab commands from a host page. - The host package:
jupyter-iframe-commands-host
. This is a package that provides a bridge to communicate with JupyterLab running in an iframe.
The host package provides a bridge to communicate with JupyterLab running in an iframe. To use it in your application:
- Install the package:
npm install jupyter-iframe-commands-host
- Import and use the
CommandBridge
:
import { createBridge } from 'jupyter-iframe-commands-host';
// Initialize the bridge with your iframe ID
const commandBridge = createBridge({ iframeId: 'your-jupyter-iframe-id' });
// Execute JupyterLab commands
// Example: Toggle the left sidebar
await commandBridge.execute('application:toggle-left-area');
// Example: Change the theme
await commandBridge.execute('apputils:change-theme', {
theme: 'JupyterLab Dark'
});
// List available JupyterLab commands
const commands = await commandBridge.listCommands();
console.log(commands);
The JupyterLab extension should be installed in the JupyterLab environment running in the iframe.
To install the extension:
pip install jupyter-iframe-commands
The Jupyter UI can be customized in different ways.
On the following screenshot:
@jupyterlab/mainmenu-extension
is disabled to remove the menu entries@jupyter-notebook/lab-extension
is disabled to remove the interface switcher from the notebook toolbar- The
visible
property of the@jupyter-notebook/application-extension:top
plugin is set tono
, to hide the top bar
For the demo in this repo, this configuration is provided via two files:
overrides.json
: This file is used to override the default settings of the JupyterLab and Jupyter Notebook applicationsjupyter-lite.json
: This file is used to set a list ofdisabledExtensions
, which can be used to disabled invidual plugins
JupyterLab can be configured in a smilar way, using well-known files at specific locations:
page_config.json
: https://jupyterlab.readthedocs.io/en/latest/user/directories.html#labconfig-directoriesoverrides.json
: https://jupyterlab.readthedocs.io/en/latest/user/directories.html#overridesjson
Note
The list of available commands may depend on:
- The JupyterLab version
- Whether your JupyterLab configuration disables some core plugins or extensions
- Third-party extensions available in the JupyterLab environment
Some examples of available commands:
application:toggle-left-area
apputils:activate-command-palette
apputils:display-shortcuts
extensionmanager:show-panel
notebook:create-new
notebook:insert-cell-below
Examples of commands with arguments:
apputils:change-theme
{ 'theme': 'JupyterLab Dark' }
settingeditor:open
{ 'settingEditorType': 'json' }
Tip
For reference JupyterLab defines a list of default commands here: https://jupyterlab.readthedocs.io/en/latest/user/commands.html#commands-list
This package utilizes a bridge mechanism to transmit commands from the host to the extension running in Jupyter. To expand functionality beyond what's currently offered, you can develop a custom extension that defines new commands. If this custom extension is installed within the same Jupyter environment as the jupyter-iframe-commands
extension, those commands will become available.
For further information please consult the Jupyter documentation:
- Creating an extension: https://jupyterlab.readthedocs.io/en/stable/extension/extension_dev.html
- Adding commands to the command registry: https://jupyterlab.readthedocs.io/en/stable/extension/extension_points.html#commands
To run the demo on a local Jupyter Lab instance:
- Follow the development install instructions
cd demo
- Start JupyterLab:
jlpm start:lab
- In another terminal, start the demo app:
jlpm start:local
Open http://localhost:8080 in your browser.
To run the demo on a Jupyter Lite instance:
- Follow the development install instructions
cd demo
- Build and start the demo app:
# Build the lite assets
jlpm build:lite
# Build the demo
jlpm build:ghpages
# Start the development server
jlpm start:lite
To remove the extension, execute:
pip uninstall jupyter-iframe-commands
Note
You will need NodeJS to build the extension package.
The jlpm
command is JupyterLab's pinned version of
yarn that is installed with JupyterLab. You may use
yarn
or npm
in lieu of jlpm
below.
# Clone the repo to your local environment
# Change directory to the jupyter-iframe-commands directory
# Install package in development mode
pip install -e "."
# Install dependencies
jlpm install
# Build extension Typescript source after making changes
jlpm build
# Link your development version of the extension with JupyterLab
jupyter labextension develop . --overwrite
You can watch the source directory and run JupyterLab at the same time in different terminals to watch for changes in the extension's source and automatically rebuild the extension.
# Watch the source directory in one terminal, automatically rebuilding when needed
jlpm watch
# Run JupyterLab in another terminal
jupyter lab
With the watch command running, every saved change will immediately be built locally and available in your running JupyterLab. Refresh JupyterLab to load the change in your browser (you may need to wait several seconds for the extension to be rebuilt).
By default, the jlpm build
command generates the source maps for this extension to make it easier to debug using the browser dev tools. To also generate source maps for the JupyterLab core extensions, you can run the following command:
jupyter lab build --minimize=False
pip uninstall jupyter-iframe-commands
In development mode, you will also need to remove the symlink created by jupyter labextension develop
command. To find its location, you can run jupyter labextension list
to figure out where the labextensions
folder is located. Then you can remove the symlink named jupyter-iframe-commands
within that folder.
This extension uses Playwright for the integration tests (aka user level tests). More precisely, the JupyterLab helper Galata is used to handle testing the extension in JupyterLab.
More information are provided within the ui-tests README.
See RELEASE