本文整理汇总了Python中nbformat.v4.new_code_cell方法的典型用法代码示例。如果您正苦于以下问题:Python v4.new_code_cell方法的具体用法?Python v4.new_code_cell怎么用?Python v4.new_code_cell使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nbformat.v4
的用法示例。
在下文中一共展示了v4.new_code_cell方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_cell
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def add_cell(cells, filename, insert=None, **kwargs):
"""Modifies cells in place by adding the content of filename as a new cell in position `insert`.
When `insert` is None, the new cell is appended to cells.
Allows optional formatting arguments as kwargs."""
with open(filename, 'r') as f:
code_source = f.read()
if kwargs:
code_source = code_source.format(**kwargs)
new_cell = new_code_cell(code_source)
if insert is None:
cells.append(new_cell)
else:
cells.insert(insert, new_cell)
return cells
示例2: buildInitCell
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def buildInitCell(nb, numOfCategories, d3Flots, appName, runId):
"""
Method to build the init cell which contains the intro,
serialized transactions object and metadata for generating reports
"""
from xpedite.jupyter.templates import loadInitCell
initCode = loadInitCell()
try:
envLink = buildReportLink('envReport', Action.Load)
initCode = initCode.format(
envLink=envLink, appName=appName, categoryCount=numOfCategories + 1, runId=runId
)
except TypeError:
typeErr = 'Number of placeholders in init code string do not match the number of args supplied'
LOGGER.exception(typeErr)
raise InvariantViloation(typeErr)
nb['cells'] = [nbf.new_code_cell(source=initCode, metadata={'init_cell': True, 'isInit': '0xFFFFFFFFA5A55A5DUL',\
'hide_input': True, 'editable': False, 'deletable': False,\
'd3Flots': d3Flots})] + nb['cells']
示例3: py2ipynb
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [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: create_regular_cell
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def create_regular_cell(source, cell_type, schema_version=SCHEMA_VERSION):
if cell_type == "markdown":
cell = new_markdown_cell(source=source)
elif cell_type == "code":
cell = new_code_cell(source=source)
else:
raise ValueError("invalid cell type: {}".format(cell_type))
cell.metadata.nbgrader = {}
cell.metadata.nbgrader["grade"] = False
cell.metadata.nbgrader["grade_id"] = ""
cell.metadata.nbgrader["points"] = 0.0
cell.metadata.nbgrader["solution"] = False
cell.metadata.nbgrader["task"] = False
cell.metadata.nbgrader["locked"] = False
cell.metadata.nbgrader["schema_version"] = schema_version
return cell
示例5: create_grade_cell
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def create_grade_cell(source, cell_type, grade_id, points, schema_version=SCHEMA_VERSION):
if cell_type == "markdown":
cell = new_markdown_cell(source=source)
elif cell_type == "code":
cell = new_code_cell(source=source)
else:
raise ValueError("invalid cell type: {}".format(cell_type))
cell.metadata.nbgrader = {}
cell.metadata.nbgrader["grade"] = True
cell.metadata.nbgrader["grade_id"] = grade_id
cell.metadata.nbgrader["points"] = points
cell.metadata.nbgrader["solution"] = False
cell.metadata.nbgrader["task"] = False
cell.metadata.nbgrader["locked"] = False
cell.metadata.nbgrader["schema_version"] = schema_version
return cell
示例6: create_solution_cell
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def create_solution_cell(source, cell_type, grade_id, schema_version=SCHEMA_VERSION):
if cell_type == "markdown":
cell = new_markdown_cell(source=source)
elif cell_type == "code":
cell = new_code_cell(source=source)
else:
raise ValueError("invalid cell type: {}".format(cell_type))
cell.metadata.nbgrader = {}
cell.metadata.nbgrader["solution"] = True
cell.metadata.nbgrader["grade_id"] = grade_id
cell.metadata.nbgrader["grade"] = False
cell.metadata.nbgrader["task"] = False
cell.metadata.nbgrader["locked"] = False
cell.metadata.nbgrader["schema_version"] = schema_version
return cell
示例7: create_locked_cell
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def create_locked_cell(source, cell_type, grade_id, schema_version=SCHEMA_VERSION):
if cell_type == "markdown":
cell = new_markdown_cell(source=source)
elif cell_type == "code":
cell = new_code_cell(source=source)
else:
raise ValueError("invalid cell type: {}".format(cell_type))
cell.metadata.nbgrader = {}
cell.metadata.nbgrader["locked"] = True
cell.metadata.nbgrader["grade_id"] = grade_id
cell.metadata.nbgrader["solution"] = False
cell.metadata.nbgrader["task"] = False
cell.metadata.nbgrader["grade"] = False
cell.metadata.nbgrader["schema_version"] = schema_version
return cell
示例8: create_task_cell
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def create_task_cell(source, cell_type, grade_id, points, schema_version=SCHEMA_VERSION):
if cell_type == "markdown":
cell = new_markdown_cell(source=source)
elif cell_type == "code":
cell = new_code_cell(source=source)
else:
raise ValueError("invalid cell type: {}".format(cell_type))
cell.metadata.nbgrader = {}
cell.metadata.nbgrader["solution"] = False
cell.metadata.nbgrader["grade"] = False
cell.metadata.nbgrader["task"] = True
cell.metadata.nbgrader["grade_id"] = grade_id
cell.metadata.nbgrader["points"] = points
cell.metadata.nbgrader["locked"] = True
cell.metadata.nbgrader["schema_version"] = schema_version
return cell
示例9: code_cell
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def code_cell(group, remove=None):
source = '\n'.join(group).strip()
return new_code_cell(source)
示例10: generate_notebook_from_arguments
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def generate_notebook_from_arguments(
self,
arguments: dict,
source: Text,
visualization_type: Text
) -> NotebookNode:
"""Generates a NotebookNode from provided arguments.
Args:
arguments: JSON object containing provided arguments.
source: Path or path pattern to be used as data reference for
visualization.
visualization_type: Name of visualization to be generated.
Returns:
NotebookNode that contains all parameters from a post request.
"""
nb = new_notebook()
nb.cells.append(exporter.create_cell_from_args(arguments))
nb.cells.append(new_code_cell('source = "{}"'.format(source)))
if visualization_type == "custom":
code = arguments.get("code", [])
nb.cells.append(exporter.create_cell_from_custom_code(code))
else:
visualization_file = str(Path.cwd() / "types/{}.py".format(visualization_type))
nb.cells.append(exporter.create_cell_from_file(visualization_file))
return nb
示例11: create_cell_from_args
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def create_cell_from_args(variables: dict) -> NotebookNode:
"""Creates NotebookNode object containing dict of provided variables.
Args:
variables: Arguments that need to be injected into a NotebookNode.
Returns:
NotebookNode with provided arguments as variables.
"""
return new_code_cell("variables = {}".format(repr(variables)))
示例12: create_cell_from_file
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def create_cell_from_file(filepath: Text) -> NotebookNode:
"""Creates a NotebookNode object with provided file as code in node.
Args:
filepath: Path to file that should be used.
Returns:
NotebookNode with specified file as code within node.
"""
with open(filepath, 'r') as f:
code = f.read()
return new_code_cell(code)
示例13: create_cell_from_custom_code
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def create_cell_from_custom_code(code: list) -> NotebookNode:
"""Creates a NotebookNode object with provided list as code in node.
Args:
code: list representing lines of code to be run.
Returns:
NotebookNode with specified file as code within node.
"""
cell = new_code_cell("\n".join(code))
cell.get("metadata")["hide_logging"] = False
return cell
示例14: test_no_outputs
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def test_no_outputs():
nb = Notebook(new_notebook(cells=[new_code_cell("test", outputs=[])]))
assert nb.scraps == collections.OrderedDict()
示例15: test_empty_metadata
# 需要导入模块: from nbformat import v4 [as 别名]
# 或者: from nbformat.v4 import new_code_cell [as 别名]
def test_empty_metadata():
output = new_output(output_type="display_data", data={}, metadata={})
raw_nb = new_notebook(cells=[new_code_cell("test", outputs=[output])])
nb = Notebook(raw_nb)
assert nb.scraps == collections.OrderedDict()