forked from nteract/papermill
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Guess params by introspecting the _parameters_ cell (nteract#531)
* Add Translator.inspect * Apply changes following review Co-authored-by: Frédéric Collonval <[email protected]>
- Loading branch information
1 parent
33b3e1c
commit 82b5c5d
Showing
19 changed files
with
674 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
Inspect | ||
======= | ||
|
||
The two ways to inspect the notebook to discover its parameters are: (1) through the | ||
Python API and (2) through the command line interface. | ||
|
||
Execute via the Python API | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The `inspect_notebook` function can be called to inspect a notebook: | ||
|
||
.. code-block:: python | ||
inspect_notebook(<notebook path>) | ||
.. code-block:: python | ||
import papermill as pm | ||
pm.inspect_notebook('path/to/input.ipynb') | ||
.. note:: | ||
If your path is parametrized, you can pass those parameters in a dictionary | ||
as second parameter: | ||
|
||
``inspect_notebook('path/to/input_{month}.ipynb', parameters={month='Feb'})`` | ||
|
||
Inspect via CLI | ||
~~~~~~~~~~~~~~~ | ||
|
||
To inspect a notebook using the CLI, enter the ``papermill --help-notebook`` command in the | ||
terminal with the notebook and optionally path parameters. | ||
|
||
.. seealso:: | ||
|
||
:doc:`CLI reference <./usage-cli>` | ||
|
||
Inspect a notebook | ||
^^^^^^^^^^^^^^^^^^ | ||
|
||
Here's an example of a local notebook being inspected and an output example: | ||
|
||
.. code-block:: bash | ||
papermill --help-notebook ./papermill/tests/notebooks/complex_parameters.ipynb | ||
Usage: papermill [OPTIONS] NOTEBOOK_PATH [OUTPUT_PATH] | ||
Parameters inferred for notebook './papermill/tests/notebooks/complex_parameters.ipynb': | ||
msg: Unknown type (default None) | ||
a: float (default 2.25) Variable a | ||
b: List[str] (default ['Hello','World']) | ||
Nice list | ||
c: NoneType (default None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ a collection of notebooks. | |
:maxdepth: 2 | ||
|
||
usage-parameterize | ||
usage-inspect | ||
usage-execute | ||
usage-store | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Deduce parameters of a notebook from the parameters cell.""" | ||
import click | ||
|
||
from .iorw import get_pretty_path, load_notebook_node, local_file_io_cwd | ||
from .log import logger | ||
from .parameterize import add_builtin_parameters, parameterize_path | ||
from .translators import papermill_translators | ||
from .utils import any_tagged_cell, find_first_tagged_cell_index | ||
|
||
|
||
def _open_notebook(notebook_path, parameters): | ||
path_parameters = add_builtin_parameters(parameters) | ||
input_path = parameterize_path(notebook_path, path_parameters) | ||
logger.info("Input Notebook: %s" % get_pretty_path(input_path)) | ||
|
||
with local_file_io_cwd(): | ||
return load_notebook_node(input_path) | ||
|
||
|
||
def _infer_parameters(nb): | ||
"""Infer the notebook parameters. | ||
Parameters | ||
---------- | ||
nb : nbformat.NotebookNode | ||
Notebook | ||
Returns | ||
------- | ||
List[Parameter] | ||
List of parameters (name, inferred_type_name, default, help) | ||
""" | ||
params = [] | ||
|
||
parameter_cell_idx = find_first_tagged_cell_index(nb, "parameters") | ||
if parameter_cell_idx < 0: | ||
return params | ||
parameter_cell = nb.cells[parameter_cell_idx] | ||
kernel_name = nb.metadata.kernelspec.name | ||
language = nb.metadata.kernelspec.language | ||
|
||
translator = papermill_translators.find_translator(kernel_name, language) | ||
try: | ||
params = translator.inspect(parameter_cell) | ||
except NotImplementedError: | ||
logger.warning( | ||
"Translator for '{}' language does not support parameter introspection.".format( | ||
language | ||
) | ||
) | ||
|
||
return params | ||
|
||
|
||
def display_notebook_help(ctx, notebook_path, parameters): | ||
"""Display help on notebook parameters. | ||
Parameters | ||
---------- | ||
ctx : click.Context | ||
Click context | ||
notebook_path : str | ||
Path to the notebook to be inspected | ||
""" | ||
nb = _open_notebook(notebook_path, parameters) | ||
click.echo(ctx.command.get_usage(ctx)) | ||
pretty_path = get_pretty_path(notebook_path) | ||
click.echo("\nParameters inferred for notebook '{}':".format(pretty_path)) | ||
|
||
if not any_tagged_cell(nb, "parameters"): | ||
click.echo("\n No cell tagged 'parameters'") | ||
return 1 | ||
|
||
params = _infer_parameters(nb) | ||
if params: | ||
for param in params: | ||
p = param._asdict() | ||
type_repr = p["inferred_type_name"] | ||
if type_repr == "None": | ||
type_repr = "Unknown type" | ||
|
||
definition = " {}: {} (default {})".format(p["name"], type_repr, p["default"]) | ||
if len(definition) > 30: | ||
if len(p["help"]): | ||
param_help = "".join((definition, "\n", 34 * " ", p["help"])) | ||
else: | ||
param_help = definition | ||
else: | ||
param_help = "{:<34}{}".format(definition, p["help"]) | ||
click.echo(param_help) | ||
else: | ||
click.echo( | ||
"\n Can't infer anything about this notebook's parameters. " | ||
"It may not have any parameter defined." | ||
) | ||
|
||
return 0 | ||
|
||
|
||
def inspect_notebook(notebook_path, parameters=None): | ||
"""Return the inferred notebook parameters. | ||
Parameters | ||
---------- | ||
notebook_path : str | ||
Path to notebook | ||
parameters : dict, optional | ||
Arbitrary keyword arguments to pass to the notebook parameters | ||
Returns | ||
------- | ||
Dict[str, Parameter] | ||
Mapping of (parameter name, {name, inferred_type_name, default, help}) | ||
""" | ||
nb = _open_notebook(notebook_path, parameters) | ||
|
||
params = _infer_parameters(nb) | ||
return {p.name: p._asdict() for p in params} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Models used by papermill.""" | ||
from collections import namedtuple | ||
|
||
Parameter = namedtuple('Parameter', [ | ||
'name', | ||
'inferred_type_name', # string of type | ||
'default', # string representing the default value | ||
'help', | ||
]) |
Oops, something went wrong.