Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
return "\n".join([prefix + t for t in text.split("\n")])
unprepend_line = lambda x, p: x[len(p):] if x.startswith(p) else x
def unprepend_lines(text, prefix):
return "\n".join([unprepend_line(t, prefix) for t in text.split("\n")])
# ensure NotebookNode objects are represented as plain dicts in YAML
def NN_representer(dumper, data):
return dumper.represent_mapping("tag:yaml.org,2002:map", dict(data),
flow_style=False)
yaml.add_representer(nbformat.NotebookNode, NN_representer)
def read_ipynb(infile, header=None):
with open(infile) as f:
node = nbformat.reader.read(f)
# ipynb format 4 is current as of IPython 3.0; update the data structure
# for consistency if it is an older version
ipynb_version = nbformat.reader.get_version(node)
if ipynb_version < (4, 0):
node = nbformat.convert(node, 4)
notebook_lang = node.metadata.get('language_info', {}).get('name', None)
if not notebook_lang == 'R':
print('Warning: Notebook language "{0}" != R'.format(notebook_lang))
print("Output is unlikely to be a valid Rmd document")
def new_latex_cell(source=''):
return NotebookNode(
cell_type='raw',
metadata=NotebookNode(raw_mimetype='text/latex'),
source=source,
)
# The current code generates notebooks version 3
NBFORMAT_VERSION = 3
if IPython:
if LooseVersion('5.0.0') > LooseVersion(IPython.__version__) >= LooseVersion('4.0.0'):
import nbformat
from jupyter_client.manager import KernelManager
def read_notebook(notebook_in): # pylint: disable=function-redefined
return nbformat.reads(notebook_in, NBFORMAT_VERSION) # pylint: disable=E1101
def write_notebook(notebook, fout): # pylint: disable=function-redefined
nbformat.write(notebook, fout) # pylint: disable=E1101
NotebookNode = nbformat.NotebookNode # pylint: disable=E1101
IPYTHON_NBCONVERT_HTML = ['jupyter', 'nbconvert', '--to html']
IPYTHON_NBCONVERT_PDF = ['jupyter', 'nbconvert', '--to pdf']
elif LooseVersion('4.0.0') > LooseVersion(IPython.__version__) >= LooseVersion('3.0.0'):
from IPython.kernel import KernelManager
import IPython.nbformat
def read_notebook(notebook_in): # pylint: disable=function-redefined
return IPython.nbformat.reads(notebook_in, NBFORMAT_VERSION) # pylint: disable=E1101
def write_notebook(notebook, fout): # pylint: disable=function-redefined
IPython.nbformat.write(notebook, fout) # pylint: disable=E1101
NotebookNode = IPython.nbformat.NotebookNode # pylint: disable=E1101
deleted_keys.add(key)
elif op == DiffOp.REPLACE:
assert key not in deleted_keys, 'cannot replace deleted key: %r' % key
newobj[key] = e.value
elif op == DiffOp.PATCH:
assert key not in deleted_keys, 'cannot patch deleted key: %r' % key
newobj[key] = patch(obj[key], e.diff)
else:
raise NBDiffFormatError("Invalid op {}.".format(op))
# Take items not mentioned in diff
for key in obj:
if key not in deleted_keys and key not in newobj:
newobj[key] = copy.deepcopy(obj[key])
return NotebookNode(newobj)
def new_latex_cell(source=''):
return NotebookNode(
cell_type='raw',
metadata=NotebookNode(raw_mimetype='text/latex'),
source=source,
)
def _set_notebook_metadata(nb):
# Set up notebook metadata to identify the kernel used
nb['metadata']['kernel_info'] = nbformat.NotebookNode()
nb['metadata']['kernel_info']['name'] = 'python2'
# Also include the metadata that would be written by JupyterLab to select a specific
# kernelspec, so that it doesn't prompt the user to select a kernel when the notebook
# is opened for the first time.
nb['metadata']['kernelspec'] = nbformat.NotebookNode()
nb['metadata']['kernelspec']['name'] = 'python2'
nb['metadata']['kernelspec']['language'] = 'python'
nb['metadata']['kernelspec']['display_name'] = 'Python 2'
# And tell CodeMirror what language to use for syntax highlighting.
nb['metadata']['language_info'] = nbformat.NotebookNode()
nb['metadata']['language_info']['name'] = 'python'
nb['metadata']['language_info']['version'] = 2
nb['metadata']['language_info']['codemirror_mode'] = nbformat.NotebookNode()
nb['metadata']['language_info']['codemirror_mode']['name'] = 'ipython'
nb['metadata']['language_info']['codemirror_mode']['version'] = 2
def _set_notebook_metadata(nb):
# Set up notebook metadata to identify the kernel used
nb['metadata']['kernel_info'] = nbformat.NotebookNode()
nb['metadata']['kernel_info']['name'] = 'python2'
# Also include the metadata that would be written by JupyterLab to select a specific
# kernelspec, so that it doesn't prompt the user to select a kernel when the notebook
# is opened for the first time.
nb['metadata']['kernelspec'] = nbformat.NotebookNode()
nb['metadata']['kernelspec']['name'] = 'python2'
nb['metadata']['kernelspec']['language'] = 'python'
nb['metadata']['kernelspec']['display_name'] = 'Python 2'
# And tell CodeMirror what language to use for syntax highlighting.
nb['metadata']['language_info'] = nbformat.NotebookNode()
nb['metadata']['language_info']['name'] = 'python'
nb['metadata']['language_info']['version'] = 2
nb['metadata']['language_info']['codemirror_mode'] = nbformat.NotebookNode()
nb['metadata']['language_info']['codemirror_mode']['name'] = 'ipython'
nb['metadata']['language_info']['codemirror_mode']['version'] = 2
def fix_notebook(self, notebook):
"""Returns a notebook object with a valid kernelspec.
In case the kernel is not found, we search for a matching kernel based on the language.
"""
# Fetch kernel name from the notebook metadata
if 'kernelspec' not in notebook.metadata:
notebook.metadata.kernelspec = nbformat.NotebookNode()
kernelspec = notebook.metadata.kernelspec
kernel_name = kernelspec.get('name', self.kernel_manager.default_kernel_name)
# We use `maybe_future` to support RemoteKernelSpecManager
all_kernel_specs = yield tornado.gen.maybe_future(self.kernel_spec_manager.get_all_specs())
# Find a spec matching the language if the kernel name does not exist in the kernelspecs
if kernel_name not in all_kernel_specs:
missing_kernel_name = kernel_name
kernel_name = yield self.find_kernel_name_for_language(kernelspec.language.lower(), kernel_specs=all_kernel_specs)
self.log.warning('Could not find a kernel named %r, will use %r', missing_kernel_name, kernel_name)
# We make sure the notebook's kernelspec is correct
notebook.metadata.kernelspec.name = kernel_name
notebook.metadata.kernelspec.display_name = all_kernel_specs[kernel_name]['spec']['display_name']
notebook.metadata.kernelspec.language = all_kernel_specs[kernel_name]['spec']['language']
raise tornado.gen.Return(notebook) # TODO py2: replace by return