JupyterLab Server Components

2.28.0 · active · verified Sun Mar 29

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

Install

Imports

Quickstart

This quickstart illustrates how to define and conceptually register a custom API handler using `jupyterlab-server` components. Typically, `jupyterlab-server` is extended within a larger Jupyter application (like JupyterLab) via a server extension. The `load_jupyter_server_extension` function is the entry point for such extensions, where custom Tornado `web.RequestHandler`s (here, `LabHandler`) are added to the server's web application.

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()

view raw JSON →