panel.io.jupyter_server_extension module#

The Panel jupyter_server_extension implements Jupyter RequestHandlers that allow a Panel application to run inside Jupyter kernels. This allows Panel applications to be served in the kernel (and therefore the environment) they were written for.

The PanelJupyterHandler may be given the path to a notebook, .py file or Lumen .yaml file. It will then attempt to resolve the appropriate kernel based on the kernelspec or a query parameter. Once the kernel has been provisioned the handler will execute a code snippet on the kernel which creates a PanelExecutor. The PanelExecutor creates a bokeh.server.session.ServerSession and connects it to a Jupyter Comm. The PanelJupyterHandler will then serve the result of executor.render().

Once the frontend has rendered the HTML it will send a request to open a WebSocket will be sent to the PanelWSProxy. This in turns forwards any messages it receives via the kernel.shell_channel and the Comm to the PanelExecutor. This way we proxy any WS messages being sent to and from the server to the kernel.

class panel.io.jupyter_server_extension.BokehStaticHandler(tornado_app: Any, *args: Any, **kw: Any)[source]#

Bases: _JupyterAuthMixin, StaticHandler

Serves BokehJS to authenticated Jupyter users.

class panel.io.jupyter_server_extension.ExtensionStaticHandler(application: Application, request: HTTPServerRequest, **kwargs: Any)[source]#

Bases: _JupyterAuthMixin, MultiRootStaticHandler

Serves extension artifacts, resolving extensions the kernel registered but the server process never imported.

Methods

get_absolute_path(root, path)

Returns the absolute location of path relative to root.

classmethod get_absolute_path(root, path)[source]#

Returns the absolute location of path relative to root.

root is the path configured for this StaticFileHandler (in most cases the static_path Application setting).

This class method may be overridden in subclasses. By default it returns a filesystem path, but other strings may be used as long as they are unique and understood by the subclass’s overridden get_content.

Raises HTTPError (403) if path leads outside of root (as written, e.g. with ../; symlinks are checked later by validate_absolute_path). This check is also what keeps get_version (and therefore ~RequestHandler.static_url) from reading files outside of the static directory.

Added in version 3.1.

Changed in version 6.6: Paths outside of root now raise HTTPError.

class panel.io.jupyter_server_extension.JupyterComponentResourceHandler(application: Application, request: HTTPServerRequest, **kwargs: Any)[source]#

Bases: _JupyterAuthMixin, ComponentResourceHandler

Serves component resources to authenticated Jupyter users.

class panel.io.jupyter_server_extension.PanelBaseHandler(application: Application, request: HTTPServerRequest, **kwargs: Any)[source]#

Bases: JupyterHandler

Attributes:
root_dir

Methods

nb_path([path, require_file])

Resolves a request path within the server root.

nb_path(path: str | None = None, require_file: bool = False) → Path[source]#

Resolves a request path within the server root.

Like jupyter_server’s contents manager the check is lexical, so symlinks inside the root may point elsewhere and the kernel runs in the symlinked directory.

class panel.io.jupyter_server_extension.PanelJupyterHandler(application: Application, request: HTTPServerRequest, **kwargs: Any)[source]#

Bases: PanelBaseHandler

The PanelJupyterHandler expects to be given a path to a notebook, .py file or Lumen .yaml file. Based on the kernelspec in the notebook or the kernel query parameter it will then provision a Jupyter kernel to run the Panel application in.

Once the kernel is launched it will instantiate a PanelExecutor inside the kernel and serve the HTML returned by it. If successful it will store the kernel and comm_id on panel.state.

Methods

get

initialize

initialize(**kwargs)[source]#
class panel.io.jupyter_server_extension.PanelLayoutHandler(application: Application, request: HTTPServerRequest, **kwargs: Any)[source]#

Bases: PanelBaseHandler

Methods

get

class panel.io.jupyter_server_extension.PanelWSProxy(tornado_app, *args, **kw)[source]#

Bases: WSHandler, JupyterHandler

The PanelWSProxy serves as a proxy between the frontend and the Jupyter kernel that is running the Panel application. It send and receives Bokeh protocol messages via a Jupyter Comm.

Methods

check_origin([origin])

Check Origin == Host or Access-Control-Allow-Origin.

get_current_user()

Get the current user.

get_login_url()

Override to customize the login URL based on the request.

on_close()

Clean up when the connection is closed.

on_message(fragment)

Process an individual wire protocol fragment.

open(path, *args, **kwargs)

Initialize a connection to a client.

prepare()

Async counterpart to get_current_user

initialize

check_origin(origin: str | None = None) → bool[source]#

Check Origin == Host or Access-Control-Allow-Origin.

Tornado >= 4 calls this method automatically, raising 403 if it returns False.

get_current_user() → str[source]#

Get the current user.

get_login_url() → str[source]#

Override to customize the login URL based on the request.

By default, we use the login_url application setting.

initialize(*args, **kwargs)[source]#
on_close() → None[source]#

Clean up when the connection is closed.

async on_message(fragment: str | bytes) → None[source]#

Process an individual wire protocol fragment.

The websocket RFC specifies opcodes for distinguishing text frames from binary frames. Tornado passes us either a text or binary string depending on that opcode, we have to look at the type of the fragment to see what we got.

Args:

message (unicode or bytes) : wire message to process

open(path, *args, **kwargs) → None[source]#

Initialize a connection to a client.

Returns:

None

async prepare()[source]#

Async counterpart to get_current_user

async panel.io.jupyter_server_extension.ensure_async(obj: Awaitable | t.Any) → t.Any[source]#

Convert a non-awaitable object to a coroutine if needed, and await it if it was not already awaited.

panel.io.jupyter_server_extension.find_extension_dist(name: str) → Path | None[source]#

Locates the dist directory of an extension without importing it.

Extensions register themselves in Bokeh’s extension_dirs when they are imported, which happens in the kernel and not in the Jupyter server process. They conventionally register the dist directory of a package whose import name is the extension name with dashes replaced by underscores, e.g. panel-material-ui -> panel_material_ui.

panel.io.jupyter_server_extension.generate_executor(path: str, token: str, root_url: str) → str[source]#

Generates the code to instantiate a PanelExecutor that is to be be run on the kernel to start a server session.

Parameters:
path: str

The path of the Panel application to execute.

token: str

The Bokeh JWT token containing the session_id, request arguments, headers and cookies.

root_url: str

The root_url the server is running on.

Returns:
The code to be executed inside the kernel.
panel.io.jupyter_server_extension.url_path_join(*pieces)[source]#

Join components of url into a relative url Use to prevent double slash when joining subpath. This will leave the initial and final / in place