JupyterLab Server Components
jupyterlab-server provides a set of REST API handlers and utilities that serve as the backend components for JupyterLab and similar applications. It acts as an intermediary layer between JupyterLab and Jupyter Server, allowing for the creation of custom JupyterLab-like environments. The library maintains an active development pace with frequent updates, often on a monthly or bi-monthly release cycle.
Warnings
- breaking In version 2.28.0, a typo was fixed in the default traitlet for the template directory, changing `template_dir` to `templates_dir`. Configurations explicitly overriding `template_dir` in earlier versions will need to be updated to `templates_dir` to maintain functionality.
- gotcha `jupyterlab-server` is primarily a component library providing backend services for JupyterLab and JupyterLab-like applications. It is not designed as a standalone end-user application. Direct programmatic use often involves extending its `ServerApp` or adding custom handlers within a larger Jupyter ecosystem context.
- gotcha When migrating server extensions from `notebook` (Classic Notebook) to `jupyter_server` (which `jupyterlab-server` builds upon), import paths for server modules may have changed. Code directly importing server components from the `notebook` package will need adjustment.
- deprecated JupyterLab v3.6 and later requires `jupyter_server v2.0` for Real-Time Collaboration features. While `jupyterlab-server` itself might support older `jupyter_server` versions for basic functionality, major new features in the ecosystem often mandate newer server versions.
Install
-
pip install jupyterlab-server -
pip install jupyterlab-server[openapi]
Imports
- LabHandler
from jupyterlab_server.handlers import LabHandler
- ServerApp
from jupyterlab_server.server import ServerApp
- add_handlers
from jupyterlab_server.handlers import add_handlers
Quickstart
import os
from tornado import web
from jupyterlab_server.server import ServerApp
from jupyterlab_server.handlers import LabHandler
# Define a simple custom handler
class MyCustomHandler(LabHandler):
@web.authenticated
def get(self, path=''):
self.finish(f"Hello from My Custom Handler! Path: {path}")
# This function would typically be in a server extension module
# (e.g., `my_extension.py`) and discovered by Jupyter Server.
def load_jupyter_server_extension(server_app: ServerApp):
host_pattern = ".*$"
base_url = server_app.base_url
route_pattern = web.url(f"/{os.environ.get('MY_CUSTOM_ROUTE_PREFIX', 'my-api')}(.*)", MyCustomHandler)
server_app.web_app.add_handlers(host_pattern, [route_pattern])
server_app.log.info(f"MyCustomHandler enabled at {base_url}my-api")
# To run this for testing, you'd integrate it with a Jupyter Server setup.
# This block is illustrative and not a direct executable for end-users
# as jupyterlab-server is a component library.
# For a real setup, this would be loaded as a server extension.
# Example command to launch JupyterLab with this extension:
# jupyter lab --ServerApp.extra_static_paths='["$(pwd)"]' \
# --ServerApp.jpserver_extensions='{"my_extension": true}'
# For testing purposes (not how it's usually run in production):
# if __name__ == "__main__":
# from jupyter_server.serverapp import ServerApp as _JupyterServerApp
# class CustomLabServerApp(_JupyterServerApp):
# def initialize_settings(self):
# super().initialize_settings()
# load_jupyter_server_extension(self)
# CustomLabServerApp.launch_instance()