module ipythonhelper.notebook_helper

Short summary

module pyquickhelper.ipythonhelper.notebook_helper

Some automation helpers about notebooks

source on GitHub

Functions

function

truncated documentation

find_notebook_kernel

return a dict mapping kernel names to resource directories

get_installed_notebook_extension

Parameters ———- user : bool [default: False] Whether to check the user’s .ipython/nbextensions directory. …

get_jupyter_datadir

Returns the data directory for the notebook.

get_jupyter_extension_dir

Parameters ++++++++++ user : bool [default: False] Whether to check the user’s .ipython/nbextensions directory. …

get_notebook_kernel

Returns a KernelSpec.

install_jupyter_kernel

Install a kernel based on executable (this python by default)

install_notebook_extension

Install notebook extensions, see install_nbextension

install_python_kernel_for_unittest

Installs a kernel based on this python (sys.executable) for unit test purposes.

read_nb

Reads a notebook and return a NotebookRunner object.

read_nb_json

Reads a notebook from a JSON stream or string.

remove_execution_number

Removes execution number from a notebook.

remove_kernel

Removes a kernel.

upgrade_notebook

Converts a notebook from version 2 to latest.

writes

Write a notebook to a string in a given format in the current nbformat version. This function always writes the notebook …

Documentation

Some automation helpers about notebooks

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.find_notebook_kernel(kernel_spec_manager=None)[source][source]

return a dict mapping kernel names to resource directories

Parameters

kernel_spec_manager – see KernelSpecManager A KernelSpecManager to use for installation. If none provided, a default instance will be created.

Returns

dict

The list of installed kernels is described at Making kernel for Jupyter. The function only works with Jupyter>=4.0.

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.get_installed_notebook_extension(user=False, prefix=None, nbextensions_dir=None)[source][source]
Parameters
  • user (bool [default: False]) – Whether to check the user’s .ipython/nbextensions directory. Otherwise check a system-wide install (e.g. /usr/local/share/jupyter/nbextensions).

  • prefix (str [optional]) – Specify install prefix, if it should differ from default (e.g. /usr/local). Will check prefix/share/jupyter/nbextensions

  • nbextensions_dir (str [optional]) – Specify absolute path of nbextensions directory explicitly.

Returns

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.get_jupyter_datadir()[source][source]

Returns the data directory for the notebook.

Returns

path

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.get_jupyter_extension_dir(user=False, prefix=None, nbextensions_dir=None)[source][source]
Parameters
  • user (bool [default: False]) – Whether to check the user’s .ipython/nbextensions directory. Otherwise check a system-wide install (e.g. /usr/local/share/jupyter/nbextensions).

  • prefix (str [optional]) – Specify install prefix, if it should differ from default (e.g. /usr/local). Will check prefix/share/jupyter/nbextensions

  • nbextensions_dir (str [optional]) – Specify absolute path of nbextensions directory explicitly.

Returns

path

Return type

path to installed extensions (by the user)

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.get_notebook_kernel(kernel_name, kernel_spec_manager=None)[source][source]

Returns a KernelSpec.

Parameters
  • kernel_spec_manager

    see KernelSpecManager A KernelSpecManager to use for installation. If none provided, a default instance will be created.

  • kernel_name – kernel name

Returns

KernelSpec

The function only works with Jupyter>=4.0.

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.install_jupyter_kernel(exe='somewhere/workspace/pyquickhelper/pyquickhelper_UT_37_std/_venv/bin/python3.7', kernel_spec_manager=None, user=False, kernel_name=None, prefix=None)[source][source]

Install a kernel based on executable (this python by default)

Parameters
  • exe – Python executable current one by default

  • kernel_spec_manager – (KernelSpecManager [optional]). A KernelSpecManager to use for installation. If none provided, a default instance will be created.

  • user – (bool). Whether to do a user-only install, or system-wide.

  • kernel_name – (str), optional. Specify a name for the kernelspec. This is needed for having multiple IPython kernels for different environments.

  • prefix – (str), optional. Specify an install prefix for the kernelspec. This is needed to install into a non-default location, such as a conda/virtual-env.

Returns

The path where the kernelspec was installed.

A kernel is defined by the following fields:

{
    "display_name": "Python 3 (ENSAE)",
    "language": "python",
    "argv": [ "c:\\PythonENSAE\\python\\python.exe",
              "-m",
              "ipykernel",
              "-f",
              "{connection_file}"
            ]
 }

For R, it looks like:

{
    "display_name": "R (ENSAE)",
    "language": "R",
    "argv": [ "c:\\PythonENSAE\\tools\\R\\bin\\x64\\R.exe",
              "--quiet",
              "-e",
              "IRkernel::main()",
              "--args",
              "{connection_file}"
            ]
}

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.install_notebook_extension(path=None, overwrite=False, symlink=False, user=False, prefix=None, nbextensions_dir=None, destination=None)[source][source]

Install notebook extensions, see install_nbextension for documentation.

Parameters
  • path – if None, use default value

  • overwrite – overwrite the extension

  • symlink – see the original function

  • user – user

  • prefix – see the original function

  • nbextensions_dir – see the original function

  • destination – see the original function

Returns

standard output

Default value is https://github.com/ipython-contrib/IPython-notebook-extensions/archive/master.zip.

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.install_python_kernel_for_unittest(suffix=None)[source][source]

Installs a kernel based on this python (sys.executable) for unit test purposes.

Parameters

suffix – suffix to add to the kernel name

Returns

kernel name

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.read_nb(filename, profile_dir=None, encoding='utf8', working_dir=None, comment='', fLOG=<function noLOG>, code_init=None, kernel_name='python', log_level='30', extended_args=None, kernel=False, replacements=None)[source][source]

Reads a notebook and return a NotebookRunner object.

Parameters
  • filename – notebook filename (or stream)

  • profile_dir – profile directory

  • encoding – encoding for the notebooks

  • working_dir – working directory

  • comment – additional information added to error message

  • 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

  • kernelkernel is True by default, the notebook can be run, if False, the notebook can be read but not run

  • replacements – replacements to make in every cell before running it, dictionary { string: string }

Returns

NotebookRunner

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.read_nb_json(js, profile_dir=None, encoding='utf8', working_dir=None, comment='', fLOG=<function noLOG>, code_init=None, kernel_name='python', log_level='30', extended_args=None, kernel=False, replacements=None)[source][source]

Reads a notebook from a JSON stream or string.

Parameters
  • js – string or stream

  • profile_dir – profile directory

  • encoding – encoding for the notebooks

  • working_dir – working directory

  • comment – additional information added to error message

  • 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

  • kernelkernel is True by default, the notebook can be run, if False, the notebook can be read but not run

  • replacements – replacements to make in every cell before running it, dictionary { string: string }

Returns

instance of NotebookRunner

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.remove_execution_number(infile, outfile=None, encoding='utf-8', indent=2, rule=<class 'int'>)[source][source]

Removes execution number from a notebook.

Parameters
  • infile – filename of the notebook

  • outfile – None ot save the file

  • encoding – encoding

  • indent – indentation

  • rule – determines the rule which specifies execution numbers, ‘None’ for None, ‘int’ for consectuive integers numbers.

Returns

modified string or None if outfile is not None and the file was not modified

notebook 5.1.0 introduced changes which are incompatible with leaving the cell executing number empty.

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.remove_kernel(kernel_name, kernel_spec_manager=None)[source][source]

Removes a kernel.

Parameters
  • kernel_spec_manager

    see KernelSpecManager A KernelSpecManager to use for installation. If none provided, a default instance will be created.

  • kernel_name – kernel name

The function only works with Jupyter>=4.0.

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.upgrade_notebook(filename, encoding='utf-8')[source][source]

Converts a notebook from version 2 to latest.

Parameters
  • filename – filename

  • encoding – encoding

Returns

modification?

source on GitHub

pyquickhelper.ipythonhelper.notebook_helper.writes(nb, **kwargs)[source][source]

Write a notebook to a string in a given format in the current nbformat version.

This function always writes the notebook in the current nbformat version.

Parameters
  • nb (NotebookNode) – The notebook to write.

  • kwargs – Among these parameters, version (int) which is The nbformat version to write. Used for downgrading notebooks.

Returns

s – The notebook string.

Return type

unicode

source on GitHub