def __init__(self, config=None, reorder=False, reset_index=False, add_header=False, no_index=False, remove_magic=False, **kwargs): self.reorder = reorder self.reset_index = reset_index self.add_header = add_header self.no_index = no_index self.remove_magic = remove_magic self.output_extension = '.sos' self.output_mimetype = 'text/x-sos' Exporter.__init__(self, config, **kwargs)
def variant(time, source, past, durl, max_distance, notebook_path): """Generate and run a variant of an analysis notebook based on a specific substitution-detection model.""" # Create the model defined on the command line. time, source, past, durl = map(lambda s: s.split('.')[1], [time, source, past, durl]) model = Model(time=Time[time], source=Source[source], past=Past[past], durl=Durl[durl], max_distance=max_distance) notebook_folder, notebook_file = split(notebook_path) variant_path = settings.NOTEBOOK.format(model=model, notebook=notebook_file) _, variant_file = split(variant_path) # Read the source notebook and generate the appropriate variant. if not exists(notebook_path): raise Exception("Couldn't find notebook '{}'".format(notebook_path)) logger.debug("Reading notebook '{}'".format(notebook_file)) with open(notebook_path) as f: notebook = nbformat.read(f, as_version=4) logger.info("Creating notebook '{}'".format(variant_file)) model_str = '{}'.format(model) for cell in notebook.cells: cell['source'] = re.sub(r'Model\(.*?\)', model_str, cell['source']) # Execute the notebook and extract its figures. logger.info("Executing and extracting figures from notebook '{}'" .format(variant_file)) config = Config() config.Exporter.preprocessors = [ 'nbconvert.preprocessors.ExecutePreprocessor', 'nbconvert.preprocessors.ExtractOutputPreprocessor' ] config.ExecutePreprocessor.timeout = 12 * 3600 exporter = Exporter(config=config) variant, resources = exporter.from_notebook_node(notebook, { 'metadata': {'path': notebook_folder}, 'unique_key': 'figure' }) # Save the resulting notebook and figures. logger.debug("Saving notebook '{}'".format(variant_file)) with open(variant_path, 'wt') as f: nbformat.write(variant, f) logger.debug("Saving figures from notebook '{}'".format(variant_file)) figures_dir = settings.FIGURE_VARIANTS.format( notebook=notebook_file, model=model) mkdirp(figures_dir) for figure_file, figure_data in resources['outputs'].items(): with open(join(figures_dir, figure_file), 'wb') as f: f.write(figure_data)
def notebook_tester(fname, kernelspec='python'): raw_nb = Exporter().from_filename(fname) raw_nb[0].metadata.setdefault('kernelspec', {})['name'] = kernelspec preproc = ExecutePreprocessor(timeout=-1) try: exec_nb = preproc.preprocess(*raw_nb) except Exception as e: return '[Failed]\n{}'.format(e) out_nb = HTMLExporter().from_notebook_node(*exec_nb) fout = fname.replace('.ipynb', '.html') with io.open(fout, 'w') as f: f.write(out_nb[0]) return '[Passed]'
def run_notebook(filename): export = Exporter() execute = ExecutePreprocessor() execute.timeout = 600 export.register_preprocessor(execute, True) export.from_filename(filename)
def notebook_tester(fname, kernelspec='python'): raw_nb = Exporter().from_filename(fname) raw_nb[0].metadata.setdefault('kernelspec', {})['name'] = kernelspec preproc = ExecutePreprocessor(timeout=-1) preproc.preprocess(*raw_nb)
def load_notebook(fname): notebook, resources = Exporter().from_filename(fname) return notebook, resources
def __init__(self, config=None, **kwargs): self.output_extension = '.sos' self.output_mimetype = 'text/x-sos' Exporter.__init__(self, config, **kwargs)