def setUp(self): self.data_dir = tempfile.mkdtemp() self.notary = sign.NotebookNotary( db_file=':memory:', secret=b'secret', data_dir=self.data_dir, ) with self.fopen(u'test3.ipynb', u'r') as f: self.nb = read(f, as_version=4) with self.fopen(u'test3.ipynb', u'r') as f: self.nb3 = read(f, as_version=3)
def from_filename(self, filename, resources=None, **kw): """ Convert a notebook from a notebook file. Parameters ---------- filename : str Full filename of the notebook file to open and convert. """ # Pull the metadata from the filesystem. if resources is None: resources = ResourcesDict() if not 'metadata' in resources or resources['metadata'] == '': resources['metadata'] = ResourcesDict() path, basename = os.path.split(filename) notebook_name = basename[:basename.rfind('.')] resources['metadata']['name'] = notebook_name resources['metadata']['path'] = path modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename)) resources['metadata']['modified_date'] = modified_date.strftime(text.date_format) with io.open(filename, encoding='utf-8') as f: return self.from_notebook_node(nbformat.read(f, as_version=4), resources=resources, **kw)
def from_filename(self, filename, resources=None, **kw): """ Convert a notebook from a notebook file. Parameters ---------- filename : str Full filename of the notebook file to open and convert. """ # Pull the metadata from the filesystem. if resources is None: resources = ResourcesDict() if not 'metadata' in resources or resources['metadata'] == '': resources['metadata'] = ResourcesDict() path, basename = os.path.split(filename) notebook_name = basename[:basename.rfind('.')] resources['metadata']['name'] = notebook_name resources['metadata']['path'] = path modified_date = datetime.datetime.fromtimestamp( os.path.getmtime(filename)) resources['metadata']['modified_date'] = modified_date.strftime( text.date_format) with io.open(filename, encoding='utf-8') as f: return self.from_notebook_node(nbformat.read(f, as_version=4), resources=resources, **kw)
def test_export_nbnode(self): """ Can a notebook be exported by a notebook node handle? """ with open(self._get_notebook(), 'r') as f: notebook = nbformat.read(f, 4) (output, resources) = export_by_name('python', notebook) assert len(output) > 0
def test_read_write_path(self): """read() and write() take filesystem paths""" path = os.path.join(self._get_files_path(), u'test4.ipynb') nb = read(path, as_version=4) with TemporaryDirectory() as td: dest = os.path.join(td, 'echidna.ipynb') write(nb, dest) assert os.path.isfile(dest)
def test_future(self): """Test than a notebook from the future with extra keys passes validation""" with self.fopen(u'test4plus.ipynb', u'r') as f: nb = read(f, as_version=4) with self.assertRaises(ValidationError): validate(nb, version=4) self.assertEqual(isvalid(nb, version=4), False) self.assertEqual(isvalid(nb), True)
def _read_notebook(self, os_path, as_version=4): """Read a notebook from an os path.""" with self.open(os_path, 'r', encoding='utf-8') as f: try: return nbformat.read(f, as_version=as_version) except Exception as e: raise HTTPError( 400, u"Unreadable Notebook: %s %r" % (os_path, e), )
def test_write_downgrade_2(self): """dowgrade a v3 notebook to v2""" # Open a version 3 notebook. with self.fopen(u'test3.ipynb', 'r') as f: nb = read(f, as_version=3) jsons = writes(nb, version=2) nb2 = json.loads(jsons) (major, minor) = get_version(nb2) self.assertEqual(major, 2)
def from_file(self, file_stream, resources=None, **kw): """ Convert a notebook from a notebook file. Parameters ---------- file_stream : file-like object Notebook file-like object to convert. """ return self.from_notebook_node(nbformat.read(file_stream, as_version=4), resources=resources, **kw)
def test_invalid(self): """Test than an invalid notebook does not pass validation""" # this notebook has a few different errors: # - one cell is missing its source # - invalid cell type # - invalid output_type with self.fopen(u'invalid.ipynb', u'r') as f: nb = read(f, as_version=4) with self.assertRaises(ValidationError): validate(nb) self.assertEqual(isvalid(nb), False)
def test_read(self): """Can older notebooks be opened and automatically converted to the current nbformat?""" # Open a version 2 notebook. with self.fopen(u'test2.ipynb', 'r') as f: nb = read(f, as_version=current_nbformat) # Check that the notebook was upgraded to the latest version automatically. (major, minor) = get_version(nb) self.assertEqual(major, current_nbformat)
def test_empty_code_cell(self): """No empty code cells in rst""" nbname = self._get_notebook() with io.open(nbname, encoding='utf8') as f: nb = nbformat.read(f, 4) exporter = self.exporter_class() (output, resources) = exporter.from_notebook_node(nb) # add an empty code cell nb.cells.append(v4.new_code_cell(source="")) (output2, resources) = exporter.from_notebook_node(nb) # adding an empty code cell shouldn't change output self.assertEqual(output.strip(), output2.strip())
def run_notebook(self, filename, opts, resources): """Loads and runs a notebook, returning both the version prior to running it and the version after running it. """ with io.open(filename) as f: input_nb = nbformat.read(f, 4) preprocessor = self.build_preprocessor(opts) cleaned_input_nb = copy.deepcopy(input_nb) for cell in cleaned_input_nb.cells: if 'execution_count' in cell: del cell['execution_count'] cell['outputs'] = [] output_nb, _ = preprocessor(cleaned_input_nb, resources) return input_nb, output_nb
def test_empty_code_cell(self): """No empty code cells in rst""" nbname = self._get_notebook() with io.open(nbname, encoding='utf8') as f: nb = nbformat.read(f, 4) exporter = self.exporter_class() (output, resources) = exporter.from_notebook_node(nb) # add an empty code cell nb.cells.append( v4.new_code_cell(source="") ) (output2, resources) = exporter.from_notebook_node(nb) # adding an empty code cell shouldn't change output self.assertEqual(output.strip(), output2.strip())
def test_nb4(self): """Test that a v4 notebook passes validation""" with self.fopen(u'test4.ipynb', u'r') as f: nb = read(f, as_version=4) validate(nb) self.assertEqual(isvalid(nb), True)
def test_nb2(self): """Test that a v2 notebook converted to current passes validation""" with self.fopen(u'test2.ipynb', u'r') as f: nb = read(f, as_version=4) validate(nb) self.assertEqual(isvalid(nb), True)