Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
Save notebook model to file system.
Note: This differs from the NotebookManager.save_notebook in that
it doesn't have a rename check.
"""
if not self.is_writable(path):
raise Exception("Notebook target is not writable")
bundle_path = self._get_bundle_path(path)
if not os.path.exists(bundle_path):
os.mkdir(bundle_path)
nb = nbformat.from_dict(model['content'])
notary = sign.NotebookNotary()
if notary.check_cells(nb):
notary.sign(nb)
self.write_notebook(bundle_path, nb)
if '__files' in model:
self.write_files(bundle_path, model)
def link_nb(nb_path):
nb = read_nb(nb_path)
cells = nb['cells']
link_markdown_cells(cells, get_imported_modules(cells, Path(nb_path).stem))
write_nb(nb, nb_path)
NotebookNotary().sign(read_nb(nb_path))
def _notary_default(self):
return sign.NotebookNotary(parent=self)
which is always an API-style unicode path,
and always refers to a directory.
- unicode, not url-escaped
- '/'-separated
- leading and trailing '/' will be stripped
- if unspecified, path defaults to '',
indicating the root path.
"""
root_dir = Unicode('/', config=True)
allow_hidden = Bool(False, config=True, help="Allow access to hidden files")
notary = Instance(sign.NotebookNotary)
def _notary_default(self):
return sign.NotebookNotary(parent=self)
hide_globs = List(Unicode(), [
u'__pycache__', '*.pyc', '*.pyo',
'.DS_Store', '*.so', '*.dylib', '*~',
], config=True, help="""
Glob patterns to hide in file and directory listings.
""")
untitled_notebook = Unicode(_("Untitled"), config=True,
help="The base name used when creating untitled notebooks."
)
untitled_file = Unicode("untitled", config=True,
help="The base name used when creating untitled files."
if spec.varargs not in vars:
vars[spec.varargs] = []
vars[spec.varargs].append(value)
template = template.__name__
notebook_dir = base_system_path(base, subdir=NOTEBOOKS)
root = join(notebook_dir, template)
all_args = all_args if all_args else template
name = all_args + IPYNB
path = join(root, name)
makedirs(root, exist_ok=True)
log.info(f'Creating {template} at {path} with {vars}')
notebook = load_notebook(template, vars)
# https://testnb.readthedocs.io/en/latest/security.html
NotebookNotary().sign(notebook)
if exists(path):
log.debug(f'Deleting old version of {path}')
unlink(path)
with open(path, 'w') as out:
log.info(f'Writing {template} to {path}')
nb.write(notebook, out)
return join(template, name)
else:
if spec.varargs not in vars:
vars[spec.varargs] = []
vars[spec.varargs].append(value)
template = template.__name__
base = join(notebook_dir, template)
all_args = all_args if all_args else template
name = all_args + IPYNB
path = join(base, name)
makedirs(base, exist_ok=True)
log.info(f'Creating {template} at {path} with {vars}')
notebook = load_notebook(template, vars)
# https://testnb.readthedocs.io/en/latest/security.html
NotebookNotary().sign(notebook)
if exists(path):
log.warning(f'Deleting old version of {path}')
unlink(path)
with open(path, 'w') as out:
log.info(f'Writing {template} to {path}')
nb.write(notebook, out)
return join(template, name)
def update_nb_metadata(nb_path=None, title=None, summary=None, keywords='fastai', overwrite=True, **kwargs):
"Creates jekyll metadata for given notebook path."
nb = read_nb(nb_path)
data = {'title': title, 'summary': summary, 'keywords': keywords, **kwargs}
data = {k:v for (k,v) in data.items() if v is not None} # remove none values
if not data: return
nb['metadata']['jekyll'] = data
write_nb(nb, nb_path)
NotebookNotary().sign(nb)