本文整理匯總了Python中nbformat.v4.new_notebook方法的典型用法代碼示例。如果您正苦於以下問題:Python v4.new_notebook方法的具體用法?Python v4.new_notebook怎麽用?Python v4.new_notebook使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類nbformat.v4
的用法示例。
在下文中一共展示了v4.new_notebook方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: tex2ipy
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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
示例2: buildNotebook
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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
示例3: py2ipynb
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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)
示例4: combine_notebooks
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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
示例5: test_regular_cells
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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)
示例6: test_regular_cells
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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)
示例7: test_regular_cells
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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)
示例8: test_grade_correct_code
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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
示例9: test_grade_incorrect_code
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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
示例10: test_grade_unchanged_markdown
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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
示例11: test_grade_changed_markdown
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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
示例12: test_grade_existing_auto_comment
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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
示例13: test_invalid_grade_cell_id
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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)
示例14: test_overwrite_kernelspec
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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
示例15: test_save_code_grade_cell
# 需要導入模塊: from nbformat import v4 [as 別名]
# 或者: from nbformat.v4 import new_notebook [as 別名]
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