How to use the nbformat.v4.nbbase.new_code_cell function in nbformat

To help you get started, we’ve selected a few nbformat examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mwouts / jupytext / tests / test_escape_magics.py View on Github external
def test_magics_are_commented(fmt):
    nb = new_notebook(cells=[new_code_cell('%pylab inline')],
                      metadata={'jupytext': {'comment_magics': True,
                                             'main_language': 'R' if fmt == 'R'
                                             else 'scheme' if fmt.startswith('ss') else 'python'}})

    text = jupytext.writes(nb, fmt)
    assert '%pylab inline' not in text.splitlines()
    nb2 = jupytext.reads(text, fmt)

    if 'sphinx' in fmt:
        nb2.cells = nb2.cells[1:]

    compare_notebooks(nb2, nb)
github mwouts / jupytext / tests / test_read_simple_python.py View on Github external
# +
# float?

# +
# float??

# +
# Finally a question in a code
# # cell?
""", nb=new_notebook(
    cells=[
        new_markdown_cell('This is a markdown cell\nthat ends with a question: float?'),
        new_markdown_cell('The next cell is also a markdown cell,\nbecause it has no code marker:'),
        new_markdown_cell('float?'),
        new_code_cell('float?'),
        new_code_cell('float??'),
        new_code_cell('# Finally a question in a code\n# cell?')])):
    nb2 = jupytext.reads(text, 'py')
    compare_notebooks(nb2, nb)

    text2 = jupytext.writes(nb2, 'py')
    compare(text2, text)
github mwouts / jupytext / tests / test_compare.py View on Github external
def test_mutiple_cells_differ():
    nb1 = new_notebook(cells=[new_code_cell(''),
                              new_code_cell('2')])
    nb2 = new_notebook(cells=[new_code_cell('1+1'),
                              new_code_cell('2\n2')])
    with pytest.raises(NotebookDifference) as exception_info:
        compare_notebooks(nb2, nb1, raise_on_first_difference=False)
    assert 'Cells 1,2 differ' in exception_info.value.args[0]
github mwouts / jupytext / tests / test_contentsmanager.py View on Github external
def test_pair_unpair_notebook(tmpdir):
    tmp_ipynb = 'notebook.ipynb'
    tmp_md = 'notebook.md'

    nb = new_notebook(
        metadata={'kernelspec': {'display_name': 'Python3', 'language': 'python', 'name': 'python3'}},
        cells=[new_code_cell('1 + 1', outputs=[
            {
                "data": {
                    "text/plain": [
                        "2"
                    ]
                },
                "execution_count": 1,
                "metadata": {},
                "output_type": "execute_result"
            }
        ])])

    cm = jupytext.TextFileContentsManager()
    cm.root_dir = str(tmpdir)

    # save notebook
github mwouts / jupytext / tests / test_compare.py View on Github external
def test_mutiple_cells_differ():
    nb1 = new_notebook(cells=[new_code_cell(''),
                              new_code_cell('2')])
    nb2 = new_notebook(cells=[new_code_cell('1+1'),
                              new_code_cell('2\n2')])
    with pytest.raises(NotebookDifference) as exception_info:
        compare_notebooks(nb2, nb1, raise_on_first_difference=False)
    assert 'Cells 1,2 differ' in exception_info.value.args[0]
github mwouts / jupytext / tests / test_read_simple_percent.py View on Github external
# %% And now a code cell
1 + 2 + 3 + 4
5
6
# %%magic # this is a commented magic, not a cell

7
"""):
    nb = jupytext.reads(script, 'py:percent')
    compare_notebooks(new_notebook(cells=[
        new_raw_cell('---\ntitle: Simple file\n---'),
        new_markdown_cell('This is a markdown cell'),
        new_markdown_cell('This is also a markdown cell', metadata={'region_name': 'md'}),
        new_raw_cell('This is a raw cell'),
        new_code_cell('# This is a sub-cell', metadata={'title': 'sub-cell title', 'cell_depth': 1}),
        new_code_cell('# This is a sub-sub-cell', metadata={'title': 'sub-sub-cell title', 'cell_depth': 2}),
        new_code_cell('''1 + 2 + 3 + 4
5
6
%%magic # this is a commented magic, not a cell

7''', metadata={'title': 'And now a code cell'})]), nb)

    script2 = jupytext.writes(nb, 'py:percent')
    compare(script2, script)
github altair-viz / altair / tools / create_example_notebooks.py View on Github external
'# Altair Example: {2}\n\n'
                                '{3}\n\n'.format(full_filename, full_filepath,
                                                 title, description))

        yield new_markdown_cell('## Load Dataset\n'
                                 'The data comes in the form of a Pandas '
                                 'Dataframe:')
        yield new_code_cell('from altair import load_dataset\n'
                            'data = load_dataset("{0}")\n'
                            'data.head()'.format(dataset))

        yield new_markdown_cell('## Define Altair Specification')
        yield new_code_cell('from altair import *  # Import the altair API\n\n'
                            'chart = {0}'.format(chart.to_altair(data='data')))
        yield new_markdown_cell('IPython rich display will invoke Vega-Lite:')
        yield new_code_cell('chart')

        yield new_markdown_cell('## Output Vega-Lite Specification')
        yield new_markdown_cell('Generate JSON dict, leaving data out:')
        yield new_code_cell('chart.to_dict(data=False)')
github scidash / sciunit / sciunit / __main__.py View on Github external
def add_code_cell(cells, source):
    """Add a code cell containing `source` to the notebook."""
    from nbformat.v4.nbbase import new_code_cell
    n_code_cells = len([c for c in cells if c['cell_type'] == 'code'])
    cells.append(new_code_cell(source=source, execution_count=n_code_cells+1))
github mwouts / jupytext / nbrmd / cells.py View on Github external
def code_or_raw_cell(source, metadata):
    """Return a code, or raw cell from given source and metadata"""
    if not is_active('ipynb', metadata):
        if metadata.get('active') == '':
            del metadata['active']
        return new_raw_cell(source='\n'.join(source), metadata=metadata)
    return new_code_cell(source='\n'.join(source), metadata=metadata)