本文整理汇总了Python中nbformat.v4.new_notebook函数的典型用法代码示例。如果您正苦于以下问题:Python new_notebook函数的具体用法?Python new_notebook怎么用?Python new_notebook使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了new_notebook函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_inline_merge_source_empty
def test_inline_merge_source_empty():
base = new_notebook()
local = new_notebook()
remote = new_notebook()
expected = new_notebook()
merged, decisions = merge_notebooks(base, local, remote)
assert merged == expected
示例2: test_inline_merge_attachments
def test_inline_merge_attachments():
# FIXME: Use output creation utils Vidar wrote in another test file
base = new_notebook()
local = new_notebook()
remote = new_notebook()
expected = new_notebook()
merged, decisions = merge_notebooks(base, local, remote)
assert merged == expected
示例3: test_inline_merge_dummy_notebooks
def test_inline_merge_dummy_notebooks():
"Just the basic empty notebook passes through."
base = new_notebook()
local = new_notebook()
remote = new_notebook()
expected = new_notebook()
merged, decisions = merge_notebooks(base, local, remote)
assert expected == merged
示例4: test_inline_merge_notebook_version
def test_inline_merge_notebook_version():
"Minor version gets bumped to max."
base = new_notebook(nbformat=4, nbformat_minor=0)
local = new_notebook(nbformat=4, nbformat_minor=1)
remote = new_notebook(nbformat=4, nbformat_minor=2)
expected = new_notebook(nbformat=4, nbformat_minor=2)
merged, decisions = merge_notebooks(base, local, remote)
assert expected == merged
示例5: notebook
def notebook(self, s):
"""Export and convert IPython notebooks.
This function can export the current IPython history to a notebook file.
For example, to export the history to "foo.ipynb" do "%notebook foo.ipynb".
The -e or --export flag is deprecated in IPython 5.2, and will be
removed in the future.
"""
args = magic_arguments.parse_argstring(self.notebook, s)
from nbformat import write, v4
cells = []
hist = list(self.shell.history_manager.get_range())
if(len(hist)<=1):
raise ValueError('History is empty, cannot export')
for session, execution_count, source in hist[:-1]:
cells.append(v4.new_code_cell(
execution_count=execution_count,
source=source
))
nb = v4.new_notebook(cells=cells)
with io.open(args.filename, 'w', encoding='utf-8') as f:
write(nb, f, version=4)
示例6: setUp
def setUp(self):
nbdir = self.notebook_dir
subdir = pjoin(nbdir, 'foo')
try:
os.mkdir(subdir)
except OSError as e:
# Deleting the folder in an earlier test may have failed
if e.errno != errno.EEXIST:
raise
self.addCleanup(partial(shutil.rmtree, subdir, ignore_errors=True))
with io.open(pjoin(subdir, 'nb1.ipynb'), 'w', encoding='utf-8') as f:
nb = new_notebook()
write(nb, f, version=4)
self.sess_api = SessionAPI(self.request)
@self.addCleanup
def cleanup_sessions():
for session in self.sess_api.list().json():
self.sess_api.delete(session['id'])
# This is necessary in some situations on Windows: without it, it
# fails to delete the directory because something is still using
# it. I think there is a brief period after the kernel terminates
# where Windows still treats its working directory as in use. On my
# Windows VM, 0.01s is not long enough, but 0.1s appears to work
# reliably. -- TK, 15 December 2014
time.sleep(0.1)
示例7: test_raw_template_dynamic_attr_reversed
def test_raw_template_dynamic_attr_reversed(self):
"""
Test that template_file and raw_template traitlets play nicely together.
- source assigns raw_template default first, then template_file
- checks that the raw_template overrules template_file if set
- checks that once raw_template is set to '', template_file returns
"""
nb = v4.new_notebook()
nb.cells.append(v4.new_code_cell("some_text"))
class AttrDynamicExporter(TemplateExporter):
@default('raw_template')
def _raw_template_default(self):
return raw_template
@default('template_file')
def _template_file_default(self):
return "rst.tpl"
exporter_attr_dynamic = AttrDynamicExporter()
output_attr_dynamic, _ = exporter_attr_dynamic.from_notebook_node(nb)
assert "blah" in output_attr_dynamic
exporter_attr_dynamic.raw_template = ''
assert exporter_attr_dynamic.template_file == "rst.tpl"
output_attr_dynamic, _ = exporter_attr_dynamic.from_notebook_node(nb)
assert "blah" not in output_attr_dynamic
示例8: new
def new(self, model=None, path=''):
"""Create a new file or directory and return its model with no content.
To create a new untitled entity in a directory, use `new_untitled`.
"""
path = path.strip('/')
if model is None:
model = {}
if path.endswith('.ipynb'):
model.setdefault('type', 'notebook')
else:
model.setdefault('type', 'file')
# no content, not a directory, so fill out new-file model
if 'content' not in model and model['type'] != 'directory':
if model['type'] == 'notebook':
model['content'] = new_notebook()
model['format'] = 'json'
else:
model['content'] = ''
model['type'] = 'file'
model['format'] = 'text'
model = self.save(model, path)
return model
示例9: test_modify_cell_with_submissions
def test_modify_cell_with_submissions(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"
gradebook.add_student("hacker123")
submission = gradebook.add_submission("ps0", "hacker123").notebooks[0]
assert len(notebook.submissions) == 1
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(submission)
gradebook.db.refresh(grade_cell)
gradebook.db.refresh(solution_cell)
gradebook.db.refresh(source_cell)
assert len(notebook.submissions) == 1
assert grade_cell.max_score == 1
assert source_cell.source == "goodbye"
示例10: test_grade_existing_manual_grade
def test_grade_existing_manual_grade(self, preprocessors, gradebook, resources):
"""Is a failing code cell correctly graded?"""
cell = create_grade_and_solution_cell("hello", "markdown", "foo", 1)
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
grade_cell.manual_score = 1
grade_cell.needs_manual_grade = False
gradebook.db.commit()
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 == None
assert grade_cell.manual_score == 1
assert grade_cell.needs_manual_grade
示例11: test_very_long_cells
def test_very_long_cells(self):
"""
Torture test that long cells do not cause issues
"""
lorem_ipsum_text = textwrap.dedent("""\
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
dignissim, ipsum non facilisis tempus, dui felis tincidunt metus,
nec pulvinar neque odio eget risus. Nulla nisi lectus, cursus
suscipit interdum at, ultrices sit amet orci. Mauris facilisis
imperdiet elit, vitae scelerisque ipsum dignissim non. Integer
consequat malesuada neque sit amet pulvinar. Curabitur pretium
ut turpis eget aliquet. Maecenas sagittis lacus sed lectus
volutpat, eu adipiscing purus pulvinar. Maecenas consequat
luctus urna, eget cursus quam mollis a. Aliquam vitae ornare
erat, non hendrerit urna. Sed eu diam nec massa egestas pharetra
at nec tellus. Fusce feugiat lacus quis urna sollicitudin volutpat.
Quisque at sapien non nibh feugiat tempus ac ultricies purus.
""")
lorem_ipsum_text = lorem_ipsum_text.replace("\n"," ") + "\n\n"
large_lorem_ipsum_text = "".join([lorem_ipsum_text]*3000)
notebook_name = "lorem_ipsum_long.ipynb"
nb = v4.new_notebook(
cells=[
v4.new_markdown_cell(source=large_lorem_ipsum_text)
]
)
with TemporaryDirectory() as td:
nbfile = os.path.join(td, notebook_name)
with open(nbfile, 'w') as f:
write(nb, f, 4)
(output, resources) = LatexExporter(template_file='article').from_filename(nbfile)
assert len(output) > 0
示例12: test_preprocessor_collapsible_headings
def test_preprocessor_collapsible_headings():
"""Test collapsible_headings preprocessor."""
# check import shortcut
from jupyter_contrib_nbextensions.nbconvert_support import CollapsibleHeadingsPreprocessor # noqa
cells = []
for lvl in range(6, 1, -1):
for collapsed in (True, False):
cells.extend([
nbf.new_markdown_cell(
source='{} {} heading level {}'.format(
'#' * lvl,
'Collapsed' if collapsed else 'Uncollapsed',
lvl),
metadata={'heading_collapsed': True} if collapsed else {}),
nbf.new_markdown_cell(source='\n'.join([
'want hidden' if collapsed else 'want to see',
'what I mean',
])),
nbf.new_code_cell(source='\n'.join([
'want hidden' if collapsed else 'want to see',
'what I mean',
])),
])
notebook_node = nbf.new_notebook(cells=cells)
body, resources = export_through_preprocessor(
notebook_node, CollapsibleHeadingsPreprocessor, RSTExporter, 'rst')
assert_not_in('hidden', body, 'check text hidden by collapsed headings')
assert_in('want to see', body, 'check for text under uncollapsed headings')
示例13: build_notebook
def build_notebook(self, with_json_outputs=False):
"""Build a notebook in memory for use with preprocessor tests"""
outputs = [
nbformat.new_output("stream", name="stdout", text="a"),
nbformat.new_output("display_data", data={'text/plain': 'b'}),
nbformat.new_output("stream", name="stdout", text="c"),
nbformat.new_output("stream", name="stdout", text="d"),
nbformat.new_output("stream", name="stderr", text="e"),
nbformat.new_output("stream", name="stderr", text="f"),
nbformat.new_output("display_data", data={'image/png': 'Zw=='}), # g
nbformat.new_output("display_data", data={'application/pdf': 'aA=='}), # h
]
if with_json_outputs:
outputs.extend([
nbformat.new_output(
"display_data", data={'application/json': [1, 2, 3]}
), # j
nbformat.new_output(
"display_data", data={'application/json': {'a': 1, 'c': {'b': 2}}}
), # k
nbformat.new_output(
"display_data", data={'application/json': 'abc'}
), # l
nbformat.new_output(
"display_data", data={'application/json': 15.03}
), # m
])
cells=[nbformat.new_code_cell(source="$ e $", execution_count=1, outputs=outputs),
nbformat.new_markdown_cell(source="$ e $")]
return nbformat.new_notebook(cells=cells)
示例14: split_into_units
def split_into_units(nb_name, include_header=True):
"""Split notebook into units."""
try:
nb = nbformat.read(nb_name, as_version=4)
except IOError as e:
if e.errno == 2:
print('File not found: {0}'.format(nb_name), sys.stderr)
return []
else:
raise e
cells = nb.cells
indexes = [i for i, cell in enumerate(cells)
if cell.cell_type == 'markdown' and cell.source.startswith('# ')]
separated_cells = [cells[i:j] for i, j in zip(indexes, indexes[1:]+[None])]
units = [current.new_notebook(cells=cells,
metadata={'name': cells[0]
.source
.split('\n')[0][2:]})
for cells in separated_cells]
if not include_header:
for unit in units:
# The first line is the header.
unit.cells[0].source = '\n'.join(unit.cells[0].source
.split('\n')[1:])
return units
示例15: create_notebook
def create_notebook(name, cells):
nb = new_notebook()
for cell in cells:
nb.cells.append(new_code_cell(source=cell))
with open(name, "w") as fh:
write(nb, fh, 4)