module ipythonhelper.notebook_runner
¶
Short summary¶
module pyquickhelper.ipythonhelper.notebook_runner
Modified version of runipy.notebook_runner.
Classes¶
class |
truncated documentation |
---|---|
Raised when the execution fails. |
|
Raised when wait_for_ready … |
|
The kernel communicates with mime-types while the notebook uses short labels for different cell types. We’ll use this … |
Static Methods¶
staticmethod |
truncated documentation |
---|---|
Returns the code of a cell. |
Methods¶
method |
truncated documentation |
---|---|
Merges two notebooks together, returns a new none. |
|
We close the kernel. |
|
Returns the number of cells, it iterates on cells to get this information and does cache the information. |
|
Returns a cells container, it may change according to the format. |
|
Checks types for a thumbnail. |
|
Merges images defined by (buffer, format). The method uses PIL to merge images when possible. |
|
Scales an image with the same aspect ratio centered in an image with a given max_width and max_height. |
|
Tries to add tags for a slide show when they are too few. |
|
Approximates the height of a cell by its number of lines it contains. |
|
Returns the cell image or None if not found. |
|
Returns the cell metadata. |
|
Returns the cell type. |
|
Cleans the code before running it, the function comment out instruction such as |
|
Copies the notebook (just the content). |
|
Returns the number of code cells in the notebook. |
|
Creates a picture from text. |
|
Returns the first cell. |
|
Gets summary and description of this notebook. We expect the first cell to contain a title and a description … |
|
Processes the notebook and creates one picture based on the outputs to illustrate a notebook. |
|
Iterates over the notebook cells. |
|
Iterates over the notebook cells containing code. |
|
Appends notebook nb to this one. |
|
Runs a notebook cell and update the output of that cell inplace. |
|
Runs all the cells of a notebook in order and update the outputs in-place. If |
|
Shuts down kernel. |
|
Converts the notebook into JSON. |
|
Converts the notebook into python. |
Documentation¶
Modified version of runipy.notebook_runner.
- exception pyquickhelper.ipythonhelper.notebook_runner.NotebookError[source]¶
Bases:
Exception
Raised when the execution fails.
- exception pyquickhelper.ipythonhelper.notebook_runner.NotebookKernelError[source]¶
Bases:
Exception
Raised when wait_for_ready fails.
- class pyquickhelper.ipythonhelper.notebook_runner.NotebookRunner(nb, profile_dir=None, working_dir=None, comment='', fLOG=<function noLOG>, theNotebook=None, code_init=None, kernel_name='python', log_level='30', extended_args=None, kernel=False, filename=None, replacements=None, detailed_log=None, startup_timeout=300, raise_exception=False)[source]¶
Bases:
object
The kernel communicates with mime-types while the notebook uses short labels for different cell types. We’ll use this to map from kernel types to notebook format types.
This classes executes a notebook end to end.
The class can use different kernels. The next links gives more information on how to create or test a kernel:
Do I need to shutdown the kernel after running a notebook?
If the class is instantiated with kernel=True, a kernel will be started. It must be shutdown otherwise the program might be waiting for it for ever. That is one of the reasons why the travis build does not complete. The build finished but cannot terminate until all kernels are shutdown.
- Parameters:
nb – notebook as JSON
profile_dir – profile directory
working_dir – working directory
comment – additional information added to error message
theNotebook – if not None, populate the variable theNotebook with this value in the notebook
code_init – to initialize the notebook with a python code as if it was a cell
fLOG – logging function
log_level – Choices: (0, 10, 20, 30=default, 40, 50, ‘DEBUG’, ‘INFO’, ‘WARN’, ‘ERROR’, ‘CRITICAL’)
kernel_name – kernel name, it can be None
extended_args – others arguments to pass to the command line (–KernelManager.autorestar=True for example), see Jupyter Notebook Arguments for a full list
kernel – kernel is True by default, the notebook can be run, if False, the notebook can be read but not run
filename – to add the notebook file if there is one in error messages
replacements – replacements to make in every cell before running it, dictionary
{ string: string }
detailed_log – to log detailed information when executing the notebook, this should be a function with the same signature as
print
or Nonestartup_timeout –
wait for this long for the kernel to be ready, see wait_for_ready
raise_exception – raise an exception if a cell raises one
- __add__(nb)[source]¶
Merges two notebooks together, returns a new none.
- Parameters:
nb – notebook
- Returns:
new notebook
- __init__(nb, profile_dir=None, working_dir=None, comment='', fLOG=<function noLOG>, theNotebook=None, code_init=None, kernel_name='python', log_level='30', extended_args=None, kernel=False, filename=None, replacements=None, detailed_log=None, startup_timeout=300, raise_exception=False)[source]¶
- Parameters:
nb – notebook as JSON
profile_dir – profile directory
working_dir – working directory
comment – additional information added to error message
theNotebook – if not None, populate the variable theNotebook with this value in the notebook
code_init – to initialize the notebook with a python code as if it was a cell
fLOG – logging function
log_level – Choices: (0, 10, 20, 30=default, 40, 50, ‘DEBUG’, ‘INFO’, ‘WARN’, ‘ERROR’, ‘CRITICAL’)
kernel_name – kernel name, it can be None
extended_args – others arguments to pass to the command line (–KernelManager.autorestar=True for example), see Jupyter Notebook Arguments for a full list
kernel – kernel is True by default, the notebook can be run, if False, the notebook can be read but not run
filename – to add the notebook file if there is one in error messages
replacements – replacements to make in every cell before running it, dictionary
{ string: string }
detailed_log – to log detailed information when executing the notebook, this should be a function with the same signature as
print
or Nonestartup_timeout –
wait for this long for the kernel to be ready, see wait_for_ready
raise_exception – raise an exception if a cell raises one
- __len__()[source]¶
Returns the number of cells, it iterates on cells to get this information and does cache the information.
- Returns:
int
- _cell_container()[source]¶
Returns a cells container, it may change according to the format.
- Returns:
cell container
- _check_thumbnail_tuple(b)[source]¶
Checks types for a thumbnail.
- Parameters:
b – tuple image, format
- Returns:
b
The function raises an exception if the type is incorrect.
- _merge_images(results)[source]¶
Merges images defined by (buffer, format). The method uses PIL to merge images when possible.
- Returns:
[ (image, format) ]
- _scale_image(in_bytes, format=None, max_width=200, max_height=200)[source]¶
Scales an image with the same aspect ratio centered in an image with a given max_width and max_height.
- Parameters:
in_bytes – image as bytes
format – indication of the format (can be empty)
max_width – maximum size of the thumbnail
max_height – maximum size of the thumbnail
- Returns:
Image (PIL)
- add_tag_slide(max_nb_cell=4, max_nb_line=25)[source]¶
Tries to add tags for a slide show when they are too few.
- Parameters:
max_nb_cell – maximum number of cells within a slide
max_nb_line – maximum number of lines within a slide
- Returns:
list of modified cells { #slide: (kind, reason, cell) }
- cell_height(cell)[source]¶
Approximates the height of a cell by its number of lines it contains.
- Parameters:
cell – cell
- Returns:
number of cell
- cell_image(cell, image_from_text=False)[source]¶
Returns the cell image or None if not found.
- Parameters:
cell – cell to examine
image_from_text – produce an image even if it is not one
- Returns:
None for no image or a list of tuple (image as bytes, extension) for each output of the cell
- cell_type(cell)[source]¶
Returns the cell type.
- Parameters:
cell – from
iter_cells
- Returns:
type
- clean_code(code)[source]¶
Cleans the code before running it, the function comment out instruction such as
show()
.- Parameters:
code – code (string)
- Returns:
cleaned code
- copy()[source]¶
Copies the notebook (just the content).
- Returns:
instance of
NotebookRunner
- create_picture_from(text, format, asbytes=True, context=None)[source]¶
Creates a picture from text.
- Parameters:
text – the text
format – text, json, …
context – (str) indication on the content of text (error, …)
asbytes – results as bytes or as an image
- Returns:
tuple (picture, format) or PIL.Image (if asbytes is False)
The picture will be bytes, the format png, bmp… The size of the picture will depend on the text. The longer, the bigger. The method relies on matplotlib and then convert the image into a PIL image.
HTML could be rendered with QWebPage from PyQt (not implemented).
- static get_cell_code(cell)[source]¶
Returns the code of a cell.
- Parameters:
cell – a cell or a string
- Returns:
boolean (=iscell), string
- get_description()[source]¶
Gets summary and description of this notebook. We expect the first cell to contain a title and a description of its content.
- Returns:
header, description
- get_thumbnail(max_width=200, max_height=200, use_default=False)[source]¶
Processes the notebook and creates one picture based on the outputs to illustrate a notebook.
- Parameters:
max_width – maximum size of the thumbnail
max_height – maximum size of the thumbnail
use_default – force using a default image even if an even is present
- Returns:
- merge_notebook(nb)[source]¶
Appends notebook nb to this one.
- Parameters:
nb – notebook or list of notebook (
NotebookRunner
)- Returns:
number of added cells
How to merge notebook?
The following code merges two notebooks into the first one and stores the result unto a file.
from pyquickhelper.ipythonhelper import read_nb nb1 = read_nb("<file1>", kernel=False) nb2 = read_nb("<file2>", kernel=False) nb1.merge_notebook(nb2) nb1.to_json(outfile)
- run_cell(index_cell, cell, clean_function=None, max_nbissue=15)[source]¶
Runs a notebook cell and update the output of that cell inplace.
- Parameters:
index_cell – index of the cell
cell – cell to execute
clean_function – cleaning function to apply to the code before running it
max_nbissue – number of times an issue can be raised before stopping
- Returns:
output of the cell
- run_notebook(skip_exceptions=False, progress_callback=None, additional_path=None, valid=None, clean_function=None, context=None)[source]¶
Runs all the cells of a notebook in order and update the outputs in-place.
If
skip_exceptions
is set, then if exceptions occur in a cell, the subsequent cells are run (by default, the notebook execution stops).- Parameters:
skip_exceptions – skip exception
progress_callback – call back function
additional_path – additional paths (as a list or None if none)
valid – if not None, valid is a function which returns whether or not the cell should be executed or not, if the function returns None, the execution of the notebooks and skip the execution of the other cells
clean_function – function which cleans a cell’s code before executing it (None for None)
- Returns:
dictionary with statistics
The function adds the local variable
theNotebook
with the absolute file name of the notebook. Function valid can return None to stop the execution of the notebook before this cell.
- to_json(filename=None, encoding='utf8')[source]¶
Converts the notebook into JSON.
- Parameters:
filename – filename or stream
encoding – encoding
- Returns:
Json string if filename is None, None otherwise