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,StaticHandlerServes BokehJS to authenticated Jupyter users.
- class panel.io.jupyter_server_extension.ExtensionStaticHandler(application: Application, request: HTTPServerRequest, **kwargs: Any)[source]#
Bases:
_JupyterAuthMixin,MultiRootStaticHandlerServes extension artifacts, resolving extensions the kernel registered but the server process never imported.
Methods
get_absolute_path(root, path)Returns the absolute location of
pathrelative toroot.- classmethod get_absolute_path(root, path)[source]#
Returns the absolute location of
pathrelative toroot.rootis the path configured for this StaticFileHandler (in most cases thestatic_pathApplication 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
pathleads outside ofroot(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
rootnow raise HTTPError.
- class panel.io.jupyter_server_extension.JupyterComponentResourceHandler(application: Application, request: HTTPServerRequest, **kwargs: Any)[source]#
Bases:
_JupyterAuthMixin,ComponentResourceHandlerServes 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.
- class panel.io.jupyter_server_extension.PanelJupyterHandler(application: Application, request: HTTPServerRequest, **kwargs: Any)[source]#
Bases:
PanelBaseHandlerThe 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
- class panel.io.jupyter_server_extension.PanelLayoutHandler(application: Application, request: HTTPServerRequest, **kwargs: Any)[source]#
Bases:
PanelBaseHandlerMethods
get
- class panel.io.jupyter_server_extension.PanelWSProxy(tornado_app, *args, **kw)[source]#
Bases:
WSHandler,JupyterHandlerThe 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 the current user.
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_userinitialize
- 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_login_url() str[source]#
Override to customize the login URL based on the request.
By default, we use the
login_urlapplication setting.
- 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
- 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.