nbclient package¶
Subpackages¶
- nbclient.tests package
- Submodules
- nbclient.tests.base module
- nbclient.tests.fake_kernelmanager module
- nbclient.tests.test_client module
AsyncMock
TestExecute
TestExecute.maxDiff
TestExecute.test_allow_errors()
TestExecute.test_async_execution_hook()
TestExecute.test_cleanup_kernel_client()
TestExecute.test_constructor()
TestExecute.test_custom_kernel_manager()
TestExecute.test_disable_stdin()
TestExecute.test_empty_kernel_name()
TestExecute.test_empty_path()
TestExecute.test_error_async_execution_hook()
TestExecute.test_error_execution_hook_error()
TestExecute.test_error_notebook_hook()
TestExecute.test_execute_function()
TestExecute.test_execution_hook()
TestExecute.test_force_raise_errors()
TestExecute.test_kernel_death_after_timeout()
TestExecute.test_kernel_death_during_execution()
TestExecute.test_populate_language_info()
TestExecute.test_process_message_wrapper()
TestExecute.test_reset_kernel_client()
TestExecute.test_sync_kernel_manager()
TestExecute.test_timeout()
TestExecute.test_timeout_func()
TestExecute.test_widgets()
TestRunCell
TestRunCell.test_allow_error_names()
TestRunCell.test_allow_errors()
TestRunCell.test_async_cell_hooks()
TestRunCell.test_busy_message()
TestRunCell.test_cell_hooks()
TestRunCell.test_clear_output_message()
TestRunCell.test_clear_output_wait_message()
TestRunCell.test_clear_output_wait_then_message_message()
TestRunCell.test_clear_output_wait_then_update_display_message()
TestRunCell.test_coalesce_streams()
TestRunCell.test_deadline_exec_reply()
TestRunCell.test_deadline_iopub()
TestRunCell.test_display_data_message()
TestRunCell.test_display_data_same_id_message()
TestRunCell.test_display_data_without_id_message()
TestRunCell.test_error_and_error_status_messages()
TestRunCell.test_error_async_cell_hooks()
TestRunCell.test_error_cell_hooks()
TestRunCell.test_error_message()
TestRunCell.test_error_message_only()
TestRunCell.test_eventual_deadline_iopub()
TestRunCell.test_execute_input_message()
TestRunCell.test_execute_result_message()
TestRunCell.test_execute_result_with_display_message()
TestRunCell.test_execution_count_message()
TestRunCell.test_execution_count_message_ignored_on_override()
TestRunCell.test_execution_count_with_stream_message()
TestRunCell.test_idle_message()
TestRunCell.test_message_for_wrong_parent()
TestRunCell.test_no_source()
TestRunCell.test_non_code_cell()
TestRunCell.test_non_code_cell_hooks()
TestRunCell.test_raises_exception_tag()
TestRunCell.test_stream_messages()
TestRunCell.test_unknown_comm_message()
TestRunCell.test_update_display_data_message()
TestRunCell.test_update_display_data_mismatch_id_message()
TestRunCell.test_update_display_data_without_id_message()
TestRunCell.test_widget_comm_buffer_message_single()
TestRunCell.test_widget_comm_buffer_messages()
TestRunCell.test_widget_comm_message()
assert_notebooks_equal()
async_run_notebook()
filter_messages_on_error_output()
get_executor_with_hooks()
make_future()
normalize_base64()
normalize_output()
notebook_resources()
prepare_cell_mocks()
run_notebook()
run_notebook_wrapper()
test_async_parallel_notebooks()
test_execution_timing()
test_many_async_parallel_notebooks()
test_many_parallel_notebooks()
test_parallel_notebooks()
test_run_all_notebooks()
test_start_new_kernel_client_cleans_up_kernel_on_failure()
test_start_new_kernel_history_file_setting()
test_startnewkernel_with_kernelmanager()
test_synchronous_setup_kernel()
- Module contents
Submodules¶
nbclient.client module¶
nbclient implementation.
- class nbclient.client.NotebookClient(**kwargs: Any)¶
Bases:
LoggingConfigurable
Encompasses a Client for executing cells in a notebook
- allow_error_names: List[str]¶
An instance of a Python list.
- allow_errors: bool¶
A boolean (True, False) trait.
- async async_execute(reset_kc: bool = False, **kwargs: Any) NotebookNode ¶
Executes each code cell.
- Parameters:
kwargs –
Any option for
self.kernel_manager_class.start_kernel()
. Because that defaults to AsyncKernelManager, this will likely include options accepted byjupyter_client.AsyncKernelManager.start_kernel()
, which includescwd
.reset_kc
if True, the kernel client will be reset and a new one will be created (default: False).- Returns:
nb – The executed notebook.
- Return type:
NotebookNode
- async async_execute_cell(cell: NotebookNode, cell_index: int, execution_count: int | None = None, store_history: bool = True) NotebookNode ¶
Executes a single code cell.
To execute all cells see
execute()
.- Parameters:
cell (nbformat.NotebookNode) – The cell which is currently being processed.
cell_index (int) – The position of the cell within the notebook object.
execution_count (int) – The execution count to be assigned to the cell (default: Use kernel response)
store_history (bool) – Determines if history should be stored in the kernel (default: False). Specific to ipython kernels, which can store command histories.
- Returns:
output – The execution output payload (or None for no output).
- Return type:
dict
- Raises:
CellExecutionError – If execution failed and should raise an exception, this will be raised with defaults about the failure.
- Returns:
cell – The cell which was just processed.
- Return type:
NotebookNode
- async_setup_kernel(**kwargs: Any) AsyncGenerator ¶
Context manager for setting up the kernel to execute a notebook.
This assigns the Kernel Manager (
self.km
) if missing and Kernel Client(self.kc
).When control returns from the yield it stops the client’s zmq channels, and shuts down the kernel.
Handlers for SIGINT and SIGTERM are also added to cleanup in case of unexpected shutdown.
- async async_start_new_kernel(**kwargs: Any) None ¶
Creates a new kernel.
- Parameters:
kwargs – Any options for
self.kernel_manager_class.start_kernel()
. Because that defaults to AsyncKernelManager, this will likely include options accepted byAsyncKernelManager.start_kernel()
, which includescwd
.
- async async_start_new_kernel_client() KernelClient ¶
Creates a new kernel client.
- Returns:
kc – Kernel client as created by the kernel manager
km
.- Return type:
KernelClient
- async async_wait_for_reply(msg_id: str, cell: NotebookNode | None = None) Dict | None ¶
Wait for a message reply.
- clear_display_id_mapping(cell_index: int) None ¶
Clear a display id mapping for a cell.
- clear_output(outs: List, msg: Dict, cell_index: int) None ¶
Clear output.
- coalesce_streams¶
A boolean (True, False) trait.
- create_kernel_manager() KernelManager ¶
Creates a new kernel manager.
- Returns:
km – Kernel manager whose client class is asynchronous.
- Return type:
KernelManager
- display_data_priority: List¶
An instance of a Python list.
- error_on_timeout: Dict | None¶
An instance of a Python dict.
One or more traits can be passed to the constructor to validate the keys and/or values of the dict. If you need more detailed validation, you may use a custom validator method.
Changed in version 5.0: Added key_trait for validating dict keys.
Changed in version 5.0: Deprecated ambiguous
trait
,traits
args in favor ofvalue_trait
,per_key_traits
.
- execute(**kwargs: Any) Any ¶
Executes each code cell.
- Parameters:
kwargs –
Any option for
self.kernel_manager_class.start_kernel()
. Because that defaults to AsyncKernelManager, this will likely include options accepted byjupyter_client.AsyncKernelManager.start_kernel()
, which includescwd
.reset_kc
if True, the kernel client will be reset and a new one will be created (default: False).- Returns:
nb – The executed notebook.
- Return type:
NotebookNode
- execute_cell(**kwargs: Any) Any ¶
Executes a single code cell.
To execute all cells see
execute()
.- Parameters:
cell (nbformat.NotebookNode) – The cell which is currently being processed.
cell_index (int) – The position of the cell within the notebook object.
execution_count (int) – The execution count to be assigned to the cell (default: Use kernel response)
store_history (bool) – Determines if history should be stored in the kernel (default: False). Specific to ipython kernels, which can store command histories.
- Returns:
output – The execution output payload (or None for no output).
- Return type:
dict
- Raises:
CellExecutionError – If execution failed and should raise an exception, this will be raised with defaults about the failure.
- Returns:
cell – The cell which was just processed.
- Return type:
NotebookNode
- extra_arguments: List¶
An instance of a Python list.
- force_raise_errors: bool¶
A boolean (True, False) trait.
- handle_comm_msg(outs: List, msg: Dict, cell_index: int) None ¶
Handle a comm message.
- interrupt_on_timeout: bool¶
A boolean (True, False) trait.
- iopub_timeout: int¶
An int trait.
- ipython_hist_file: str¶
A trait for unicode strings.
- kernel_manager_class¶
A trait whose value must be a subclass of a specified class.
- kernel_name: str¶
A trait for unicode strings.
- on_cell_complete: Callable | None¶
A trait which is callable.
Notes
Classes are callable, as are instances with a __call__() method.
- on_cell_error: Callable | None¶
A trait which is callable.
Notes
Classes are callable, as are instances with a __call__() method.
- on_cell_execute: Callable | None¶
A trait which is callable.
Notes
Classes are callable, as are instances with a __call__() method.
- on_cell_executed: Callable | None¶
A trait which is callable.
Notes
Classes are callable, as are instances with a __call__() method.
- on_cell_start: Callable | None¶
A trait which is callable.
Notes
Classes are callable, as are instances with a __call__() method.
- on_comm_open_jupyter_widget(msg: Dict) Any | None ¶
Handle a jupyter widget comm open.
- on_notebook_complete: Callable | None¶
A trait which is callable.
Notes
Classes are callable, as are instances with a __call__() method.
- on_notebook_error: Callable | None¶
A trait which is callable.
Notes
Classes are callable, as are instances with a __call__() method.
- on_notebook_start: Callable | None¶
A trait which is callable.
Notes
Classes are callable, as are instances with a __call__() method.
- output(outs: List, msg: Dict, display_id: str, cell_index: int) NotebookNode | None ¶
Handle output.
- process_message(msg: Dict, cell: NotebookNode, cell_index: int) NotebookNode | None ¶
Processes a kernel message, updates cell state, and returns the resulting output object that was appended to cell.outputs.
The input argument cell is modified in-place.
- Parameters:
msg (dict) – The kernel message being processed.
cell (nbformat.NotebookNode) – The cell which is currently being processed.
cell_index (int) – The position of the cell within the notebook object.
- Returns:
output – The execution output payload (or None for no output).
- Return type:
NotebookNode
- Raises:
CellExecutionComplete – Once a message arrives which indicates computation completeness.
- raise_on_iopub_timeout: bool¶
A boolean (True, False) trait.
- record_timing: bool¶
A boolean (True, False) trait.
- register_output_hook(msg_id: str, hook: OutputWidget) None ¶
Registers an override object that handles output/clear_output instead.
Multiple hooks can be registered, where the last one will be used (stack based)
- remove_output_hook(msg_id: str, hook: OutputWidget) None ¶
Unregisters an override object that handles output/clear_output instead
- reset_execution_trackers() None ¶
Resets any per-execution trackers.
- resources¶
An instance of a Python dict.
One or more traits can be passed to the constructor to validate the keys and/or values of the dict. If you need more detailed validation, you may use a custom validator method.
Changed in version 5.0: Added key_trait for validating dict keys.
Changed in version 5.0: Deprecated ambiguous
trait
,traits
args in favor ofvalue_trait
,per_key_traits
.
- set_widgets_metadata() None ¶
Set with widget metadata.
- setup_kernel(**kwargs: Any) Generator ¶
Context manager for setting up the kernel to execute a notebook.
The assigns the Kernel Manager (
self.km
) if missing and Kernel Client(self.kc
).When control returns from the yield it stops the client’s zmq channels, and shuts down the kernel.
- shell_timeout_interval: int¶
An int trait.
- shutdown_kernel¶
An enum whose value must be in a given sequence.
- skip_cells_with_tag: str¶
A trait for unicode strings.
- start_new_kernel(**kwargs: Any) Any ¶
Creates a new kernel.
- Parameters:
kwargs – Any options for
self.kernel_manager_class.start_kernel()
. Because that defaults to AsyncKernelManager, this will likely include options accepted byAsyncKernelManager.start_kernel()
, which includescwd
.
- start_new_kernel_client(**kwargs: Any) Any ¶
Creates a new kernel client.
- Returns:
kc – Kernel client as created by the kernel manager
km
.- Return type:
KernelClient
- startup_timeout: int¶
An int trait.
- store_widget_state: bool¶
A boolean (True, False) trait.
- timeout: int¶
An int trait.
- timeout_func: Callable[[...], int | None]¶
A trait which allows any value.
- wait_for_reply(**kwargs: Any) Any ¶
Wait for a message reply.
- nbclient.client.execute(nb: NotebookNode, cwd: str | None = None, km: KernelManager | None = None, **kwargs: Any) NotebookNode ¶
Execute a notebook’s code, updating outputs within the notebook object.
This is a convenient wrapper around NotebookClient. It returns the modified notebook object.
- Parameters:
nb (NotebookNode) – The notebook object to be executed
cwd (str, optional) – If supplied, the kernel will run in this directory
km (AsyncKernelManager, optional) – If supplied, the specified kernel manager will be used for code execution.
kwargs – Any other options for NotebookClient, e.g. timeout, kernel_name
- nbclient.client.timestamp(msg: Dict | None = None) str ¶
Get the timestamp for a message.
nbclient.exceptions module¶
Exceptions for nbclient.
- exception nbclient.exceptions.CellControlSignal¶
Bases:
Exception
A custom exception used to indicate that the exception is used for cell control actions (not the best model, but it’s needed to cover existing behavior without major refactors).
- exception nbclient.exceptions.CellExecutionComplete¶
Bases:
CellControlSignal
Used as a control signal for cell execution across execute_cell and process_message function calls. Raised when all execution requests are completed and no further messages are expected from the kernel over zeromq channels.
- exception nbclient.exceptions.CellExecutionError(traceback: str, ename: str, evalue: str)¶
Bases:
CellControlSignal
Custom exception to propagate exceptions that are raised during notebook execution to the caller. This is mostly useful when using nbconvert as a library, since it allows to deal with failures gracefully.
- classmethod from_cell_and_msg(cell: NotebookNode, msg: Dict) CellExecutionError ¶
Instantiate from a code cell object and a message contents (message is either execute_reply or error)
- exception nbclient.exceptions.CellTimeoutError¶
Bases:
TimeoutError
,CellControlSignal
A custom exception to capture when a cell has timed out during execution.
- classmethod error_from_timeout_and_cell(msg: str, timeout: int, cell: NotebookNode) CellTimeoutError ¶
Create an error from a timeout on a cell.
- exception nbclient.exceptions.DeadKernelError¶
Bases:
RuntimeError
A dead kernel error.