Skip to main content

A JupyterLab extension that tracks a user's current notebook and cell.

Project description

jupyterlab_notebook_awareness

Github Actions Status

A JupyterLab extension that tracks a user's current notebook and cell.

Usage

A JupyterLab frontend extension that tracks user awareness by adding activeCellId and notebookPath to each client's collaborative awareness state. This enables applications to know which notebook and cell each user is currently viewing.

Awareness State Schema

The extension adds the following fields to each client's awareness state:

  • activeCellId (string | null): The unique ID of the currently active cell the user is viewing
  • notebookPath (string | null): The file path of the notebook the user has open

Example Awareness Structure

Here is an example of the complete awareness structure in JSON with multiple users:

{
  "4233401820": {
    "user": {
      "username": "960fe034b7b847dfbee15eede12caac7",
      "name": "Anonymous Kale",
      "display_name": "Anonymous Kale",
      "initials": "AK",
      "avatar_url": null,
      "color": "var(--jp-collaborator-color5)"
    },
    "activeCellId": "42768507-1132-43fb-86ba-980a4e73e490",
    "notebookPath": "data_analysis.ipynb",
    "cursors": [
      {
        "anchor": {
          "type": {
            "client": 2431406065,
            "clock": 6
          },
          "tname": null,
          "item": null,
          "assoc": 0
        },
        "head": {
          "type": {
            "client": 2431406065,
            "clock": 6
          },
          "tname": null,
          "item": null,
          "assoc": 0
        },
        "primary": true,
        "empty": true
      }
    ]
  },
  "1582940372": {
    "user": {
      "username": "alice_researcher",
      "name": "Alice Smith",
      "display_name": "Alice Smith",
      "initials": "AS",
      "avatar_url": "https://example.com/avatar.jpg",
      "color": "var(--jp-collaborator-color2)"
    },
    "activeCellId": "b8f3e729-4891-4c2a-9876-543210fedcba",
    "notebookPath": "machine_learning.ipynb",
    "cursors": []
  }
}

State Updates

The awareness state is automatically updated when:

  • A user opens a notebook (sets notebookPath)
  • A user clicks on a different cell (updates activeCellId)
  • A user switches between notebooks (updates both fields)
  • A user refreshes the page (restores state when notebook loads)

Accessing Awareness Data

You can access the awareness data programmatically in JupyterLab:

// Get the current notebook's awareness
const notebook = notebookTracker.currentWidget;
const awareness = notebook?.model?.sharedModel?.awareness;

// Get all awareness states
const allStates = awareness?.getStates();

// Get local user's state
const localState = awareness?.getLocalState();

Requirements

  • JupyterLab >= 4.0.0

Install

To install the extension, execute:

pip install jupyterlab_notebook_awareness

Uninstall

To remove the extension, execute:

pip uninstall jupyterlab_notebook_awareness

Contributing

Development install

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 jupyterlab_notebook_awareness directory
# 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 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

pip uninstall jupyterlab_notebook_awareness

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

Testing the extension

Frontend tests

This extension is using Jest for JavaScript code testing.

To execute them, execute:

jlpm
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.

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

jupyterlab_notebook_awareness-0.2.0.tar.gz (130.7 kB view details)

Uploaded Source

Built Distribution

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

jupyterlab_notebook_awareness-0.2.0-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file jupyterlab_notebook_awareness-0.2.0.tar.gz.

File metadata

File hashes

Hashes for jupyterlab_notebook_awareness-0.2.0.tar.gz
Algorithm Hash digest
SHA256 98ef9e61088bec54cd826a793091b74ea557e69b8b7f9cdc8d8387e0ba60c1be
MD5 75f5d6cf4eac037ddff55b56ccaeef85
BLAKE2b-256 a15d8f3d0d9f40441b147f89247136b32c3dc7765996a5bd40d81a379f9d1c84

See more details on using hashes here.

File details

Details for the file jupyterlab_notebook_awareness-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for jupyterlab_notebook_awareness-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c761718b21648bc31ea5b7c38585d0a3ad0f4a5bde9d7feadaaaba301bc276a3
MD5 2126e66eb6b03380ac5f19fd2f223625
BLAKE2b-256 3db9f54616ace9225c7688e2076f9f7c64bddd8e221405e581dd44c798573327

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