Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from nbconvert.preprocessors import Preprocessor
from traitlets import List, Unicode, Bool
class NbGraderPreprocessor(Preprocessor):
default_language = Unicode('ipython')
display_data_priority = List(['text/html', 'application/pdf', 'text/latex', 'image/svg+xml', 'image/png', 'image/jpeg', 'text/plain'])
enabled = Bool(True, help="Whether to use this preprocessor when running nbgrader").tag(config=True)
def clear_notebook(path):
real_path = Path(path)
body = ""
with open(real_path, "r", encoding="utf-8") as nbfile:
nb = nbformat.read(nbfile, as_version=4)
co = ClearOutputPreprocessor()
exporter = NotebookExporter()
node, resources = co.preprocess(nb, {'metadata': {'path': './'}})
body, resources = exporter.from_notebook_node(node, resources)
with open(real_path, "w", encoding="utf-8") as nbfile:
nbfile.write(body)
def __init__(self, config=None, export_all=False, **kwargs):
self.output_extension = '.sos'
self.output_mimetype = 'text/x-sos'
self.export_all = export_all
Exporter.__init__(self, config, **kwargs)
def execute_notebook(npth, dpth, timeout=1800, kernel='python3'):
"""
Execute the notebook at `npth` using `dpth` as the execution
directory. The execution timeout and kernel are `timeout` and
`kernel` respectively.
"""
ep = ExecutePreprocessor(timeout=timeout, kernel_name=kernel)
nb = nbformat.read(npth, as_version=4)
t0 = timer()
ep.preprocess(nb, {'metadata': {'path': dpth}})
t1 = timer()
with open(npth, 'wt') as f:
nbformat.write(nb, f)
return t1 - t0
def render_code(self, cell):
captured_outputs = ['text', 'html', 'svg', 'latex', 'javascript']
cell = copy.deepcopy(cell)
cell['input'] = ''
for output in cell.outputs:
if output.output_type != 'display_data':
cell.outputs.remove(output)
return nb.ConverterNotebook.render_code(self, cell)