Skip to content

Add on_cell_input Callable to Support Input #329

@dgrahn

Description

@dgrahn

Hello!

I am working on a project that requires executing Jupyter Notebooks from a Python script. nbclient would be ideal to do this, unfortunately the notebooks frequently request user input with input or getpass. Per the Jupyter Contribution Workflow, I am opening a proposal here. If accepted, I'm happy to implement this myself.

I propose a new callable named on_cell_input (or on_cell_input_request) is created. It is called with kwargs cell, cell_index and cell_messages. When this is set, the client will be updated to keep a log of cell messages and call the on_cell_input callable whenever an std message is requested.

This is the code in question.

nbclient/nbclient/client.py

Lines 982 to 986 in 2650b21

parent_msg_id = await ensure_async(
self.kc.execute(
cell.source, store_history=store_history, stop_on_error=not cell_allows_errors
)
)

It would be updated roughly as:

if self.on_cell_input:
   run_cell = self.kc.execute(...)
   # handle messages
   parent_msg_id = await ensure_async(run_cell)
else:
   # Continue as usual

As this path will only be followed followed when on_cell_input is set, it should have minimal impact on anyone not using it (exactly one if-statement to check whether

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions