Skip to main content

A JupyterLab/JupyterLite extension for Jupyter UI metrics.

Project description

notebook-metrics

A JupyterLab/JupyterLite extension for Jupyter UI metrics.

If you are integrating the package for the first time, start with the onboarding guide.

Usage

This package contains four Jupyter extensions.

Jupyter server extension

The Jupyter server extension, notebook_metrics registers event schemas for four types of metrics events:

  1. CommandExecuted events (anonymous: false, sensitivity: "high"), which are emitted every time the command registry executes a command
  2. CurrentChanged events (anonymous: false, sensitivity: "high"), which are emitted every time a non-null new value is emitted by the application shell's currentChanged signal
  3. JupyterError events (anonymous: false, sensitivity: "high"), which are emitted every time notebook cell execution fails with a Jupyter error
  4. RuntimeError events (anonymous: false, sensitivity: "high"), which are emitted every time an error or unhandledpromiserejection listener on the application window is invoked

JupyterLab/JupyterLite dispatcher extension

The front-end dispatcher extension listens for registered events and dispatches them based on filter and user settings to a collector extension. It also provides a single API point of entry, a function called register(...), which registers an event schema URL and an optional broadcast source that emits metrics events for dispatch. In cases where no broadcast is necessary (e.g., where a UI component automatically emits metrics events), the source argument can be omitted.

JupyterLab/JupyterLite collector extension

The collector extension is the client that receives metrics emissions. The default implementation is a no-op and in production, the extension notebook-metrics:collector needs to be disabled and replaced with a custom extension that provides an IMetrics.ICollector for the dispatcher extension to use. The interface for a collector is minimal:

interface ICollector {
  collect: (schema: string, event: IMetrics.Event) => Promise<void>;
}

An extension that replaces the default no-op collector would have this shape:

const collector: JupyterFrontEndPlugin<IMetrics.ICollector> = {
  id: 'my-collector-extension',
  description: 'A collector for metrics emissions',
  provides: IMetrics.ICollector,
  activate: (): IMetrics.ICollector => ({
    collect: async (schema: string, event: IMetrics.Event) => {
      // Save emission here...
    }
  })
};

JupyterLab/JupyterLite broadcasts extension

This frontend extension registers the four metrics event types that ship with this package.

Configuration

This plugin can be configured in two ways to allow a user to disable or limit emissions: the JupyterLab user settings system or using the JupyterLab PageConfig object.

There are several ways to populate PageConfig, (which is an object literal loaded in the HTML page that hosts JupyterLab). As a convenience for JupyterLab deployment, this package supports setting a path to a YAML file as an environment variable, NOTEBOOK_METRICS_OVERRIDE.

The contents of an override file are the same keys that exist in the user settings (dispatcher.json) and if set, they always take precedence over user settings.

Here is an example override file for the most permissive emission settings:

anonymous: false
disabled: false
excluded:
  'https://schema.notebook.link/metrics/command-executed/v1': false
  'https://schema.notebook.link/metrics/current-changed/v1': false
  'https://schema.notebook.link/metrics/jupyter-error/v1': false
  'https://schema.notebook.link/metrics/runtime-error/v1': false
sensitivity: high

Requirements

  • Python >= 3.9
  • JupyterLab >= 4.0.0,<5

Install

python -m pip install notebook-metrics

Uninstall

To remove the extension, execute:

python -m pip uninstall notebook-metrics

Contributing

Development install

Note: You will need Node.js 24.x to build the extension package and run the frontend tooling.

This repo uses jlpm, JupyterLab's pinned Yarn wrapper.

# Clone the repo to your local environment
# Change directory to the metrics directory
# Install package in development mode
python -m pip install -e ".[test]"
# Link your development version of the extension with JupyterLab
jupyter labextension develop . --overwrite
# Rebuild extension Typescript source after making changes
jlpm build

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

Development uninstall

python -m pip uninstall notebook-metrics

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 notebook-metrics within that folder.

Testing the extension

Python tests

Install the editable package with test extras, then run:

python -m pip install -e ".[test]"
pytest -vv -r ap --cov notebook_metrics

Frontend tests

This extension is using Jest for JavaScript code testing.

To execute them, execute:

jlpm install
jlpm test

Integration tests

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.

The local flow that is currently validated in this repo is:

jlpm build:prod
cd ui-tests
jlpm install
jlpm playwright install
PLAYWRIGHT_HTML_OPEN=never jlpm playwright test
cd ..

Packaging the extension

See RELEASE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

notebook_metrics-1.0.0a1.tar.gz (228.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

notebook_metrics-1.0.0a1-py3-none-any.whl (39.7 kB view details)

Uploaded Python 3

File details

Details for the file notebook_metrics-1.0.0a1.tar.gz.

File metadata

  • Download URL: notebook_metrics-1.0.0a1.tar.gz
  • Upload date:
  • Size: 228.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for notebook_metrics-1.0.0a1.tar.gz
Algorithm Hash digest
SHA256 c779b1998e4320f72251168cc52b30cb5cd85552482d68568e078e002300659d
MD5 39cd49176586400f9bbbe242449aa53c
BLAKE2b-256 cda0f5df622bc5b5a784a8252bbfb28632b0317fa2438b259c80878df823de09

See more details on using hashes here.

File details

Details for the file notebook_metrics-1.0.0a1-py3-none-any.whl.

File metadata

File hashes

Hashes for notebook_metrics-1.0.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 755c54aaca475f7111c6b1006c5890059229366b5c9c61e31baa91b26e737bb7
MD5 8c21912e52bed525b27604457150a022
BLAKE2b-256 266c4aae45c19a0cd1eceea22c3469dc8c7a9396f8bd71d7597c22cfe832906d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page