def lines_to_notebook(lines, name=None):
    """
    Convert the lines of an m file into an IPython notebook

    Parameters
    ----------
    lines : list
        A list of strings. Each element is a line in the m file

    Returns
    -------
    notebook : an IPython NotebookNode class instance, containing the
    information required to create a file


    """
    source = []
    md = np.empty(len(lines), dtype=object)
    new_cell = np.empty(len(lines), dtype=object)
    for idx, l in enumerate(lines):
        new_cell[idx], md[idx], this_source = format_line(l)
        # Transitions between markdown and code and vice-versa merit a new
        # cell, even if no newline, or "%%" is found. Make sure not to do this
        # check for the very first line!
        if idx>1 and not new_cell[idx]:
            if md[idx] != md[idx-1]:
                new_cell[idx] = True

        source.append(this_source)
    # This defines the breaking points between cells:
    new_cell_idx = np.hstack([np.where(new_cell)[0], -1])

    # Listify the sources:
    cell_source = [source[new_cell_idx[i]:new_cell_idx[i+1]]
                   for i in range(len(new_cell_idx)-1)]
    cell_md = [md[new_cell_idx[i]] for i in range(len(new_cell_idx)-1)]
    cells = []

    # Append the notebook with loading matlab magic extension
    notebook_head = "import pymatbridge as pymat\n" + "ip = get_ipython()\n" \
                    + "pymat.load_ipython_extension(ip)"
    cells.append(nbformat.new_code_cell(notebook_head))#, language='python'))

    for cell_idx, cell_s in enumerate(cell_source):
        if cell_md[cell_idx]:
            cells.append(nbformat.new_markdown_cell(cell_s))
        else:
            cell_s.insert(0, '%%matlab\n')
            cells.append(nbformat.new_code_cell(cell_s))#, language='matlab'))

    #ws = nbformat.new_worksheet(cells=cells)
    notebook = nbformat.new_notebook(cells=cells)
    return notebook
 def test_html_collapsible_headings(self):
     """Test exporter for inlining collapsible_headings"""
     nb = v4.new_notebook(cells=[
         v4.new_markdown_cell(source=('# level 1 heading')),
         v4.new_code_cell(source='a = range(1,10)'),
         v4.new_markdown_cell(source=('## level 2 heading')),
         v4.new_code_cell(source='a = range(1,10)'),
         v4.new_markdown_cell(source=('### level 3 heading')),
         v4.new_code_cell(source='a = range(1,10)'),
     ])
     self.check_stuff_gets_embedded(
         nb, 'html_ch', to_be_included=['collapsible_headings'])
    def test_html_collapsible_headings(self):
        """Test exporter for inlining collapsible_headings"""
        nb = v4.new_notebook(cells=[
            v4.new_markdown_cell(source=('# level 1 heading')),
            v4.new_code_cell(source='a = range(1,10)'),
            v4.new_markdown_cell(source=('## level 2 heading')),
            v4.new_code_cell(source='a = range(1,10)'),
            v4.new_markdown_cell(source=('### level 3 heading')),
            v4.new_code_cell(source='a = range(1,10)'),
        ])

        def check(byte_string, root_node):
            assert b'collapsible_headings' in byte_string

        self.check_html(nb, 'html_ch', check_func=check)
Exemple #4
0
    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)
    def build_notebook(self):
        """Build a reveal slides notebook in memory for use with tests.
        Overrides base in PreprocessorTestsBase"""

        outputs = [nbformat.new_output(output_type="stream", name="stdout", text="a")]

        slide_metadata = {'slideshow' : {'slide_type': 'slide'}}
        subslide_metadata = {'slideshow' : {'slide_type': 'subslide'}}

        cells=[nbformat.new_code_cell(source="", execution_count=1, outputs=outputs),
               nbformat.new_markdown_cell(source="", metadata=slide_metadata),
               nbformat.new_code_cell(source="", execution_count=2, outputs=outputs),
               nbformat.new_markdown_cell(source="", metadata=slide_metadata),
               nbformat.new_markdown_cell(source="", metadata=subslide_metadata)]

        return nbformat.new_notebook(cells=cells)
Exemple #6
0
    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)
def create_code_cell():
    source = """print("something")
### BEGIN SOLUTION
print("hello")
### END SOLUTION"""
    cell = new_code_cell(source=source)
    return cell
    def setUp(self):
        nbdir = self.notebook_dir
        
        if not os.path.isdir(pjoin(nbdir, 'foo')):
            subdir = pjoin(nbdir, 'foo')

            os.mkdir(subdir)

            # Make sure that we clean this up when we're done.
            # By using addCleanup this will happen correctly even if we fail
            # later in setUp.
            @self.addCleanup
            def cleanup_dir():
                shutil.rmtree(subdir, ignore_errors=True)

        nb = new_notebook()
        
        nb.cells.append(new_markdown_cell(u'Created by test ³'))
        cc1 = new_code_cell(source=u'print(2*6)')
        cc1.outputs.append(new_output(output_type="stream", text=u'12'))
        cc1.outputs.append(new_output(output_type="execute_result",
            data={'image/png' : png_green_pixel},
            execution_count=1,
        ))
        nb.cells.append(cc1)
        
        with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'), 'w',
                     encoding='utf-8') as f:
            write(nb, f, version=4)

        self.nbconvert_api = NbconvertAPI(self.request)
    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
Exemple #10
0
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)
Exemple #11
0
def test_pretty_print_code_cell():
    cell = v4.new_code_cell(source='def foo():\n    return 4',
        execution_count=3,
        outputs=[
            v4.new_output('stream', name='stdout', text='some\ntext'),
            v4.new_output('display_data', {'text/plain': 'hello display'}),
        ]
    )

    io = StringIO()
    pp.pretty_print_value_at(cell, "/cells/0", "+", io)
    text = io.getvalue()
    lines = text.splitlines()

    assert lines == [
        '+code cell:',
        '+  execution_count: 3',
        '+  source:',
        '+    def foo():',
        '+        return 4',
        '+  outputs:',
        '+    output 0:',
        '+      output_type: stream',
        '+      name: stdout',
        '+      text:',
        '+        some',
        '+        text',
        '+    output 1:',
        '+      output_type: display_data',
        '+      data:',
        '+        text/plain: hello display',
    ]
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')
def test_preprocessor_codefolding():
    """Test codefolding preprocessor."""
    # check import shortcut
    from jupyter_contrib_nbextensions.nbconvert_support import CodeFoldingPreprocessor  # noqa
    notebook_node = nbf.new_notebook(cells=[
        nbf.new_code_cell(source='\n'.join(["# Codefolding test 1",
                                            "'AXYZ12AXY'"]),
                          metadata={"code_folding": [0]}),
        nbf.new_code_cell(source='\n'.join(["# Codefolding test 2",
                                            "def myfun():",
                                            "    'GR4CX32ZT'"]),
                          metadata={"code_folding": [1]}),
    ])
    body, resources = export_through_preprocessor(
        notebook_node, CodeFoldingPreprocessor, RSTExporter, 'rst')
    assert_not_in('AXYZ12AXY', body, 'check firstline fold has worked')
    assert_not_in('GR4CX32ZT', body, 'check function fold has worked')
 def test_htmltoc2(self):
     """Test exporter for adding table of contents"""
     nb = v4.new_notebook(cells=[
         v4.new_code_cell(source="a = 'world'"),
         v4.new_markdown_cell(source="# Heading"),
     ])
     self.check_stuff_gets_embedded(
         nb, 'html_toc', to_be_included=['toc2'])
Exemple #15
0
    def get(self, cid):
        """Retrieve (and build) notebook for a single contribution [internal].
        ---
        operationId: get_entry
        parameters:
            - name: cid
              in: path
              type: string
              pattern: '^[a-f0-9]{24}$'
              required: true
              description: contribution ID (ObjectId)
        responses:
            200:
                description: single notebook
                schema:
                    $ref: '#/definitions/NotebooksSchema'
        """
        try:
            nb = Notebooks.objects.get(id=cid)
            nb.restore()
        except DoesNotExist:
            cells = [
                nbf.new_code_cell(
                    "# provide apikey to `load_client` in order to connect to api.mpcontribs.org\n"
                    "# or use bravado (see https://mpcontribs.org/api)\n"
                    "from mpcontribs.client import load_client\n"
                    "client = load_client()"
                ), nbf.new_code_cell(
                    "from mpcontribs.io.archieml.mpfile import MPFile\n"
                    f"result = client.contributions.get_entry(cid='{cid}').response().result\n"
                    "mpfile = MPFile.from_contribution(result)"
                )
            ]
            for typ in ['h', 't', 'g', 's']:
                cells.append(nbf.new_code_cell(f"mpfile.{typ}data"))
            nb = nbf.new_notebook()
            nb['cells'] = cells
            exprep.preprocess(nb, {})
            nb = Notebooks(**nb)
            nb.id = cid # to link to the according contribution
            nb.save() # calls Notebooks.clean()

        del nb.id
        return nb
    def test_raw_template_constructor(self):
        """
        Test `raw_template` as a keyword argument in the exporter constructor.
        """
        nb = v4.new_notebook()
        nb.cells.append(v4.new_code_cell("some_text"))

        output_constructor, _ = TemplateExporter(
            raw_template=raw_template).from_notebook_node(nb)
        assert "blah" in output_constructor
Exemple #17
0
def test_present_code_cell():
    cell = v4.new_code_cell(source='def foo()',
        outputs=[
            v4.new_output('stream', name='stdout', text='some\ntext'),
            v4.new_output('display_data', {'text/plain': 'hello display'}),
        ]
    )
    lines = pp.present_value('+ ', cell)
    assert lines[0] == ''
    assert lines[1] == '+ code cell:'
 def test_raw_template_assignment(self):
     """
     Test `raw_template` assigned after the fact on non-custom Exporter.
     """
     nb = v4.new_notebook()
     nb.cells.append(v4.new_code_cell("some_text"))
     exporter_assign = TemplateExporter()
     exporter_assign.raw_template = raw_template
     output_assign, _ = exporter_assign.from_notebook_node(nb)
     assert "blah" in output_assign
 def test_embedhtml(self):
     """Test exporter for embedding images into HTML"""
     nb = v4.new_notebook(cells=[
         v4.new_code_cell(source="a = 'world'"),
         v4.new_markdown_cell(
             source="![testimage]({})".format(path_in_data('icon.png'))
         ),
     ])
     self.check_stuff_gets_embedded(
         nb, 'html_embed', to_be_included=['base64'])
Exemple #20
0
    def build_notebook(self):
        """Build a reveal slides notebook in memory for use with tests.
        Overrides base in PreprocessorTestsBase"""

        outputs = [nbformat.new_output(output_type='display_data',
                                       data={'image/svg+xml':self.simple_svg})
                  ]

        cells=[nbformat.new_code_cell(source="", execution_count=1, outputs=outputs)]

        return nbformat.new_notebook(cells=cells)
def py_to_ipynb(source, dest):
    # Create the code cells by parsing the file in input
    cells = []
    for c in parse_py(source):
        cells.append(new_code_cell(source=c))

    # This creates a V4 Notebook with the code cells extracted above
    nb0 = new_notebook(cells=cells, metadata={"language": "python"})

    with codecs.open(dest, encoding="utf-8", mode="w") as f:
        nbformat.write(nb0, f, 4)
    def test_htmltoc2(self):
        """Test exporter for adding table of contents"""
        with self.create_temp_cwd():
            nb = v4.new_notebook(
                cells=[v4.new_code_cell(source="a = 'world'"), v4.new_markdown_cell(source="# Heading")]
            )
            with io.open("notebook2.ipynb", "w", encoding="utf-8") as f:
                write(nb, f, 4)

            self.nbconvert("--to html_toc" ' "notebook2"')
            assert os.path.isfile("notebook2.html")
    def build_notebook(self):
        notebook = super(TestRegexRemove, self).build_notebook()
        # Add a few empty cells
        notebook.cells.extend([
            nbformat.new_code_cell(''),
            nbformat.new_markdown_cell(' '),
            nbformat.new_raw_cell('\n'),
            nbformat.new_raw_cell('\t'),
        ])

        return notebook
    def test_htmltoc2(self):
        """Test exporter for adding table of contents"""
        nb = v4.new_notebook(cells=[
            v4.new_code_cell(source="a = 'world'"),
            v4.new_markdown_cell(source="# Heading"),
        ])

        def check(byte_string, root_node):
            assert b'toc2' in byte_string

        self.check_html(nb, 'html_toc', check_func=check)
Exemple #25
0
def parseBkr(data):
    nb = new_notebook()
    evaluators = list((cell['evaluator']) for cell in data['cells'] if 'evaluator' in cell)
    kernel_name = max(evaluators, key=evaluators.count) if evaluators else 'IPython'
    if kernel_name in ['JavaScript', 'HTML', 'TeX']:
        kernel_name = 'IPython'
    if kernel_name == 'IPython':
        kernel_spec = {"kernelspec": {
            "display_name": "Python 2",
            "language": "python",
            "name": "python2"
        }}
    else:
        kernel_spec = {"kernelspec": {
            "display_name": kernel_name,
            "language": kernel_name.lower(),
            "name": kernel_name.lower()
        }}
    nb.metadata = kernel_spec
    for cell in data['cells']:
        if cell['type'] == 'code':
            metadata = {}
            if 'initialization' in cell:
                metadata['init_cell'] = True
            if 'tags' in cell:
                tags = [cell['tags']]
                metadata['tags'] = tags
            if cell['evaluator'] != kernel_name:
                if cell['evaluator'] == 'TeX':
                    nb.cells.append(new_markdown_cell("${0}$".format(getFixedCodeText(cell['input']))))
                else:
                    nb.cells.append(
                        new_code_cell(source='%%{0}\n{1}'.format(cell['evaluator'].lower(), getFixedCodeText(cell['input'])),
                                      metadata=metadata))
            else:
                nb.cells.append(new_code_cell(source=getFixedCodeText(cell['input']), metadata=metadata))
        if cell['type'] == 'markdown':
            nb.cells.append(new_markdown_cell(getFixedCodeText(cell)))
        if cell['type'] == 'section':
            nb.cells.append(new_markdown_cell(setHeader(cell['level'], cell['title'])))
    return nb
Exemple #26
0
    def test_custom_filter_highlight_code(self):
        # Overwriting filters takes place at: Exporter.from_notebook_node
        nb = v4.new_notebook()
        nb.cells.append(v4.new_code_cell("some_text"))

        def custom_highlight_code(source, language="python", metadata=None):
            return source + " ADDED_TEXT"

        filters = {
            "highlight_code": custom_highlight_code
        }
        (output, resources) = HTMLExporter(template_file='basic', filters=filters).from_notebook_node(nb)
        self.assertTrue("ADDED_TEXT" in output)
def create_locked_cell(source, cell_type, grade_id):
    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

    return cell
def test_preprocessor_pymarkdown():
    """Test python markdown preprocessor."""
    # check import shortcut
    from jupyter_contrib_nbextensions.nbconvert_support import PyMarkdownPreprocessor  # noqa
    notebook_node = nbf.new_notebook(cells=[
        nbf.new_code_cell(source="a = 'world'"),
        nbf.new_markdown_cell(source="Hello {{ a }}",
                              metadata={"variables": {" a ": "world"}}),
    ])
    body, resources = export_through_preprocessor(
        notebook_node, PyMarkdownPreprocessor, RSTExporter, 'rst')
    expected = 'Hello world'
    assert_in(expected, body, 'first cell should contain {}'.format(expected))
Exemple #29
0
 def new_code_cell(self, node, index=None):
     # Get the code cell input: the first child of the CodeCell block.
     input_block = node.children[0]
     assert input_block.name == 'CodeBlock'
     cell = new_code_cell(input_block.children[0],
                          execution_count=self.execution_count,
                          )
     # Next we need to add the outputs: the next children in the CodeCell.
     for child in node.children[1:]:
         # Outputs can be code blocks or Markdown paragraphs containing
         # an image.
         if child.name == 'CodeBlock':
             # The output is a code block.
             # What is the output's type? It depends on the code block's
             # name. It can be: `stdout`, `stderr`, `result`.
             output_type = child.lang or 'result'
             assert output_type in ('stdout', 'stderr', 'result')
             contents = child.children[0]
             # NOTE: append new lines at the end of every line in stdout
             # and stderr contents, to match with the Jupyter Notebook.
             if output_type != 'result':
                 contents = _append_newlines(contents)
             if output_type == 'result':
                 kwargs = dict(execution_count=self.execution_count,
                               data={'text/plain': contents})
                 # Output type to pass to nbformat.
                 output_type = 'execute_result'
             elif output_type in ('stdout', 'stderr'):
                 # Standard output or error.
                 kwargs = dict(text=contents, name=output_type)
                 # Output type to pass to nbformat.
                 output_type = 'stream'
         elif child.name == 'Para':
             img = child.children[0]
             assert img.name == 'Image'
             fn = img.url
             caption = self._md.write(img.children[0])
             output_type = 'display_data'
             data = {}  # Dictionary {mimetype: data_buffer}.
             # Infer the mime type of the file, from its filename and
             # extension.
             mime_type = guess_type(fn)[0]
             assert mime_type  # unknown extension: this shouldn't happen!
             data[mime_type] = self._get_b64_resource(fn)
             assert data[mime_type]  # TODO
             data['text/plain'] = caption
             kwargs = dict(data=data)
         output = new_output(output_type, **kwargs)
         cell.outputs.append(output)
     self.execution_count += 1
     return cell
Exemple #30
0
def convert_exercise_to_cells(exercise):
    """
    Generates a header, exercise text and code cell from an Exercise class
    """
    cells = []
    markdown_text = "## " + exercise.name + "\n"
    markdown_text += textwrap.dedent(exercise.__doc__) or ""
    cells.append(nbf.new_markdown_cell(markdown_text))

    code_cell_text = "%%run_exercise " + exercise.name + "\n"
    code_cell_text += textwrap.dedent(exercise.cell_code)
    cells.append(nbf.new_code_cell(code_cell_text))

    return cells
Exemple #31
0
def test_preprocessor_codefolding():
    """Test codefolding preprocessor."""
    # check import shortcut
    from jupyter_contrib_nbextensions.nbconvert_support import CodeFoldingPreprocessor  # noqa: E501
    notebook_node = nbf.new_notebook(cells=[
        nbf.new_code_cell(source='\n'.join(["# Codefolding test 1",
                                            "'AXYZ12AXY'"]),
                          metadata={"code_folding": [0]}),
        nbf.new_code_cell(source='\n'.join(["# Codefolding test 2",
                                            "def myfun():",
                                            "    if True : ",
                                            "       ",
                                            "      ",
                                            "        'GR4CX32ZT'",
                                            "        ",
                                            "      "]),
                          metadata={"code_folding": [1]}),
        nbf.new_code_cell(source='\n'.join(["# Codefolding test 3",
                                            "def myfun():",
                                            "    if True : ",
                                            "       ",
                                            "      ",
                                            "        'GR4CX32ZE'",
                                            "        ",
                                            "      ",
                                            "    'GR4CX32ZR'"]),
                          metadata={"code_folding": [2]})
    ])
    customconfig = Config(CodeFoldingPreprocessor={'remove_folded_code': True})
    body, resources = export_through_preprocessor(
        notebook_node, CodeFoldingPreprocessor, RSTExporter, 'rst',
        customconfig)
    assert_not_in('AXYZ12AXY', body, 'check firstline fold has worked')
    assert_not_in('GR4CX32ZT', body, 'check function fold has worked')
    assert_in('GR4CX32ZR', body, 'check if fold has worked')
    assert_not_in('GR4CX32ZE', body, 'check if fold has worked')
 def test_javascript_output(self):
     nb = v4.new_notebook(
         cells=[
             v4.new_code_cell(
                 outputs=[v4.new_output(
                     output_type='display_data',
                     data={
                         'application/javascript': "javascript_output();"
                     }
                 )]
             )
         ]
     )
     (output, resources) = HTMLExporter(template_file='basic').from_notebook_node(nb)
     self.assertIn('javascript_output', output)
Exemple #33
0
    def test_custom_filter_highlight_code(self):
        # Overwriting filters takes place at: Exporter.from_notebook_node
        nb = v4.new_notebook()
        nb.cells.append(v4.new_code_cell("some_text"))

        def custom_highlight_code(source,
                                  language="python",
                                  metadata=None,
                                  strip_verbatim=False):
            return source + " ADDED_TEXT"

        filters = {"highlight_code": custom_highlight_code}
        (output,
         resources) = LatexExporter(filters=filters).from_notebook_node(nb)
        self.assertTrue("ADDED_TEXT" in output)
Exemple #34
0
def generate_notebook(fname, nlines=10, ncells=100):
    """Generate a notebook to test loading time

    fname: destination filename
    nlines: number of lines of input/output per cell
    ncells: number of cells
    """
    nb = v4.new_notebook()
    source = '\n'.join(['print(%i)' % i for i in range(nlines)])
    output = v4.new_output('stream', text='\n'.join(map(str, range(nlines))))
    nb.cells = [
        v4.new_code_cell(source, outputs=[output]) for i in range(ncells)
    ]
    with open(fname, 'w') as f:
        write(nb, f)
Exemple #35
0
def create_grade_and_solution_cell(source, cell_type, grade_id, points):
    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"] = True
    cell.metadata.nbgrader["grade_id"] = grade_id
    cell.metadata.nbgrader["points"] = points

    return cell
Exemple #36
0
def nbuild(in_files: List[str]) -> NotebookNode:
    """Create an unexecuted Jupyter notebook from markdown and code files.

    :param in_files: A list of source file names.
    :return: An unexecuted ``nbformat.NotebookNode`` object.
    """
    nb = new_notebook()
    nb.cells = [
        new_code_cell(Path(name).read_text())
        if name.endswith((".py", ".R"))
        else new_markdown_cell(Path(name).read_text())
        for name in in_files
    ]

    return nb
    def test_embedhtml(self):
        """Test exporter for embedding images into HTML"""
        nb = v4.new_notebook(cells=[
            v4.new_code_cell(source="a = 'world'"),
            v4.new_markdown_cell(
                source="![testimage]({})".format(path_in_data('icon.png'))),
        ])

        def check(byte_string, root_node):
            nodes = root_node.findall(".//img")
            for n in nodes:
                url = n.attrib["src"]
                assert url.startswith('data')

        self.check_html(nb, 'html_embed', check_func=check)
Exemple #38
0
 def test_raw_template_deassignment(self):
     """
     Test `raw_template` does not overwrite template_file if deassigned after
     being assigned to a non-custom Exporter.
     """
     nb = v4.new_notebook()
     nb.cells.append(v4.new_code_cell("some_text"))
     exporter_deassign = RSTExporter()
     exporter_deassign.raw_template = raw_template
     output_deassign, _ = exporter_deassign.from_notebook_node(nb)
     assert "blah" in output_deassign
     exporter_deassign.raw_template = ''
     assert exporter_deassign.template_file == 'index.rst.j2'
     output_deassign, _ = exporter_deassign.from_notebook_node(nb)
     assert "blah" not in output_deassign
Exemple #39
0
 def test_run_nb(self):
     """Test %run notebook.ipynb"""
     from nbformat import v4, writes
     nb = v4.new_notebook(
        cells=[
             v4.new_markdown_cell("The Ultimate Question of Everything"),
             v4.new_code_cell("answer=42")
         ]
     )
     src = writes(nb, version=4)
     self.mktmp(src, ext='.ipynb')
     
     _ip.magic("run %s" % self.fname)
     
     nt.assert_equal(_ip.user_ns['answer'], 42)
Exemple #40
0
    def setup_class(cls):
        """Make a test notebook. Borrowed from nbconvert test. Assumes the class
        teardown will clean it up in the end."""
        super(BundleAPITest, cls).setup_class()
        nbdir = cls.notebook_dir

        nb = new_notebook()

        nb.cells.append(new_markdown_cell(u'Created by test'))
        cc1 = new_code_cell(source=u'print(2*6)')
        cc1.outputs.append(new_output(output_type="stream", text=u'12'))
        nb.cells.append(cc1)

        with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w', encoding='utf-8') as f:
            write(nb, f, version=4)
Exemple #41
0
def py_to_ipynb(path, py_filename):
    print('Converting {}'.format(os.path.join(path, filename)))

    ipynb_filename = py_filename.split('.')[0] + '.ipynb'

    with open(os.path.join(path, py_filename), 'r') as f:
        red = RedBaron(f.read())

    # detect whether the state has changed and thus need to flush the code up to that point before processing a node
    sources = []
    cell_source = []

    prev_type = red[0].type
    # concat_state = [False]
    for node in red[1:]:
        cur_type = node.type

        # ignore blank lines
        if cur_type == 'endl':
            continue

        if should_concat(prev_type, cur_type):
            cell_source.append(node.dumps())
        else:
            sources.append('\n'.join(cell_source))
            cell_source = [node.dumps()]

        prev_type = cur_type

    # last cell, special handling
    cell_source = []

    # the value of the IfNode 
    node_value = node.value[0].value

    # just include all the argparse lines
    for line in node_value:
        cell_source.append(line.dumps())

    sources.append('\n'.join(cell_source))

    # build cells and notebook
    cells = [new_code_cell(source=source) for source in sources if source]
    notebook = new_notebook(cells=cells)

    # output
    with open(os.path.join(path, ipynb_filename), 'w') as f:
        nbformat.write(notebook, f)
Exemple #42
0
    def test_run_nb_error(self):
        """Test %run notebook.ipynb error"""
        pytest.importorskip("nbformat")
        from nbformat import v4, writes

        # %run when a file name isn't provided
        pytest.raises(Exception, _ip.magic, "run")

        # %run when a file doesn't exist
        pytest.raises(Exception, _ip.magic, "run foobar.ipynb")

        # %run on a notebook with an error
        nb = v4.new_notebook(cells=[v4.new_code_cell("0/0")])
        src = writes(nb, version=4)
        self.mktmp(src, ext='.ipynb')
        pytest.raises(Exception, _ip.magic, "run %s" % self.fname)
Exemple #43
0
def create_locked_cell(source, cell_type, grade_id, schema_version=1):
    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["grade"] = False
    cell.metadata.nbgrader["schema_version"] = schema_version

    return cell
Exemple #44
0
def test_preprocessor_pymarkdown():
    """Test python markdown preprocessor."""
    # check import shortcut
    from jupyter_contrib_nbextensions.nbconvert_support import PyMarkdownPreprocessor  # noqa
    notebook_node = nbf.new_notebook(cells=[
        nbf.new_code_cell(source="a = 'world'"),
        nbf.new_markdown_cell(source="Hello {{ a }}",
                              metadata={"variables": {
                                  " a ": "world"
                              }}),
    ])
    body, resources = export_through_preprocessor(notebook_node,
                                                  PyMarkdownPreprocessor,
                                                  RSTExporter, 'rst')
    expected = 'Hello world'
    assert_in(expected, body, 'first cell should contain {}'.format(expected))
Exemple #45
0
 def test_empty_code_cell(self):
     """No empty code cells in rst"""
     nbname = self._get_notebook()
     with io.open(nbname, encoding='utf8') as f:
         nb = nbformat.read(f, 4)
     
     exporter = self.exporter_class()
     
     (output, resources) = exporter.from_notebook_node(nb)
     # add an empty code cell
     nb.cells.append(
         v4.new_code_cell(source="")
     )
     (output2, resources) = exporter.from_notebook_node(nb)
     # adding an empty code cell shouldn't change output
     self.assertEqual(output.strip(), output2.strip())
Exemple #46
0
def tester(import_list_dict):
    # http://nbviewer.ipython.org/gist/fperez/9716279
    for import_dict in import_list_dict:
        code = import_dict['code']
        target = import_dict['targets']
        nb = v4.new_notebook()
        cells = [v4.new_code_cell(code)]
        nb['cells'].extend(cells)
        fname = tempfile.NamedTemporaryFile(suffix='.ipynb').name
        with open(fname, 'w') as f:
            f.write(v4.writes(nb))

        # parse the notebook!
        assert target == depfinder.notebook_path_to_dependencies(fname)

        os.remove(fname)
Exemple #47
0
def test_lc_cell_meme_initialized_cells_with_meme(prefill_notebook):
    initial_codes = ['print("{}")'.format(i) for i in range(1000)]
    initial_cells = [new_code_cell(c) for c in initial_codes]
    for cell in initial_cells:
        cell['metadata']['lc_cell_meme'] = {
            'current': '0825aaee-48b9-11ea-9b81-0242ac120002'
        }
    notebook = prefill_notebook(initial_cells)

    metadata_list = get_cell_metadata_list(notebook)
    for metadata in metadata_list:
        assert_json(metadata, {
            'lc_cell_meme': {
                'current': '0825aaee-48b9-11ea-9b81-0242ac120002'
            }
        })
Exemple #48
0
 def test_job_put_get(self):
     """
     test job put and get
     """
     om = self.om
     # create a notebook
     cells = []
     code = "print 'hello'"
     cells.append(v4.new_code_cell(source=code))
     notebook = v4.new_notebook(cells=cells)
     # put the notebook
     meta = om.jobs.put(notebook, 'testjob')
     self.assertEqual(meta.name, 'testjob.ipynb')
     # read it back and see what's in it
     notebook2 = om.jobs.get('testjob')
     self.assertDictEqual(notebook2, notebook)
Exemple #49
0
def filter_input_cell(block_id, dag):
    """Input filter cell code. Imports dill and loads all of the parents' filtered vars."""
    source_str = '# DAGpy input filter\nimport dill'
    parents = dag.parents_of(block_id)
    for parent in parents:
        pfilter = dag.get_block_att(parent, 'filter', default=[])
        for var_name in pfilter:
            inblock_var_name = '{!s}_{!s}'.format(parent, var_name)
            fpathname = filter_file(var_name, parent, dag)
            source_str += "\n{} = dill.load(open('{!s}','rb'))".format(
                inblock_var_name, fpathname)
    return new_code_cell(
        source=source_str,
        metadata={'dagpy': {
            'cell_type': INPUT_FILTER_CELL_TYPE
        }})
Exemple #50
0
    def build_notebook(self):
        """Build a reveal slides notebook in memory for use with tests.
        Overrides base in PreprocessorTestsBase"""

        outputs = [
            nbformat.new_output(output_type='display_data',
                                data={'image/svg+xml': self.simple_svg})
        ]

        cells = [
            nbformat.new_code_cell(source="",
                                   execution_count=1,
                                   outputs=outputs)
        ]

        return nbformat.new_notebook(cells=cells)
    def test_coalesce_sequenced_streams(self):
        """Can the coalesce streams preprocessor merge a sequence of streams?"""
        outputs = [nbformat.new_output(output_type="stream", name="stdout", text="0"),
                   nbformat.new_output(output_type="stream", name="stdout", text="1"),
                   nbformat.new_output(output_type="stream", name="stdout", text="2"),
                   nbformat.new_output(output_type="stream", name="stdout", text="3"),
                   nbformat.new_output(output_type="stream", name="stdout", text="4"),
                   nbformat.new_output(output_type="stream", name="stdout", text="5"),
                   nbformat.new_output(output_type="stream", name="stdout", text="6"),
                   nbformat.new_output(output_type="stream", name="stdout", text="7")]
        cells=[nbformat.new_code_cell(source="# None", execution_count=1,outputs=outputs)]

        nb = nbformat.new_notebook(cells=cells)
        res = self.build_resources()
        nb, res = coalesce_streams(nb, res)
        outputs = nb.cells[0].outputs
        self.assertEqual(outputs[0].text, u'01234567')
    def test_coalesce_replace_streams(self):
        """Are \\r characters handled?"""
        outputs = [
            nbformat.new_output(output_type="stream", name="stdout", text="z"),
            nbformat.new_output(output_type="stream", name="stdout", text="\ra"),
            nbformat.new_output(output_type="stream", name="stdout", text="\nz\rb"),
            nbformat.new_output(output_type="stream", name="stdout", text="\nz"),
            nbformat.new_output(output_type="stream", name="stdout", text="\rc\n"),
            nbformat.new_output(output_type="stream", name="stdout", text="z\rz\rd"),
        ]
        cells = [nbformat.new_code_cell(source="# None", execution_count=1, outputs=outputs)]

        nb = nbformat.new_notebook(cells=cells)
        res = self.build_resources()
        nb, res = coalesce_streams(nb, res)
        outputs = nb.cells[0].outputs
        self.assertEqual(outputs[0].text, "a\nb\nc\nd")
Exemple #53
0
def test_preprocessor_embedimages():
    """Test python embedimages preprocessor."""
    # check import shortcut
    from jupyter_contrib_nbextensions.nbconvert_support import EmbedImagesPreprocessor  # noqa E501
    notebook_node = nbf.new_notebook(cells=[
        nbf.new_code_cell(source="a = 'world'"),
        nbf.new_markdown_cell(
            source="![testimage]({})".format(path_in_data('icon.png'))
        ),
    ])
    customconfig = Config(EmbedImagesPreprocessor={'embed_images': True})
    body, resources = export_through_preprocessor(
        notebook_node, EmbedImagesPreprocessor, NotebookExporter, 'ipynb',
        customconfig)

    expected = 'image/png'
    assert_in(expected, body, 'Attachment {} is missing'.format(expected))
Exemple #54
0
    def __init__(self, watcher, filename: str = None) -> None:
        self.filename = filename or \
            (path.splitext(watcher.filename)[0] + '.ipynb' if watcher.filename else \
            'tensorwatch.ipynb')

        self.cells = []
        self._default_vis_args = VisArgs()

        watcher_args_str = NotebookMaker._get_vis_args(watcher)

        # create initial cell
        self.cells.append(
            new_code_cell(source=linesep.join([
                '%matplotlib notebook', 'import tensorwatch as tw',
                'client = tw.WatcherClient({})'.format(
                    NotebookMaker._get_vis_args(watcher))
            ])))
Exemple #55
0
 def cells(self):
     for cell in self.nb.cells:
         if isinstance(cell, TextCell):
             yield new_markdown_cell(source=cell.input, )
         elif isinstance(cell, ComputeCell):
             yield new_code_cell(source=cell.input,
                                 execution_count=cell.index,
                                 outputs=[
                                     new_output(
                                         output_type=u'execute_result',
                                         data={
                                             'text/plain': cell.output,
                                         },
                                         execution_count=cell.index,
                                     )
                                 ])
         else:
             log.critical('unknown cell: {0}'.format(cell))
Exemple #56
0
    def get_ipynb_file(self):
        from nbformat.v4 import new_notebook, new_code_cell, new_markdown_cell
        from nbformat.v4.nbjson import JSONWriter

        json_writer = JSONWriter()

        nb = new_notebook()

        cell_names, edges, _ = self.get_all_cells_edges()
        cells = [self.get_cell(cn) for cn in cell_names]

        for c in cells:
            if c.content_type == "python":
                nb.cells.append(new_code_cell(c.content, metadata={'name': c.name}))
            else:
                nb.cells.append(new_markdown_cell(c.content, metadata={'name': c.name}))

        return json_writer.writes(nb)
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
Exemple #58
0
def python_to_ipython():

    """Create a notebook containing code from a script.
    Run as:  python make_nb.py my_script.py
    """
    import nbformat
    from nbformat.v4 import new_notebook, new_code_cell

    python_fileName = "visualize_topics.py"
    ipython_fileName ="topic_models.ipynb"

    nb = new_notebook()
    with open(python_fileName) as f:
        code = f.read()

    nb.cells.append(new_code_cell(code))
    nbformat.write(nb, ipython_fileName)
    print("IPython Notebook created Successfully") 
Exemple #59
0
 def test_job_list(self):
     """
     test job listing
     """
     fs = self.fs
     om = self.om
     # create a notebook
     cells = []
     code = "print 'hello'"
     cells.append(v4.new_code_cell(source=code))
     notebook = v4.new_notebook(cells=cells)
     # put the notebook
     meta = om.jobs.put(notebook, 'testjob')
     self.assertEqual(meta.name, 'testjob.ipynb')
     nb = v4.new_notebook(cells=cells)
     job_list = self.om.jobs.list()
     expected = 'testjob.ipynb'
     self.assertIn(expected, job_list)
Exemple #60
0
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