Python nbformat.v4.new_notebook() Examples
The following are 30
code examples of nbformat.v4.new_notebook().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
nbformat.v4
, or try the search function
.
Example #1
Source File: py2ipynb.py From py2ipynb with GNU General Public License v3.0 | 7 votes |
def py2ipynb(input, output, cellmark_style, other_ignores=[]): """Converts a .py file to a V.4 .ipynb notebook usiing `parsePy` function :param input: Input .py filename :param output: Output .ipynb filename :param cellmark_style: Determines cell marker based on IDE, see parsePy documentation for values :param other_ignores: Other lines to ignore """ # Create the code cells by parsing the file in input cells = [] for c in parsePy(input, cellmark_style, other_ignores): codecell, metadata, code = c cell = new_code_cell(source=code, metadata=metadata) if codecell else new_markdown_cell(source=code, metadata=metadata) cells.append(cell) # This creates a V4 Notebook with the code cells extracted above nb0 = new_notebook(cells=cells, metadata={'language': 'python',}) with codecs.open(output, encoding='utf-8', mode='w') as f: nbformat.write(nb0, f, 4)
Example #2
Source File: test_assignment_list.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def class_files(coursedir): # copy files from the user guide source_path = os.path.join(os.path.dirname(__file__), "..", "..", "docs", "source", "user_guide", "source") shutil.copytree(os.path.join(os.path.dirname(__file__), source_path), os.path.join(coursedir, "source")) # rename to old names -- we do this rather than changing all the tests # because I want the tests to operate on files with spaces in the names os.rename(os.path.join(coursedir, "source", "ps1"), os.path.join(coursedir, "source", "Problem Set 1")) os.rename(os.path.join(coursedir, "source", "Problem Set 1", "problem1.ipynb"), os.path.join(coursedir, "source", "Problem Set 1", "Problem 1.ipynb")) os.rename(os.path.join(coursedir, "source", "Problem Set 1", "problem2.ipynb"), os.path.join(coursedir, "source", "Problem Set 1", "Problem 2.ipynb")) # create a fake ps1 os.mkdir(os.path.join(coursedir, "source", "ps.01")) with io.open(os.path.join(coursedir, "source", "ps.01", "problem 1.ipynb"), mode="w", encoding="utf-8") as fh: write_nb(new_notebook(), fh, 4) with open("nbgrader_config.py", "a") as fh: fh.write(dedent( """ c.CourseDirectory.root = '{}' """.format(coursedir) )) return coursedir
Example #3
Source File: test_v2.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_regular_cells(): validator = MetadataValidatorV2() # code cell without nbgrader metadata cell = create_code_cell() validator.validate_cell(cell) validator.upgrade_cell_metadata(cell) # code cell with metadata, but not an nbgrader cell cell = create_regular_cell("", "code", schema_version=2) del cell.metadata.nbgrader["task"] validator.validate_cell(cell) nb = new_notebook() cell1 = create_code_cell() cell2 = create_regular_cell("", "code", schema_version=2) del cell2.metadata.nbgrader["task"] nb.cells = [cell1, cell2] validator.validate_nb(nb)
Example #4
Source File: latex.py From bookbook with MIT License | 6 votes |
def combine_notebooks(notebook_files: Sequence[Path]) -> NotebookNode: combined_nb = new_notebook() count = 0 for filename in notebook_files: count += 1 log.debug('Adding notebook: %s', filename) nbname = filename.stem nb = nbformat.read(str(filename), as_version=4) try: combined_nb.cells.extend(add_sec_label(nb.cells[0], nbname)) except NoHeader: raise NoHeader("Failed to find header in " + filename) combined_nb.cells.extend(nb.cells[1:]) if not combined_nb.metadata: combined_nb.metadata = nb.metadata.copy() log.info('Combined %d files' % count) return combined_nb
Example #5
Source File: driver.py From Xpedite with Apache License 2.0 | 6 votes |
def buildNotebook(appName, result, notebookPath, dataFilePath, runId): """ Method to build .ipynb notebook with init code cell for profiles and one report cell per category. """ begin = time.time() LOGGER.info('generating notebook %s -> ', os.path.basename(notebookPath)) nb = nbf.new_notebook() numOfCategories, d3Flots = buildReportCells(nb, result, dataFilePath) buildInitCell(nb, numOfCategories, d3Flots, appName, runId) try: with open(notebookPath, 'w') as reportFile: nbformat.write(nb, reportFile) notebookSize = formatHumanReadable(os.path.getsize(notebookPath)) elapsed = time.time() - begin LOGGER.completed('completed %s in %0.2f sec.', notebookSize, elapsed) return True except IOError: LOGGER.exception('Could not write to the notebook(.ipynb) file') return False
Example #6
Source File: test_v1.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_regular_cells(): validator = MetadataValidatorV1() # code cell without nbgrader metadata cell = create_code_cell() validator.validate_cell(cell) validator.upgrade_cell_metadata(cell) # code cell with metadata, but not an nbgrader cell cell = create_regular_cell("", "code", schema_version=1) del cell.metadata.nbgrader["task"] validator.validate_cell(cell) nb = new_notebook() cell1 = create_code_cell() cell2 = create_regular_cell("", "code", schema_version=1) del cell2.metadata.nbgrader["task"] nb.cells = [cell1, cell2] validator.validate_nb(nb)
Example #7
Source File: test_v3.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_regular_cells(): validator = MetadataValidatorV3() # code cell without nbgrader metadata cell = create_code_cell() validator.validate_cell(cell) validator.upgrade_cell_metadata(cell) # code cell with metadata, but not an nbgrader cell cell = create_regular_cell("", "code", schema_version=3) validator.validate_cell(cell) nb = new_notebook() cell1 = create_code_cell() cell2 = create_regular_cell("", "code", schema_version=3) nb.cells = [cell1, cell2] validator.validate_nb(nb)
Example #8
Source File: test_saveautogrades.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_grade_correct_code(self, preprocessors, gradebook, resources): """Is a passing code cell correctly graded?""" cell = create_grade_cell("hello", "code", "foo", 1) cell.metadata.nbgrader['checksum'] = compute_checksum(cell) nb = new_notebook() nb.cells.append(cell) preprocessors[0].preprocess(nb, resources) gradebook.add_submission("ps0", "bar") preprocessors[1].preprocess(nb, resources) grade_cell = gradebook.find_grade("foo", "test", "ps0", "bar") assert grade_cell.score == 1 assert grade_cell.max_score == 1 assert grade_cell.auto_score == 1 assert grade_cell.manual_score == None assert not grade_cell.needs_manual_grade
Example #9
Source File: test_saveautogrades.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_grade_incorrect_code(self, preprocessors, gradebook, resources): """Is a failing code cell correctly graded?""" cell = create_grade_cell("hello", "code", "foo", 1) cell.metadata.nbgrader['checksum'] = compute_checksum(cell) cell.outputs = [new_output('error', ename="NotImplementedError", evalue="", traceback=["error"])] nb = new_notebook() nb.cells.append(cell) preprocessors[0].preprocess(nb, resources) gradebook.add_submission("ps0", "bar") preprocessors[1].preprocess(nb, resources) grade_cell = gradebook.find_grade("foo", "test", "ps0", "bar") assert grade_cell.score == 0 assert grade_cell.max_score == 1 assert grade_cell.auto_score == 0 assert grade_cell.manual_score == None assert not grade_cell.needs_manual_grade
Example #10
Source File: test_saveautogrades.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_grade_unchanged_markdown(self, preprocessors, gradebook, resources): """Is an unchanged markdown cell correctly graded?""" cell = create_grade_and_solution_cell("hello", "markdown", "foo", 1) cell.metadata.nbgrader['checksum'] = compute_checksum(cell) nb = new_notebook() nb.cells.append(cell) preprocessors[0].preprocess(nb, resources) gradebook.add_submission("ps0", "bar") preprocessors[1].preprocess(nb, resources) grade_cell = gradebook.find_grade("foo", "test", "ps0", "bar") assert grade_cell.score == 0 assert grade_cell.max_score == 1 assert grade_cell.auto_score == 0 assert grade_cell.manual_score == None assert not grade_cell.needs_manual_grade
Example #11
Source File: test_saveautogrades.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_grade_changed_markdown(self, preprocessors, gradebook, resources): """Is a changed markdown cell correctly graded?""" cell = create_grade_and_solution_cell("hello", "markdown", "foo", 1) cell.metadata.nbgrader['checksum'] = compute_checksum(cell) nb = new_notebook() nb.cells.append(cell) preprocessors[0].preprocess(nb, resources) gradebook.add_submission("ps0", "bar") cell.source = "hello!" preprocessors[1].preprocess(nb, resources) grade_cell = gradebook.find_grade("foo", "test", "ps0", "bar") assert grade_cell.score == 0 assert grade_cell.max_score == 1 assert grade_cell.auto_score == None assert grade_cell.manual_score == None assert grade_cell.needs_manual_grade
Example #12
Source File: test_saveautogrades.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_grade_existing_auto_comment(self, preprocessors, gradebook, resources): """Is a failing code cell correctly graded?""" cell = create_grade_and_solution_cell("hello", "markdown", "foo", 1) cell.metadata.nbgrader['checksum'] = compute_checksum(cell) nb = new_notebook() nb.cells.append(cell) preprocessors[0].preprocess(nb, resources) gradebook.add_submission("ps0", "bar") preprocessors[1].preprocess(nb, resources) comment = gradebook.find_comment("foo", "test", "ps0", "bar") assert comment.auto_comment == "No response." nb.cells[-1].source = 'goodbye' preprocessors[1].preprocess(nb, resources) gradebook.db.refresh(comment) assert comment.auto_comment is None
Example #13
Source File: test_checkcellmetadata.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_invalid_grade_cell_id(self, preprocessor): """Check that an error is raised when the grade cell id is invalid""" resources = dict(grade_ids=[]) nb = new_notebook() nb.cells = [create_grade_cell("", "code", "", 1)] with pytest.raises(ValidationError): preprocessor.preprocess(nb, resources) nb.cells = [create_grade_cell("", "code", "a b", 1)] with pytest.raises(ValidationError): preprocessor.preprocess(nb, resources) nb.cells = [create_grade_cell("", "code", "a\"b", 1)] with pytest.raises(ValidationError): preprocessor.preprocess(nb, resources) nb.cells = [create_solution_cell("", "code", "abc-ABC_0")] preprocessor.preprocess(nb, resources)
Example #14
Source File: cli.py From tex2ipy with BSD 2-Clause "Simplified" License | 6 votes |
def tex2ipy(code, cls=Tex2Cells): t2c = cls(code) cells = t2c.parse() livereveal = dict( transition='none', scroll=True, controls=True, slideNumber=True, help=True ) md = dict( language='python', celltoolbar='Slideshow', livereveal=livereveal ) nb = new_notebook( metadata=from_dict(md), cells=from_dict(cells) ) return nb
Example #15
Source File: test_savecells.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_save_code_grade_cell(self, preprocessor, gradebook, resources): cell = create_grade_cell("hello", "code", "foo", 1) cell.metadata.nbgrader['checksum'] = compute_checksum(cell) nb = new_notebook() nb.cells.append(cell) nb, resources = preprocessor.preprocess(nb, resources) grade_cell = gradebook.find_grade_cell("foo", "test", "ps0") assert grade_cell.max_score == 1 assert grade_cell.cell_type == "code" source_cell = gradebook.find_source_cell("foo", "test", "ps0") assert source_cell.source == "hello" assert source_cell.checksum == cell.metadata.nbgrader["checksum"] assert source_cell.cell_type == "code" assert source_cell.locked
Example #16
Source File: base.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _empty_notebook(self, path, kernel=None): nb = new_notebook() if kernel is not None: nb.metadata.kernelspec = { "display_name": "kernel", "language": kernel, "name": kernel } full_dest = os.path.abspath(path) if not os.path.exists(os.path.dirname(full_dest)): os.makedirs(os.path.dirname(full_dest)) if os.path.exists(full_dest): remove(full_dest) with io.open(full_dest, mode='w', encoding='utf-8') as f: write_nb(nb, f, 4)
Example #17
Source File: test_getgrades.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_save_changed_markdown(self, preprocessors, gradebook, resources): """Is a changed markdown cell correctly graded?""" cell = create_grade_and_solution_cell("hello", "markdown", "foo", 1) cell.metadata.nbgrader['checksum'] = compute_checksum(cell) nb = new_notebook() nb.cells.append(cell) preprocessors[0].preprocess(nb, resources) gradebook.add_submission("ps0", "bar") cell.source = "hello!" preprocessors[1].preprocess(nb, resources) preprocessors[2].preprocess(nb, resources) assert cell.metadata.nbgrader['score'] == 0 assert cell.metadata.nbgrader['points'] == 1 assert cell.metadata.nbgrader['comment'] is None
Example #18
Source File: test_overwritecells.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_nonexistant_grade_id(self, preprocessors, resources): """Are cells not in the database ignored?""" cell = create_grade_cell("", "code", "", 1) cell.metadata.nbgrader['grade'] = False nb = new_notebook() nb.cells.append(cell) nb, resources = preprocessors[0].preprocess(nb, resources) nb, resources = preprocessors[1].preprocess(nb, resources) assert 'grade_id' not in cell.metadata.nbgrader cell = create_grade_cell("", "code", "foo", 1) cell.metadata.nbgrader['grade'] = False nb = new_notebook() nb.cells.append(cell) nb, resources = preprocessors[0].preprocess(nb, resources) nb, resources = preprocessors[1].preprocess(nb, resources) assert 'grade_id' not in cell.metadata.nbgrader
Example #19
Source File: test_savecells.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_modify_cell(self, preprocessor, gradebook, resources): nb = new_notebook() nb.cells.append(create_grade_and_solution_cell("hello", "markdown", "foo", 2)) nb, resources = preprocessor.preprocess(nb, resources) notebook = gradebook.find_notebook("test", "ps0") grade_cell = gradebook.find_grade_cell("foo", "test", "ps0") solution_cell = gradebook.find_solution_cell("foo", "test", "ps0") source_cell = gradebook.find_source_cell("foo", "test", "ps0") assert grade_cell.max_score == 2 assert source_cell.source == "hello" nb.cells[-1] = create_grade_and_solution_cell("goodbye", "markdown", "foo", 1) nb, resources = preprocessor.preprocess(nb, resources) gradebook.db.refresh(notebook) gradebook.db.refresh(grade_cell) gradebook.db.refresh(solution_cell) gradebook.db.refresh(source_cell) assert grade_cell.max_score == 1 assert source_cell.source == "goodbye"
Example #20
Source File: test_savecells.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_remove_cell(self, preprocessor, gradebook, resources): cell1 = create_grade_and_solution_cell("hello", "markdown", "foo", 2) cell2 = create_grade_and_solution_cell("hello", "markdown", "bar", 1) nb = new_notebook() nb.cells.append(cell1) nb.cells.append(cell2) nb, resources = preprocessor.preprocess(nb, resources) notebook = gradebook.find_notebook("test", "ps0") assert len(notebook.grade_cells) == 2 assert len(notebook.solution_cells) == 2 assert len(notebook.source_cells) == 2 nb.cells = nb.cells[:-1] nb, resources = preprocessor.preprocess(nb, resources) gradebook.db.refresh(notebook) assert len(notebook.grade_cells) == 1 assert len(notebook.solution_cells) == 1 assert len(notebook.source_cells) == 1
Example #21
Source File: test_savecells.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_save_new_cell_with_submissions(self, preprocessor, gradebook, resources): cell1 = create_grade_and_solution_cell("hello", "markdown", "foo", 2) cell2 = create_grade_and_solution_cell("hello", "markdown", "bar", 1) nb = new_notebook() nb.cells.append(cell1) nb, resources = preprocessor.preprocess(nb, resources) notebook = gradebook.find_notebook("test", "ps0") assert len(notebook.grade_cells) == 1 assert len(notebook.solution_cells) == 1 assert len(notebook.source_cells) == 1 gradebook.add_student("hacker123") gradebook.add_submission("ps0", "hacker123") nb.cells.append(cell2) with pytest.raises(RuntimeError): nb, resources = preprocessor.preprocess(nb, resources)
Example #22
Source File: test_savecells.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_save_new_cell(self, preprocessor, gradebook, resources): cell1 = create_grade_and_solution_cell("hello", "markdown", "foo", 2) cell2 = create_grade_and_solution_cell("hello", "markdown", "bar", 1) nb = new_notebook() nb.cells.append(cell1) nb, resources = preprocessor.preprocess(nb, resources) notebook = gradebook.find_notebook("test", "ps0") assert len(notebook.grade_cells) == 1 assert len(notebook.solution_cells) == 1 assert len(notebook.source_cells) == 1 nb.cells.append(cell2) nb, resources = preprocessor.preprocess(nb, resources) gradebook.db.refresh(notebook) assert len(notebook.grade_cells) == 2 assert len(notebook.solution_cells) == 2 assert len(notebook.source_cells) == 2
Example #23
Source File: test_savecells.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_save_code_grade_and_solution_cell(self, preprocessor, gradebook, resources): cell = create_grade_and_solution_cell("hello", "code", "foo", 1) cell.metadata.nbgrader['checksum'] = compute_checksum(cell) nb = new_notebook() nb.cells.append(cell) nb, resources = preprocessor.preprocess(nb, resources) grade_cell = gradebook.find_grade_cell("foo", "test", "ps0") assert grade_cell.max_score == 1 assert grade_cell.cell_type == "code" gradebook.find_solution_cell("foo", "test", "ps0") source_cell = gradebook.find_source_cell("foo", "test", "ps0") assert source_cell.source == "hello" assert source_cell.checksum == cell.metadata.nbgrader["checksum"] assert source_cell.cell_type == "code" assert not source_cell.locked
Example #24
Source File: test_overwritekernelspec.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_overwrite_kernelspec(self, preprocessors, resources, gradebook): kernelspec = dict( display_name='blarg', name='python3', language='python', ) nb = new_notebook() nb.metadata['kernelspec'] = kernelspec nb, resources = preprocessors[0].preprocess(nb, resources) nb.metadata['kernelspec'] = {} nb, resources = preprocessors[1].preprocess(nb, resources) validate(nb) notebook = gradebook.find_notebook("test", "ps0") assert nb.metadata['kernelspec'] == kernelspec assert json.loads(notebook.kernelspec) == kernelspec
Example #25
Source File: test_overwritecells.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_overwrite_locked_checksum(self, preprocessors, resources): """Is the checksum overwritten for locked cells?""" cell = create_locked_cell("hello", "code", "foo") cell.metadata.nbgrader['checksum'] = compute_checksum(cell) nb = new_notebook() nb.cells.append(cell) nb, resources = preprocessors[0].preprocess(nb, resources) cell.metadata.nbgrader["checksum"] = "1234" nb, resources = preprocessors[1].preprocess(nb, resources) assert cell.metadata.nbgrader["checksum"] == compute_checksum(cell)
Example #26
Source File: test_exporter.py From pipelines with Apache License 2.0 | 5 votes |
def test_create_cell_from_args_with_multiple_args(self): nb = new_notebook() args = { "source": "gs://ml-pipeline/data.csv", "target_lambda": "lambda x: (x['target'] > x['fare'] * 0.2)" } code = [ "print(variables.get('source'))", "print(variables.get('target_lambda'))" ] nb.cells.append(exporter.create_cell_from_args(args)) nb.cells.append(exporter.create_cell_from_custom_code(code)) html = self.exporter.generate_html_from_notebook(nb) self.assertMatchSnapshot(html)
Example #27
Source File: test_overwritecells.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_overwrite_solution_checksum(self, preprocessors, resources): """Is the checksum overwritten for solution cells?""" cell = create_solution_cell("hello", "code", "foo") cell.metadata.nbgrader['checksum'] = compute_checksum(cell) nb = new_notebook() nb.cells.append(cell) nb, resources = preprocessors[0].preprocess(nb, resources) cell.metadata.nbgrader["checksum"] = "1234" nb, resources = preprocessors[1].preprocess(nb, resources) assert cell.metadata.nbgrader["checksum"] == compute_checksum(cell)
Example #28
Source File: test_overwritecells.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_overwrite_grade_checksum(self, preprocessors, resources): """Is the checksum overwritten for grade cells?""" cell = create_grade_cell("hello", "code", "foo", 1) cell.metadata.nbgrader['checksum'] = compute_checksum(cell) nb = new_notebook() nb.cells.append(cell) nb, resources = preprocessors[0].preprocess(nb, resources) cell.metadata.nbgrader["checksum"] = "1234" nb, resources = preprocessors[1].preprocess(nb, resources) assert cell.metadata.nbgrader["checksum"] == compute_checksum(cell)
Example #29
Source File: test_overwritecells.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_overwrite_solution_cell_type(self, preprocessors, resources): """Is the cell type overwritten for solution cells?""" cell = create_solution_cell("hello", "code", "foo") cell.metadata.nbgrader['checksum'] = compute_checksum(cell) nb = new_notebook() nb.cells.append(cell) nb, resources = preprocessors[0].preprocess(nb, resources) cell.cell_type = "markdown" nb, resources = preprocessors[1].preprocess(nb, resources) assert cell.cell_type == "code"
Example #30
Source File: test_overwritecells.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_dont_overwrite_grade_and_solution_source(self, preprocessors, resources): """Is the source not overwritten for grade+solution cells?""" cell = create_grade_and_solution_cell("hello", "code", "foo", 1) cell.metadata.nbgrader['checksum'] = compute_checksum(cell) nb = new_notebook() nb.cells.append(cell) nb, resources = preprocessors[0].preprocess(nb, resources) cell.source = "hello!" nb, resources = preprocessors[1].preprocess(nb, resources) assert cell.source == "hello!"