jupyter: run notebooks, completion in a notebook, notebook kernels, magic commands

completion

pyquickhelper.ipythonhelper.AutoCompletion (self, value = None)

You can add auto completion object to IPython by adding member to an instance of this class.

All members must begin by _

pyquickhelper.ipythonhelper.AutoCompletionFile (self, value)

builds a tree based on a list of files, the class adds A__ before every folder or file starting with _

controls

pyquickhelper.ipythonhelper.open_html_form (params, title = ‘’, key_save = ‘’, style = ‘background-color:gainsboro; padding:2px; border:0px;’, raw = False, hook = None)

The function displays a form onto a notebook, it requires a notebook to be open.

pyquickhelper.ipythonhelper.DropDownWidget (self, values, name = None, labels = None, default = None, divclass = None, delimiter = ‘ ‘)

drop down list

pyquickhelper.ipythonhelper.RangeWidget (self, min, max, step = 1, name = None, default = None, width = 350, divclass = None, show_range = False)

Range (slider) widget

The class overloads html and values.

pyquickhelper.ipythonhelper.RadioWidget (self, values, name = None, labels = None, default = None, divclass = None, delimiter = ‘ ‘)

radio button

pyquickhelper.ipythonhelper.RangeWidget (self, min, max, step = 1, name = None, default = None, width = 350, divclass = None, show_range = False)

Range (slider) widget

The class overloads html and values.

pyquickhelper.ipythonhelper.StaticInteract (self, function, kwargs)

Static Interact Object

See notebook Having a form in a notebook.

kernels

pyquickhelper.ipythonhelper.find_notebook_kernel (kernel_spec_manager = None)

Returns a dict mapping kernel names to resource directories.

pyquickhelper.ipythonhelper.get_installed_notebook_extension (user = False, prefix = None, nbextensions_dir = None)

Retuns installed extensions.

pyquickhelper.ipythonhelper.get_jupyter_datadir ()

Returns the data directory for the notebook.

pyquickhelper.ipythonhelper.get_notebook_kernel (kernel_name, kernel_spec_manager = None)

Returns a KernelSpec.

pyquickhelper.ipythonhelper.install_jupyter_kernel (exe = ‘somewhere/workspace/pyquickhelper/pyquickhelper_UT_39_std/_venv/bin/python3.9’, kernel_spec_manager = None, user = False, kernel_name = None, prefix = None)

Installs a kernel based on executable (this python by default).

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

Installs notebook extensions, see install_nbextension for documentation.

pyquickhelper.ipythonhelper.install_python_kernel_for_unittest (suffix = None)

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

pyquickhelper.ipythonhelper.remove_kernel (kernel_name, kernel_spec_manager = None)

Removes a kernel.

pyquickhelper.ipythonhelper.upgrade_notebook (filename, encoding = ‘utf-8’)

Converts a notebook from version 2 to latest.

magic command helpers

pyquickhelper.ipythonhelper.MagicClassWithHelpers (self, shell = None, kwargs)

Provides some functions reused in others classes inherited from Magics. The class should not be registered as it is but should be used as an ancestor for another class. It can be registered this way:

def register_file_magics():
    from IPython import get_ipython
    ip = get_ipython()
    ip.register_magics(MagicFile)

add_context (self, context)

add context to the class, mostly for debug purpose

get_args (self, line, parser, print_function = <built-in function print>)

parser a command line with a given parser

get_parser (self, parser_class, name)

Returns a parser for a magic command, initializes it if it does not exists, it creates it. The parsers are stored in static member _parser_store.

pyquickhelper.ipythonhelper.MagicCommandParser (self, prog, args, kwargs)

Adds method parse_cmd to argparse.ArgumentParser.

add_argument (self, args, kwargs)

Overloads the method, see ArgumentParser. Among the parameters:

  • no_eval: avoid considering the parameter value as a potential variable stored in the notebook workspace.

  • eval_type: type can be used for parsing and eval_type is the expected return type.

The method adds parameter no_eval to avoid considering the parameter value as a potential variable stored in the notebook workspace.

eval (self, value, context, fLOG = <function noLOG at 0x7fda35240dc0>)

Evaluate a string knowing the context, it returns value if it does not belong to the context or if it contains brackets or symbols (+, *), if the value cannot be evaluated (with function eval), it returns the value value

expected_eval_type (self, name)

return the expected evaluation type for the parameter (if the value is interpreter as a python expression)

expected_type (self, name)

Returns the expected type for the parameter.

has_choices (self, name)

tells if a parameter has choises

has_eval (self, name)

Tells if a parameter value should be consider as a variable or some python code to evaluate.

parse_cmd (self, line, context = None, fLOG = <function noLOG at 0x7fda35240dc0>)

Splits line using shlex and call parse_args

notebook

pyquickhelper.ipythonhelper.execute_notebook_list (folder, notebooks, clean_function = None, valid = None, fLOG = <function noLOG at 0x7fda35240dc0>, additional_path = None, deepfLOG = <function noLOG at 0x7fda35240dc0>, kernel_name = ‘python’, log_level = ‘30’, extended_args = None, cache_urls = None, replacements = None, detailed_log = None, startup_timeout = 300)

Executes a list of notebooks.

pyquickhelper.ipythonhelper.execute_notebook_list_finalize_ut (res, dump = None, fLOG = <function noLOG at 0x7fda35240dc0>)

Checks the list of results and raises an exception if one failed. This is meant to be used in unit tests.

pyquickhelper.helpgen.nb2html (nb_file, outfile, exc = True)

Converts a notebook into HTML.

pyquickhelper.helpgen.nb2slides (nb_file, outfile, add_tag = True)

Converts a notebook into slides, it copies reveal.js if not present in the folder of the output.

pyquickhelper.helpgen.nb2rst (nb_file, outfile, exc = True, post_process = True)

Converts a notebook into RST.

pyquickhelper.helpgen.NbImage (name, repository = None, force_github = False, width = None, branch = ‘master’, row_height = 200, background = (255, 255, 255))

Retrieves a name or a url of the image if it is not found in the local folder or a subfolder.

pyquickhelper.helpgen.process_notebooks (notebooks, outfold, build, latex_path = None, pandoc_path = None, formats = ‘ipynb,html,python,rst,slides,pdf,github’, fLOG = <function fLOG at 0x7fda35240e50>, exc = True, remove_unicode_latex = False, nblinks = None, notebook_replacements = None)

Converts notebooks into html, rst, latex, pdf, python, docx using nbconvert.

pyquickhelper.ipythonhelper.read_nb (filename, profile_dir = None, encoding = ‘utf8’, working_dir = None, comment = ‘’, fLOG = <function noLOG at 0x7fda35240dc0>, code_init = None, kernel_name = ‘python’, log_level = ‘30’, extended_args = None, kernel = False, replacements = None)

Reads a notebook and return a NotebookRunner object.

pyquickhelper.ipythonhelper.remove_execution_number (infile, outfile = None, encoding = ‘utf-8’, indent = 2, rule = <class ‘int’>)

Removes execution number from a notebook.

pyquickhelper.ipythonhelper.run_notebook (filename, profile_dir = None, working_dir = None, skip_exceptions = False, outfilename = None, encoding = ‘utf8’, additional_path = None, valid = None, clean_function = None, code_init = None, fLOG = <function noLOG at 0x7fda35240dc0>, kernel_name = ‘python’, log_level = ‘30’, extended_args = None, cache_urls = None, replacements = None, detailed_log = None, startup_timeout = 30, raise_exception = False)

Runs a notebook end to end, it is inspired from module runipy.

test

pyquickhelper.ipythonhelper.test_notebook_execution_coverage (filename, name, folder, this_module_name, valid = None, copy_files = None, modules = None, filter_name = None, fLOG = <function noLOG at 0x7fda35240dc0>)

Runs and tests a specific list of notebooks. The function raises an exception if the execution fails.