def merge_notebooks(filenames): merged = None for fname in filenames: with io.open(fname, 'r', encoding='utf-8') as f: nb = current.read(f, 'json') if merged is None: merged = nb else: merged.worksheets[0].cells.extend(nb.worksheets[0].cells) merged.metadata.name += "_merged" print current.writes(merged, 'json')
def write_notebook_object(self, nb, notebook_id=None): """Save an existing notebook object by notebook_id.""" try: new_name = nb.metadata.name except AttributeError: raise web.HTTPError(400, u"Missing notebook name") if notebook_id is None: notebook_id = self.new_notebook_id(new_name) if notebook_id not in self.mapping: raise web.HTTPError(404, u"Notebook does not exist: %s" % notebook_id) try: data = current.writes(nb, u"json") except Exception as e: raise web.HTTPError(400, u"Unexpected error while saving notebook: %s" % e) metadata = {"nbname": new_name} try: self.blob_service.put_blob(self.container, notebook_id, data, "BlockBlob", x_ms_meta_name_values=metadata) except Exception as e: raise web.HTTPError(400, u"Unexpected error while saving notebook: %s" % e) self.mapping[notebook_id] = new_name return notebook_id
def write_notebook_object(self, nb, notebook_id=None): """Save an existing notebook object by notebook_id.""" try: new_name = nb.metadata.name except AttributeError: raise web.HTTPError(400, 'Missing notebook name') if notebook_id is None: notebook_id = self.new_notebook_id(new_name) if notebook_id not in self.mapping: raise web.HTTPError(404, NB_DNEXIST_ERR.format(notebook_id)) try: data = current.writes(nb, 'json') except Exception as e: raise web.HTTPError(400, NB_SAVE_UNK_ERR.format(e)) metadata = {METADATA_NBNAME: new_name} try: obj = self.container.store_object(notebook_id, data) obj.set_metadata(metadata) except Exception as e: raise web.HTTPError(400, NB_SAVE_UNK_ERR.format(e)) self.mapping[notebook_id] = new_name return notebook_id
def model_to_files(model): """ Converts a IPython notebook model to a github.Gist `files` dict. Parameters __________ model : dict Notebook model as specified by the NotebookManager. There is an additional `__files` dict of the form {filename: file_content} Returns ------- files : dict {filename: content} Note: changed files dict values to be strings. """ files = {} name = model['name'] content = current.writes(model['content'], format=u'json') files[name] = content __files = model.get('__files', {}) for fn, content in __files.items(): files[fn] = content return files
def process(self, request, filename, db_session): # format for notebook. format = u'json' data = request.form['download_data'] try: # read notebook and format it. nb = current.reads(data.decode('utf-8'), format) except: return "Unable to save notebook. Invalid JSON data" # if notebook has a name we use it else use a generic name try: name = nb.metadata.name except: name = "mergedNotebook" nb.metadata.name = name name = normalize('NFC', nb.metadata.name) # uses ipython's current ipynb formatting. notebook_formatted = current.writes(nb, format) # make a file download response response = make_response(notebook_formatted) header = "attachment; filename=mergedNotebook.ipynb" response.headers["Content-Type"] = "text/plain" response.headers["Content-Disposition"] = header return response
def write_notebook_object(self, nb, notebook_id=None): try: new_name = nb.metadata.name except AttributeError: raise web.HTTPError(400, u'Missing notebook name') if notebook_id is None: notebook_id = self.new_notebook_id(new_name) try: data = current.writes(nb, u'json') except Exception as e: raise web.HTTPError( 400, u'Unexpected error while saving notebook: %s' % e) try: key = self.bucket.new_key(self.s3_prefix + notebook_id) key.set_metadata('nbname', new_name) key.set_contents_from_string(data) except Exception as e: raise web.HTTPError( 400, u'Unexpected error while saving notebook: %s' % e) self.mapping[notebook_id] = new_name return notebook_id
def write_notebook_object(self, nb, notebook_id=None): """Save an existing notebook object by notebook_id.""" try: new_name = nb.metadata.name except AttributeError: raise web.HTTPError(400, u'Missing notebook name') if notebook_id is None: notebook_id = self.new_notebook_id(new_name) if notebook_id not in self.mapping: raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id) try: data = current.writes(nb, u'json') except Exception as e: raise web.HTTPError( 400, u'Unexpected error while saving notebook: %s' % e) metadata = {'nbname': new_name} try: self.blob_service.put_blob(self.container, notebook_id, data, 'BlockBlob', x_ms_meta_name_values=metadata) except Exception as e: raise web.HTTPError( 400, u'Unexpected error while saving notebook: %s' % e) self.mapping[notebook_id] = new_name return notebook_id
def write_notebook_object(self, nb, notebook_id=None): """Save an existing notebook object by notebook_id.""" try: new_name = nb.metadata.name except AttributeError: raise web.HTTPError(400, u'Missing notebook name') if notebook_id is None: notebook_id = self.new_notebook_id(new_name) if notebook_id not in self.mapping: raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id) try: data = current.writes(nb, u'json') except Exception as e: raise web.HTTPError(400, u'Unexpected error while saving notebook: %s' % e) try: bucket = self.s3_conn.get_bucket(self.__bucket_name) key = Key(bucket) key.key = notebook_id key.set_metadata(self.meta_nbname, new_name) #self.log.info("Setting contents to: %s" % (data,)) key.set_contents_from_string(data) except Exception as e: raise web.HTTPError(400, u'Unexpected error while saving notebook: %s' % e) self.mapping[notebook_id] = new_name return notebook_id
def write_notebook_object(self, nb, notebook_id=None): """Save an existing notebook object by notebook_id.""" try: new_name = nb.metadata.name except AttributeError: raise web.HTTPError(400, "Missing notebook name") if notebook_id is None: notebook_id = self.new_notebook_id(new_name) if notebook_id not in self.mapping: raise web.HTTPError(404, NB_DNEXIST_ERR.format(notebook_id)) try: data = current.writes(nb, "json") except Exception as e: raise web.HTTPError(400, NB_SAVE_UNK_ERR.format(e)) metadata = {METADATA_NBNAME: new_name} try: obj = self.container.store_object(notebook_id, data) obj.set_metadata(metadata) except Exception as e: raise web.HTTPError(400, NB_SAVE_UNK_ERR.format(e)) self.mapping[notebook_id] = new_name return notebook_id
def write_notebook_object(self, nb, notebook_id=None): """Save an existing notebook object by notebook_id.""" try: new_name = nb.metadata.name except AttributeError: raise web.HTTPError(400, u'Missing notebook name') try: if notebook_id is None: notebook_id = self.new_notebook_id(new_name) nb.metadata.created = datetime.datetime.now().isoformat() nb.metadata.user = self.shock_user if self.shock_user else 'public' nb.metadata.type = 'ipynb' nb.metadata.uuid = notebook_id except Exception as e: raise web.HTTPError(400, u'Unexpected error setting notebook attributes: %s' %e) if notebook_id not in self.mapping: raise web.HTTPError(404, u'Notebook does not exist: %s' %notebook_id) try: data = current.writes(nb, u'json') attr = json.dumps(nb.metadata) shock_node = self.postShock(self.shock_url+'/node', new_name, data, attr) except Exception as e: raise web.HTTPError(400, u'Unexpected error while saving notebook: %s' %e) self.mapping[notebook_id] = new_name self.shock_map[notebook_id] = shock_node return notebook_id
def merge_notebooks(filenames): merged = None for fname in filenames: with io.open(fname, 'r', encoding='utf-8') as f: fmt = 'json' if not fname.endswith('.py') else 'py' nb = current.read(f, fmt) if merged is None: merged = nb else: # TODO: add an optional marker between joined notebooks # like an horizontal rule, for example, or some other arbitrary # (user specified) markdown cell) merged.worksheets[0].cells.extend(nb.worksheets[0].cells) if not hasattr(merged.metadata, 'name'): merged.metadata.name = '' merged.metadata.name += "_merged" print current.writes(merged, 'json')
def from_notebook_node(self, nb, resources=None, **kw): nb_copy, resources = super(NotebookExporter, self).from_notebook_node(nb, resources, **kw) if self.nbformat_version != nbformat.current_nbformat: resources['output_suffix'] = '.v%i' % self.nbformat_version else: resources['output_suffix'] = '.nbconvert' output = nbformat.writes(nb_copy, version=self.nbformat_version) return output, resources
def new_notebook_files(name='default.ipynb'): # make a new file ugh metadata = current.new_metadata(name=name) nb = current.new_notebook(metadata=metadata) content = current.writes(nb, format=u'json') file = github.InputFileContent(content) files = {name: file} return files
def save_notebook(self, model, name='', path=''): """Save the notebook model and return the model with no content.""" path = path.strip('/') self.log.debug('File manager: saving notebook %s, %s' % (name, path)) if 'content' not in model: raise web.HTTPError(400, u'No notebook JSON data provided') # One checkpoint should always exist if self.notebook_exists(name, path) and not self.list_checkpoints(name, path): self.create_checkpoint(name, path) new_path = model.get('path', path) new_name = model.get('name', name) if path != new_path or name != new_name: self.log.debug('renaming notebook %s %s->%s %s' (path, name, new_path, new_name)) self.rename_notebook(name, path, new_name, new_path) # Save the notebook file self.log.debug('getting json content') os_path = self._get_os_path(new_name, new_path) nb = current.to_notebook_json(model['content']) self.check_and_sign(nb, new_name, new_path) self.log.debug("checked and signed") if 'name' in nb['metadata']: nb['metadata']['name'] = u'' try: self.log.debug("Autosaving notebook %s", os_path) new_key_from_string(self.bucket, os_path, current.writes(nb, u'json')) except Exception as e: self.log.debug(e) raise web.HTTPError(400, u'Unexpected error while autosaving notebook: %s %s' % (os_path, e)) # Save .py script as well if self.save_script: py_path = os.path.splitext(os_path)[0] + '.py' self.log.debug("Writing script %s", py_path) try: new_key_from_string(self.bucket, py_path, current.writes(nb, u'py')) except Exception as e: self.log.error(e) raise web.HTTPError(400, u'Unexpected error while saving notebook as script: %s %s' % (py_path, e)) return self.get_notebook(new_name, new_path, content=False)
def from_notebook_node(self, nb, resources=None, **kw): nb_copy, resources = super(NotebookExporter, self).from_notebook_node( nb, resources, **kw) if self.nbformat_version != nbformat.current_nbformat: resources['output_suffix'] = '.v%i' % self.nbformat_version else: resources['output_suffix'] = '.nbconvert' output = nbformat.writes(nb_copy, version=self.nbformat_version) return output, resources
def save (self, keep_alt=False): if keep_alt: # xxx store in alt filename outfilename = "{}.alt.ipynb".format(self.name) else: outfilename = self.filename new_contents = current_notebook.writes (self.notebook,'ipynb') if replace_file_with_string (outfilename, new_contents): print("{} saved into {}".format(self.name, outfilename))
def get_notebook(self, notebook_id, format=u'json'): """Get the representation of a notebook in format by notebook_id.""" format = unicode(format) if format not in self.allowed_formats: raise web.HTTPError(415, u'Invalid notebook format: %s' % format) last_modified, nb = self.get_notebook_object(notebook_id) data = current.writes(nb, format) name = nb.get('name','notebook') return last_modified, name, data
def get_notebook(self, notebook_id, format=u'json'): """Get the representation of a notebook in format by notebook_id.""" format = unicode(format) if format not in self.allowed_formats: raise web.HTTPError(415, u'Invalid notebook format: %s' % format) last_modified, nb = self.get_notebook_object(notebook_id) data = current.writes(nb, format) name = nb.get('name', 'notebook') return last_modified, name, data
def save_notebook_object(self, nb, path): gist = self._get_gist_by_path(path) gfile = self.get_gist_file(gist) content = current.writes(nb, format=u'json') file = github.InputFileContent(content) files = {gfile.filename: file} # name and desc are synched for gist-notebooks desc = change_gist_name(gist, nb.metadata.name) self.edit_gist(gist, files=files, desc=desc)
def _clear_notebook(self, node): # pragma: no cover if NotebookExporter is not None: exporter = NotebookExporter() exporter.register_preprocessor(ClearOutputPreprocessor(enabled=True)) cleared,_ = exporter.from_notebook_node(node) else: stripped_node = v3_strip_output(node) cleared = current.writes(stripped_node, 'ipynb') return cleared
def new_notebook(self): """Create a new notebook and return its notebook_id.""" n = Notebook() n.id = str(uuid.uuid4()) n.name = 'New Notebook' metadata = current.new_metadata(name=n.name) nb = current.new_notebook(metadata=metadata) data = current.writes(nb, u'json') n.content = data n.save(force_insert=True) return n.id
def notebook_content(self, content): if isinstance(content, compat.string_types): self._notebook_content = content return try: # maybe this is a notebook content = current.writes(content, format=u'json') self._notebook_content = content except: raise
def get_notebook(self, notebook_id, format=u'json'): """Get the representation of a notebook in format by notebook_id.""" format = unicode(format) if format not in self.allowed_formats: raise web.HTTPError(415, u'Invalid notebook format: %s' % format) last_modified, nb = self.get_notebook_object(notebook_id) kwargs = {} if format == 'json': # don't split lines for sending over the wire, because it # should match the Python in-memory format. kwargs['split_lines'] = False data = current.writes(nb, format, **kwargs) name = nb.get('name','notebook') return last_modified, name, data
def get_notebook(self, notebook_id, format=u'json'): """Get the representation of a notebook in format by notebook_id.""" format = unicode(format) if format not in self.allowed_formats: raise web.HTTPError(415, u'Invalid notebook format: %s' % format) last_modified, nb = self.read_notebook_object(notebook_id) kwargs = {} if format == 'json': # don't split lines for sending over the wire, because it # should match the Python in-memory format. kwargs['split_lines'] = False data = current.writes(nb, format, **kwargs) name = nb.metadata.get('name', 'notebook') return last_modified, name, data
def save_notebook(self, model, name='', path=''): """Save the notebook model and return the model with no content.""" path = path.strip('/') if 'content' not in model: raise web.HTTPError(400, u'No notebook JSON data provided') if not path: raise web.HTTPError(400, u'We require path for saving.') gist = self._get_gist(name, path) if gist is None: tags = parse_tags(name) if path: tags.append(path) content = current.writes(model['content'], format=u'json') gist = self.gisthub.create_gist(name, tags, content) # One checkpoint should always exist #if self.notebook_exists(name, path) and not self.list_checkpoints(name, path): # self.create_checkpoint(name, path) new_path = model.get('path', path).strip('/') new_name = model.get('name', name) if path != new_path: raise web.HTTPError(400, u'Gist backend does not support path change') # Save the notebook file nb = current.to_notebook_json(model['content']) # remove [gist_id] if we're being sent old key_name gist.name = gist.strip_gist_id(new_name) gist.notebook_content = nb self.check_and_sign(nb, new_path, new_name) if 'name' in nb['metadata']: nb['metadata']['name'] = u'' try: self.log.debug("Autosaving notebook %s %s", path, name) self.gisthub.save(gist) except Exception as e: raise web.HTTPError(400, u'Unexpected error while autosaving notebook: %s %s %s' % (path, name, e)) # NOTE: since gist.name might not have [gist_id] suffix on rename # we use gist.key_name model = self.get_notebook(gist.key_name, new_path, content=False) return model
def test_run_nb(self): """Test %run notebook.ipynb""" from IPython.nbformat import current nb = current.new_notebook(worksheets=[ current.new_worksheet(cells=[ current.new_text_cell("The Ultimate Question of Everything"), current.new_code_cell("answer=42") ]) ]) src = current.writes(nb, 'json') self.mktmp(src, ext='.ipynb') _ip.magic("run %s" % self.fname) nt.assert_equal(_ip.user_ns['answer'], 42)
def get_notebook(self, notebook_id, format=u'json'): """Get the representation of a notebook in format by notebook_id.""" if format != 'json': raise Exception('Only supporting JSON in Django backed notebook') n = Notebook.objects.get(id=notebook_id) # we want more informative names for archived notebooks nb = current.reads(n.content, format) nb.metadata.name = DjangoNotebookManager._name(n) kwargs = {} if format == 'json': # don't split lines for sending over the wire, because it should match # the Python in-memory format. kwargs['split_lines'] = False n.content = current.writes(nb, format, **kwargs) return n.updated_on, DjangoNotebookManager._name(n), n.content
def test_run_nb(self): """Test %run notebook.ipynb""" from IPython.nbformat import current nb = current.new_notebook( worksheets=[ current.new_worksheet(cells=[ current.new_code_cell("answer=42") ]) ] ) src = current.writes(nb, 'json') self.mktmp(src, ext='.ipynb') _ip.magic("run %s" % self.fname) nt.assert_equal(_ip.user_ns['answer'], 42)
def main(): parser = ArgumentParser(description=DESCRIP, epilog=EPILOG, formatter_class=RawDescriptionHelpFormatter) parser.add_argument('filename1', type=str, help='notebook filename') parser.add_argument('filename2', type=str, help='notebook filename') args = parser.parse_args() nb_strs = [] for fname in (args.filename1, args.filename2): with io.open(fname, 'r') as f: nb = current.read(f, 'json') remove_outputs(nb) remove_prompt_numbers(nb) nb_strs.append(current.writes(nb, 'json').split('\n')) diff_gen = difflib.unified_diff(nb_strs[0], nb_strs[1]) print('\n'.join(diff_gen))
def explode(nb, quiet=False, stdout=False): """ Explode a parametrized notebook Parameters ---------- nb : IPython Notebook object The parameterized notebook to be exploded quiet : bool (default=False) whether to print out filenames of the exploded notebooks """ first_cell = nb.worksheets[0].cells[0] if not first_cell.input.lower().startswith("## parameters"): log.warning("no parameters found in this notebook") return params = OrderedDict() exec(first_cell.input, globals(), params) saved = params.pop('__save__', []) [params.pop(k) for k in params if isinstance(params[k], types.ModuleType)] saved_params =", ".join(["%s=%s"% (var,var) for var in saved]) last_cell = nb.worksheets[0].cells[0].copy() if saved: nb.worksheets[0].cells.append(last_cell) for i, p in enumerate(itertools.product(*params.values())): log.info("p = %s", p) basename = ipynb.rsplit('.ipynb')[0] outname = args.prefix + basename +"%04d" % i outfile = outname + ".ipynb" header = "## Parameterized by %s\n" % ipynb assignments = [] # XXX: the code below won't currently work for callables, I think for k,v in zip(params.keys(), p): assignments.append('%s = %s' % (k,repr(v))) loader = loader_template % (outfile, saved) if len(saved) else '' first_cell.input = header + "\n".join(assignments) + loader last_cell.input = footer_template % (outfile, saved_params) nb_as_json = writes(nb, 'json') with open(outfile, 'w') as f: f.write(nb_as_json) log.info('writing file...') if not quiet: print outfile
def explode(nb, quiet=False, stdout=False): """ Explode a parametrized notebook Parameters ---------- nb : IPython Notebook object The parameterized notebook to be exploded quiet : bool (default=False) whether to print out filenames of the exploded notebooks """ first_cell = nb.worksheets[0].cells[0] if not first_cell.input.lower().startswith("## parameters"): log.warning("no parameters found in this notebook") return params = OrderedDict() exec(first_cell.input, globals(), params) saved = params.pop('__save__', []) [params.pop(k) for k in params if isinstance(params[k], types.ModuleType)] saved_params = ", ".join(["%s=%s" % (var, var) for var in saved]) last_cell = nb.worksheets[0].cells[0].copy() if saved: nb.worksheets[0].cells.append(last_cell) for i, p in enumerate(itertools.product(*params.values())): log.info("p = %s", p) basename = ipynb.rsplit('.ipynb')[0] outname = args.prefix + basename + "%04d" % i outfile = outname + ".ipynb" header = "## Parameterized by %s\n" % ipynb assignments = [] # XXX: the code below won't currently work for callables, I think for k, v in zip(params.keys(), p): assignments.append('%s = %s' % (k, repr(v))) loader = loader_template % (outfile, saved) if len(saved) else '' first_cell.input = header + "\n".join(assignments) + loader last_cell.input = footer_template % (outfile, saved_params) nb_as_json = writes(nb, 'json') with open(outfile, 'w') as f: f.write(nb_as_json) log.info('writing file...') if not quiet: print outfile
def save_new_notebook(self, data, name=None, format=u'json'): """Save a new notebook and return its notebook_id.""" if format != 'json': raise Exception('Only supporting JSON in Django backed notebook') n = Notebook() n.id = str(uuid.uuid4()) if name != None: n.name = name nb = current.reads(data.decode('utf-8'), format) nb.metadata.name = name data = current.writes(nb, format) else: nb = current.reads(data.decode('utf-8'), format) n.name = nb.metadata.name n.content = data n.save(force_insert=True) self._archive(n) return n.id
def merge_notebooks(filenames): merged = None added_appendix = False for fname in filenames: with io.open(fname, 'r', encoding='utf-8') as f: nb = current.read(f, u'json') remove_formatting(nb) if not added_appendix and fname[0:8] == 'Appendix': remove_links_add_appendix(nb) added_appendix = True else: remove_links(nb) if merged is None: merged = nb else: merged.worksheets[0].cells.extend(nb.worksheets[0].cells) merged.metadata.name += "_merged" print(current.writes(merged, u'json'))
def save_notebook(self, notebook_id, data, name=None, format=u'json'): """Save an existing notebook by notebook_id.""" if format != 'json': raise Exception('Only supporting JSON in Django backed notebook') n = Notebook.objects.get(id=notebook_id) if n.archive == True: raise Exception('Cannot update archived copy') # update copy if name != None: n.name = name nb = current.reads(data.decode('utf-8'), format) nb.metadata.name = name data = current.writes(nb, format) else: nb = current.reads(data.decode('utf-8'), format) n.name = nb.metadata.name n.content = data n.save() self._archive(n) return n.id
def write_notebook_object(self, nb, notebook_id=None): try: new_name = nb.metadata.name except AttributeError: raise web.HTTPError(400, u'Missing notebook name') if notebook_id is None: notebook_id = self.new_notebook_id(new_name) try: data = current.writes(nb, u'json') except Exception as e: raise web.HTTPError(400, u'Unexpected error while saving notebook: %s' % e) try: key = self.bucket.new_key(self.s3_prefix + notebook_id) key.set_metadata('nbname', new_name) key.set_contents_from_string(data) except Exception as e: raise web.HTTPError(400, u'Unexpected error while saving notebook: %s' % e) self.mapping[notebook_id] = new_name return notebook_id
def translate(self): self.visitor = visitor = self.translator_class(self.document) self.document.walkabout(visitor) for attr in self.visitor_attributes: setattr(self, attr, getattr(visitor, attr)) self.output = '{0}'.format(nbformat.writes(visitor.nb, 'ipynb'))
def execute_and_save(ipynb): with open(ipynb) as f: nb = reads(f.read(), "json") executed_nb = execute_notebook(nb) with open(ipynb.replace(".ipynb", "_executed.ipynb"), "w") as f: f.write(writes(executed_nb, "json"))
import sys import io from IPython.nbformat import current def remove_outputs(nb): for ws in nb.worksheets: for cell in ws.cells: if cell.cell_type == 'code': cell.outputs = [] if __name__ == '__main__': fname = sys.argv[1] with io.open(fname, 'r') as f: nb = current.read(f, 'json') remove_outputs(nb) print(current.writes(nb, 'json'))
""" usage: python remove_output.py notebook.ipynb [ > without_output.ipynb ] Author : Min RK """ import sys import io from IPython.nbformat import current def remove_outputs(nb): """remove the outputs from a notebook""" for ws in nb.worksheets: for cell in ws.cells: if cell.cell_type == 'code': cell.outputs = [] if __name__ == '__main__': fname = sys.argv[1] with io.open(fname, 'r') as f: nb = current.read(f, 'json') remove_outputs(nb) print current.writes(nb, 'json')
Usage: `stripnbprompts.py foo.ipynb > stripped.ipynb` """ import os import sys import time from IPython.nbformat.current import reads, writes def strip_prompts(nb): """reorder the cells in this notebook order is based on the cell execution order non-code cells are associated with the first subsequent code cell as a chunk of cells """ for cell in nb.worksheets[0].cells: if 'prompt_number' in cell: print >> sys.stderr, "stripping In [%s]" % cell.prompt_number cell.prompt_number = None if __name__ == '__main__': for ipynb in sys.argv[1:]: print >> sys.stderr, "stripping prompts from %s" % ipynb with open(ipynb) as f: nb = reads(f.read(), 'json') strip_prompts(nb) print writes(nb, 'json')
""" usage: python remove_output.py notebook.ipynb [ > without_output.ipynb ] """ import sys import io from IPython.nbformat import current def remove_outputs(nb): """remove the outputs from a notebook""" for ws in nb.worksheets: for cell in ws.cells: if cell.cell_type == 'code': cell.outputs = [] if __name__ == '__main__': fname = sys.argv[1] with io.open(fname, 'r') as f: nb = current.read(f, 'json') remove_outputs(nb) print current.writes(nb, 'json')
def astext(self): return '{0}'.format(nbformat.writes(self.nb, 'ipynb'))
def execute_and_save(ipynb): with open(ipynb) as f: nb = reads(f.read(), 'json') executed_nb = execute_notebook(nb) with open(ipynb.replace('.ipynb', '_executed.ipynb'), 'w') as f: f.write(writes(executed_nb, 'json'))
from IPython.nbformat import current from copy import deepcopy as copy with open('Widgets.ipynb') as f: nb = current.reads(f.read()) splits = [[]] cells = nb['worksheets'][0].cells for i, cell in enumerate(cells): if 'source' in cell and cell['source'].strip().lower() == 'split here': splits.append([]) else: splits[-1].append(cell) for i, split in enumerate(splits): new_nb = copy(nb) new_nb['worksheets'][0]['cells'] = split with open('Widgets%d.ipynb' % (i+1), 'w') as f: f.write(current.writes(new_nb))