Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def with_file_object(f):
nbsource = nbformat.writes(contents)
f.write(nbsource.encode("UTF-8"))
f.flush()
func(f.name)
with_temporary_file(with_file_object)
def exec_notebook(tmp_path: Path) -> str:
"""Helper function to create a list of pathlib Path objects."""
nb = tmp_path / "notebook.ipynb"
nb.write_text(writes(nbless(make_files(tmp_path), kernel="python3")))
return nb.as_posix()
def writes_v3(nb, **kwargs):
MetadataValidatorV3().validate_nb(nb)
return _writes(nb, **kwargs)
self.lib_dirs = eld.lib_dirs
efm = ExtractFinalMetatabDoc()
efm.preprocess(nb_copy, {})
if not efm.doc:
raise MetapackError("Notebook does not contain the metatab output from %mt_final_metatab. Check the executed notebook source")
self.doc = efm.doc
for section, term, value in self.extra_terms:
self.doc[section].get_or_new_term(term, value)
nb, _ = RemoveMetatab().preprocess(nb, {})
resources['outputs']['notebooks/{}.ipynb'.format(self.package_name)] = nbformat.writes(nb).encode('utf-8')
return efm.doc.as_csv(), resources
def translate(self):
visitor = NBTranslator(self.document, self.app, self.docpath)
self.document.walkabout(visitor)
nb = _finilize_markdown_cells(visitor.nb)
if self.app.config.nbexport_execute:
ep = ExecutePreprocessor(allow_errors=True)
try:
ep.preprocess(nb, {'metadata': {}})
except CellExecutionError as e:
self.app.warn(str(e))
self.output = nbformat.writes(nb)
def nbless_cli(in_files: List[str], kernel: str, out_file: str) -> None:
"""Create an executed Jupyter notebook from markdown and code files.
:param in_files: The source files used to create a Jupyter notebook file.
:param kernel: The programming language used to run the notebook.
:param out_file: The name of the output Jupyter notebook file.
"""
nb = nbless(in_files, kernel) if kernel else nbless(in_files)
if out_file:
nbformat.write(nb, out_file, version=4)
else:
sys.stdout.write(nbformat.writes(nb))
def nbexec_cli(in_file: str, kernel: str, out_file: str) -> None:
"""Create an executed notebook without modifying the input notebook.
:param in_file: The name of the input Jupyter notebook file.
:param kernel: The programming language used to execute the notebook.
:param out_file: The name of the output Jupyter notebook file.
"""
nb = nbexec(in_file, kernel) if kernel else nbexec(in_file)
if out_file:
nbformat.write(nb, out_file, version=4)
else:
sys.stdout.write(nbformat.writes(nb))
def writes(self):
"""
https://nbformat.readthedocs.io/en/latest/api.html#nbformat.writes
"""
return nbformat.writes(self.nb)
def write_ipynb(nb, path):
"""Saves a notebook object to the specified path.
Args:
nb_node (nbformat.NotebookNode): Notebook object to save.
notebook_path (str): Path to save the notebook object to.
"""
papermill_io.write(nbformat.writes(nb), path)
c['source'] = c['source'].replace(
'"../data', '"data').replace("'../data", "'data")
notedown.run(notebook, timeout)
print('=== Finished in %f sec'%(time.time()-tic))
# even that we will check it later, but do it ealier so we can see the
# error message before evaluating all notebooks
_check_notebook(notebook)
# write
# need to add language info to for syntax highlight
notebook['metadata'].update({'language_info':{'name':'python'}})
new_fname = _replace_ext(new_fname, 'ipynb')
print('=== Convert %s -> %s' % (fname, new_fname))
with open(new_fname, 'w') as f:
f.write(nbformat.writes(notebook))
converted_files.append((fname, new_fname))
return converted_files