Overview
TheNBRunner class is a wrapper over Runner for executing flows defined in a Jupyter notebook cell. It automatically extracts the flow code from the notebook cell and creates a temporary Python file for execution.
Usage
InstantiateNBRunner on the last line of a notebook cell where a flow is defined. Unlike Runner, this class is not meant to be used as a context manager.
Constructor
NBRunner(flow, show_output=True, profile=None, env=None, base_dir=None, file_read_timeout=3600, **kwargs)
Create a new NBRunner instance for executing a flow defined in a notebook cell.
Parameters:
Flow class defined in the same notebook cell.
Show the stdout and stderr to the console by default. Only applicable for synchronous
nbrun and nbresume functions.Metaflow profile to use to run this flow. If not specified, the default profile is used (or the one already set using
METAFLOW_PROFILE).Additional environment variables to set for the run. This overrides the environment set for this process.
The directory to run the subprocess in. If not specified, the current working directory is used.
The timeout in seconds until which we try to read the runner attribute file.
Additional arguments that you would pass to
python myflow.py before the run command.NBRunnerInitializationError - If NBRunner is not used in an interactive Python environment (such as Jupyter).
Methods
nbrun(**kwargs)
Blocking execution of the flow. This method will wait until the run has completed execution.
Unlike run(), this method returns a metaflow.Run object directly and calls cleanup() internally to support a common notebook pattern of executing a flow and retrieving its results immediately.
Parameters:
Additional arguments that you would pass to
python myflow.py after the run command, in particular, any parameters accepted by the flow.metaflow.Run - A Run object representing the finished run.
Example:
nbresume(**kwargs)
Blocking resume execution of a previously failed run. This method will wait until the resumed run has completed execution.
Unlike resume(), this method returns a metaflow.Run object directly and calls cleanup() internally.
Parameters:
Additional arguments that you would pass to
python myflow.py after the resume command.metaflow.Run - A Run object representing the resumed run.
Example:
run(**kwargs)
Runs the flow and returns an ExecutingRun object. This is equivalent to Runner.run().
Note: When using this method (instead of nbrun()), you must call cleanup() manually.
Parameters:
Additional arguments that you would pass to
python myflow.py after the run command.ExecutingRun - An ExecutingRun object.
Example:
resume(**kwargs)
Resumes the flow and returns an ExecutingRun object. This is equivalent to Runner.resume().
Note: When using this method (instead of nbresume()), you must call cleanup() manually.
Parameters:
Additional arguments that you would pass to
python myflow.py after the resume command.ExecutingRun - An ExecutingRun object.
async_run(**kwargs)
Non-blocking execution of the flow. This method will return as soon as the run has launched. This method is equivalent to Runner.async_run().
Note: This method is asynchronous and needs to be awaited. You must call cleanup() manually after using this method.
Parameters:
Additional arguments that you would pass to
python myflow.py after the run command, in particular, any parameters accepted by the flow.ExecutingRun - An ExecutingRun object representing the run that was started.
Example:
async_resume(**kwargs)
Non-blocking resume execution of the flow. This method will return as soon as the resume has launched. This method is equivalent to Runner.async_resume().
Note: This method is asynchronous and needs to be awaited. You must call cleanup() manually after using this method.
Parameters:
Additional arguments that you would pass to
python myflow.py after the resume command.ExecutingRun - An ExecutingRun object representing the resumed run that was started.
cleanup()
Delete any temporary files created during execution.
You must call this method after using async_run(), async_resume(), run(), or resume(). You don’t need to call this after nbrun() or nbresume() as they call it internally.
Example:
Differences from Runner
- No context manager:
NBRunneris not meant to be used as a context manager - Automatic cleanup:
nbrun()andnbresume()callcleanup()internally - Direct Run object:
nbrun()andnbresume()return ametaflow.Runobject directly instead ofExecutingRun - Notebook-specific: Automatically extracts flow code from the notebook cell
- Environment handling: Automatically clears the
JPY_PARENT_PIDenvironment variable to prevent interference
Notes
NBRunnerrequires an interactive Python environment (such as Jupyter)- The flow must be defined in the same notebook cell as the
NBRunnerinstantiation - Temporary flow files are created in
base_dir(or the current working directory) - When using non-blocking APIs (
async_run,async_resume,run,resume), remember to callcleanup()explicitly
