def clear_notebook(path, kwargs={ "SCOPETYPE": "OPENADC", "PLATFORM": "CWLITEARM", "VERSION": "HARDWARE" }): real_path = Path(path) body = "" with open(real_path, "r", encoding="utf-8") as nbfile: nb = nbformat.read(nbfile, as_version=4) # special case: if the top block isn't a parameter block, nbparameterise will: # * error out if invalid python syntax # * replace code with a parameter block (whatever we passed in with kwargs, including an empty block) # so if no parameters being changed, don't run nbparameterise if len(kwargs) > 0: orig_parameters = extract_parameters(nb) params = parameter_values(orig_parameters, **kwargs) new_nb = replace_definitions(nb, params, execute=False) else: new_nb = nb co = ClearOutputPreprocessor() exporter = NotebookExporter() node, resources = co.preprocess(new_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 main(argv=sys.argv[1:]): try: parser = argparse.ArgumentParser(prog='jupyxplorer') parser.add_argument("-c", '--config-file', default='config.yaml', required=True, type=load_yaml, help='Config file. Defaults to config.yaml.') parser.add_argument("-o", '--output-dir', default='.output', required=False, help='Directory where output files will be saved to.') parser.add_argument("-i", '--input-dir', default='.input', required=False, help='Directory where output files will be saved to. Defaults to directory ".input"') parser.add_argument("-e", "--execute", required=False, action='store_true', help="Execute notebooks generated. Note: Requires a docker daemon running.") args = parser.parse_args(argv) data = args.config_file output_dir = os.path.abspath(args.output_dir or ".output") input_dir = os.path.abspath(args.input_dir or ".input") if not os.path.exists(output_dir): os.makedirs(output_dir) for field in data["fields"]: c = Config() c.Formatter.params = dict( _field=field["name"], _dataset_type=data['dataset']['type'], _dataset_source=data['dataset']['source'], ) c.InstallRequirements.requirements = data.get("requirements", []) c.NotebookExporter.preprocessors = [Formatter, InstallRequirements] exporter = NotebookExporter(config=c) notebook = nbformat.read(os.path.join(BASE_DIR, 'notebooks/{}.ipynb'.format(field["type"])), as_version=4) with open('{}/exploration_{}_{}.ipynb'.format(output_dir, field["type"], field["name"]), 'w') as nbfile: nbfile.write(exporter.from_notebook_node(notebook)[0]) # Running a jupyter container for executing new generated notebooks if args.execute: execute(input_dir, output_dir) except KeyboardInterrupt: logger.warning("... jupyxplorer command was interrupted") sys.exit(2) except Exception as ex: logger.error('Unexpected error: %s' % ex) sys.exit(1) sys.exit(0)
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 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) orig_parameters = extract_parameters(nb) params = parameter_values(orig_parameters, SCOPETYPE="OPENADC", PLATFORM="CWLITEARM") new_nb = replace_definitions(nb, params, execute=False) co = ClearOutputPreprocessor() exporter = NotebookExporter() node, resources = co.preprocess(new_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 main(argv=sys.argv[1:]): try: parser = argparse.ArgumentParser(prog='jupyxplorer') parser.add_argument("-c", '--config-file', default='config.yaml', required=True, type=load_yaml, help='Config file. Defaults to config.yaml') parser.add_argument( "-o", '--output-dir', default='.output', required=False, help='Directory where output files will be saved to.') args = parser.parse_args(argv) data = args.config_file output_dir = args.output_dir or ".output" if not os.path.exists(output_dir): os.makedirs(output_dir) for field in data["fields"]: c = Config() c.FillName.field = field["name"] c.NotebookExporter.preprocessors = [FillName] c.FillName.enabled = True exporter = NotebookExporter(config=c) notebook = nbformat.read(os.path.join( BASE_DIR, 'notebooks/{}.ipynb'.format(field["type"])), as_version=4) with open( '{}/exploration_{}.ipynb'.format(output_dir, field["type"]), 'w') as nbfile: nbfile.write(exporter.from_notebook_node(notebook)[0]) except KeyboardInterrupt: logger.warning("... jupyxplorer command was interrupted") sys.exit(2) except Exception as ex: logger.error('Unexpected error: %s' % ex) sys.exit(1) sys.exit(0)
def nbgrader_generate(notebook_path, config=None): # Read in the notebook notebook = nbformat.read(notebook_path, as_version=4) # Set up the config if not config: c = Config() else: c = config c.NotebookExporter.preprocessors = [ ClearSolutions, LockCells, ComputeChecksums, CheckCellMetadata, ClearOutput, ClearHiddenTests, ClearMarkScheme ] # Convert the notebook exporter = NotebookExporter(config=c) nb, resources = exporter.from_notebook_node(notebook) fw = FilesWriter(config=c) fw.write(nb, resources, notebook_name='index')
def _strip_tagged_cells(self, tmpdir, entry, tags, remove_name: str): """Strip specially tagged cells from a notebook. Copy notebook to a temporary location, and generate a stripped version there. No files are modified in the original directory. Args: tmpdir: directory to copy notebook into entry: original notebook tags: List of tags (strings) to strip remove_name: Remove this component from the name Returns: stripped-entry, original-entry - both in the temporary directory """ _log.debug(f"run notebook in temporary directory: {tmpdir}") # Copy notebook to temporary directory tmp_entry = tmpdir / entry.name shutil.copy(entry, tmp_entry) # Remove the given tags # Configure tag removal tag_names = [self._cell_tags[t] for t in tags] self._nb_remove_config.TagRemovePreprocessor.remove_cell_tags = tag_names _log.debug( f"removing tag(s) <{', '.join(tag_names)}'> from notebook: {entry.name}" ) (body, resources) = NotebookExporter( config=self._nb_remove_config).from_filename(str(tmp_entry)) # Determine output notebook name: # remove suffixes, either "Capitalized" or "lowercase" version nmc, nml = remove_name.capitalize(), remove_name.lower() nb_name = re.sub(f'(_{nmc}|_{nml}|_{nmc}_|_{nml}_|{nmc}_|{nml}_)', '', entry.stem) # Create the new notebook wrt = nbconvert.writers.FilesWriter() wrt.build_directory = str(tmpdir) _log.debug(f"writing stripped notebook: {nb_name}") wrt.write(body, resources, notebook_name=nb_name) # Return both notebook names, and temporary directory (for cleanup) stripped_entry = tmpdir / f"{nb_name}.ipynb" return stripped_entry
import argparse import os from traitlets.config import Config from nbconvert.exporters import NotebookExporter c = Config() c.TagRemovePreprocessor.remove_cell_tags = ("solution", ) c.TagRemovePreprocessor.enabled = True c.NotebookExporter.preprocessors = [ "nbconvert.preprocessors.TagRemovePreprocessor" ] parser = argparse.ArgumentParser( description='Strip cells tagged with "solution" from notebooks.') parser.add_argument('names', metavar='input_path', type=str, nargs='+', help='Paths of notebooks to convert.') parser.add_argument('--output-dir', dest='path', help='Output folder') args = parser.parse_args() for name in args.names: print(f"Stripping solution cells from {name}") output = NotebookExporter(config=c).from_filename(name) with open(os.path.join(args.path, os.path.basename(name)), "w") as f: f.write(output[0])
def strip_tags(nb: Path) -> bool: """Strip tags from notebook, if there are any. Behavior depends on filename (case-insensitive). - if the notebook file name ends with "_solution_testing", create two files: 1. chop off "_testing" from name, remove test and exercise cells 2. chop off "_solution_testing" from name & add "_exercise", remove solution and test cells - if the file ends with "_testing" only, create one file: 1. chop off "_testing" from name, remove test cells - otherwise: do nothing Returns: Was anything stripped (and new files created)? Raises: IOError: If the desired target for the stripped notebook already exists """ _log.info(f"stripping tags from notebook {nb}") nb_name = nb.stem # Figure out which tags to strip: # - check for case (1), solution_testing.ipynb if nb_name.lower().endswith("_solution_testing"): pre = nb_name[:-17] names = [f"{pre}_solution.ipynb", f"{pre}_exercise.ipynb"] tags_to_strip = [(REMOVE_CELL_TAG, EXERCISE_CELL_TAG), (REMOVE_CELL_TAG, SOLUTION_CELL_TAG)] # - check for case (2), _testing.ipynb elif nb_name.lower().endswith("_testing"): pre = nb_name[:-8] names = [f"{pre}.ipynb"] tags_to_strip = [(REMOVE_CELL_TAG, )] # - if neither, we are done here else: _log.debug(f"notebook '{nb}' does not need to have tags stripped") return False # Create all desired tag-stripped copies for name, remove_tags in zip(names, tags_to_strip): target_nb = nb.parent / name _log.info(f"creating new notebook '{target_nb}'") # Don't overwrite an existing file if target_nb.exists(): _log.warning( f"cannot create new notebook '{target_nb}': file exists") continue # Set up configuration for removing specially tagged cells conf = Config() conf.TagRemovePreprocessor.remove_cell_tags = remove_tags conf.NotebookExporter.preprocessors = [ # this requires the full module path "nbconvert.preprocessors.TagRemovePreprocessor" ] # Convert from Notebook format to Notebook format, stripping tags (body, resources) = NotebookExporter(config=conf).from_filename(str(nb)) # Set up output destination wrt = FilesWriter() wrt.build_directory = str(target_nb.parent) # Write stripped notebook to output file wrt.write(body, resources, notebook_name=target_nb.stem) return True
from glob import glob import nbformat as nbf from nbconvert.exporters import NotebookExporter from traitlets.config import Config notebooks = glob("docs/content/**/*.ipynb", recursive=True) c = Config() c.NotebookExporter.preprocessors = ["nbconvert.preprocessors.ClearOutputPreprocessor"] exporter = NotebookExporter(config=c) for path_ntbk in notebooks: print(f"Clearing output for {path_ntbk}") ntbk = nbf.read(path_ntbk, nbf.NO_CONVERT) (ntbk, _) = exporter.from_notebook_node(ntbk) ntbk = nbf.reads(ntbk, nbf.NO_CONVERT) nbf.write(ntbk, path_ntbk)