JupyterLabSession#

class rubin.nublado.client.JupyterLabSession(*, username, base_url, kernel_name='LSST', notebook_name=None, max_websocket_size, http_client, xsrf, logger)#

Bases: object

Represents an open session with a Jupyter Lab.

A context manager providing an open WebSocket session. The session will be automatically deleted when exiting the context manager. Objects of this type should be created by calling NubladoClient.open_lab_session.

Parameters:
  • username (str) – User the session is for.

  • base_url (str) – Base URL for talking to JupyterLab.

  • kernel_name (str, default: 'LSST') – Name of the kernel to use for the session.

  • notebook_name (str | None, default: None) – Name of the notebook we will be running, which is passed to the session and might influence logging on the lab side. If set, the session type will be set to notebook. If not set, the session type will be set to console.

  • http_client (AsyncClient) – HTTP client to talk to the Jupyter lab.

  • logger (BoundLogger) – Logger to use.

  • max_websocket_size (int | None)

  • xsrf (str | None)

Methods Summary

run_notebook(notebook[, context])

Run a notebook of Python code in a Jupyter lab kernel.

run_notebook_via_rsp_extension(*[, path, ...])

Run a notebook through the RSP noninteractive extension, which will return the the executed notebook.

run_python(code[, context])

Run a block of Python code in a Jupyter lab kernel.

run_python_cell(source[, context])

Run a cell of Python code in a Jupyter lab kernel.

Methods Documentation

async run_notebook(notebook, context=None)#

Run a notebook of Python code in a Jupyter lab kernel.

Parameters:
  • notebook (Path) – Path of notebook (relative to $HOME) to run.

  • context (CodeContext | None, default: None) – Optional set of overrides for exception reporting.

Returns:

Output from the kernel, one string per cell.

Return type:

list[str]

Raises:
  • CodeExecutionError – Raised if an error was reported by the Jupyter lab kernel.

  • JupyterWebSocketError – Raised if there was a WebSocket protocol error while running code or waiting for the response.

  • JupyterWebError – Raised if called before entering the context and thus before creating the WebSocket session.

async run_notebook_via_rsp_extension(*, path=None, content=None)#

Run a notebook through the RSP noninteractive extension, which will return the the executed notebook.

This is our way around having to deal individually with the execute_result and data_display message types, which can represent a wild variety of use cases: those things will be found in the output notebook and it is the caller’s responsibility to deal with them.

Parameters:
  • path (Path | None, default: None) – Path of notebook (relative to $HOME) to run.

  • content (str | None, default: None) – Notebook content as a string. Only used if path is None.

Returns:

The executed Jupyter Notebook.

Return type:

NotebookExecutionResult

Raises:
  • ExecutionAPIError – Raised if there is an error interacting with the JupyterLab Notebook execution extension.

  • JupyterWebError – Raised if neither notebook path nor notebook contents are supplied.

async run_python(code, context=None)#

Run a block of Python code in a Jupyter lab kernel.

Parameters:
Returns:

Output from the kernel.

Return type:

str

Raises:
  • CodeExecutionError – Raised if an error was reported by the Jupyter lab kernel.

  • JupyterWebSocketError – Raised if there was a WebSocket protocol error while running code or waiting for the response.

  • JupyterWebError – Raised if called before entering the context and thus before creating the WebSocket session.

async run_python_cell(source, context=None)#

Run a cell of Python code in a Jupyter lab kernel.

Parameters:
  • source (list[str]) – code to run

  • context (CodeContext | None, default: None) – context of source code to run

Returns:

Output from the kernel, one string per cell.

Return type:

str

Raises:
  • CodeExecutionError – Raised if an error was reported by the Jupyter lab kernel.

  • JupyterWebSocketError – Raised if there was a WebSocket protocol error while running code or waiting for the response.

  • JupyterWebError – Raised if called before entering the context and thus before creating the WebSocket session.