Esempio n. 1
0
def test_cell_output_to_nodes(doctree):
    # tests the image uri paths on conversion to docutils image nodes
    priority =  ['image/png', 'image/jpeg', 'text/latex', 'text/plain']
    output_dir = '/_build/jupyter_execute'
    img_locs = ['/_build/jupyter_execute/docs/image_1.png','/_build/jupyter_execute/image_2.png']

    cells = [
        {'outputs':
            [{'data': {'image/png': 'Vxb6L1wAAAABJRU5ErkJggg==\n', 'text/plain': '<Figure size 432x288 with 1 Axes>'}, 'metadata': {'filenames': {'image/png': img_locs[0]}}, 'output_type': 'display_data'}]
        },
        {'outputs':
            [{'data': {'image/png': 'iVBOJggg==\n', 'text/plain': '<Figure size 432x288 with 1 Axes>'}, 'metadata': {'filenames': {'image/png': img_locs[1]}}, 'output_type': 'display_data'}]
        }]

    for index, cell in enumerate(cells):
        cell = from_dict(cell)
        output_node = cell_output_to_nodes(cell["outputs"], priority, True, output_dir, None)
        assert output_node[0].attributes['uri'] == img_locs[index]

    # Testing inline functionality
    outputs = [
        {"name": "stdout", "output_type": "stream", "text": ["hi\n"]},
        {"name": "stderr", "output_type": "stream", "text": ["hi\n"]},
    ]
    output_nodes = cell_output_to_nodes(outputs, priority, True, output_dir, None)
    for output, kind in zip(output_nodes, [literal_block, container]):
        assert isinstance(output, kind)

    output_nodes = cell_output_to_nodes(outputs, priority, True, output_dir, None, inline=True)
    for output, kind in zip(output_nodes, [literal, literal]):
        assert isinstance(output, kind)
Esempio n. 2
0
    def apply(self):
        builder = self.app.builder.name
        output_dir = sphinx_abs_dir(self.env)
        for node in self.document.traverse(CellOutputBundleNode):
            cell = {"outputs": node.outputs}
            outputs = cell.get("outputs", [])
            if node.get("inline", False):
                output_nodes = cell_output_to_nodes_inline(
                    outputs, RENDER_PRIORITY.get(builder, 'html'), True,
                    output_dir, None)
            else:
                output_nodes = cell_output_to_nodes(
                    outputs, RENDER_PRIORITY.get(builder, 'html'), True,
                    output_dir, None)
            # TODO add warning if output_nodes is empty
            node.replace_self(output_nodes)

        # Image collect extra nodes from cell outputs that we need to process
        for node in self.document.traverse(nodes.image):
            # If the image node has `candidates` then it's already been processed
            # as in-line markdown, so skip it
            if "candidates" in node:
                continue
            col = ImageCollector()
            col.process_doc(self.app, node)
Esempio n. 3
0
def cell_output_to_nodes(outputs, data_priority, write_stderr, dir,
                         thebe_config):
    from jupyter_sphinx.ast import cell_output_to_nodes

    to_add = []
    for _, output in enumerate(outputs):
        if output["output_type"] == "display_data":
            if "text/markdown" in output["data"]:
                continue  # because we had to deal with the markdown earlier on.
        to_add.extend(
            cell_output_to_nodes(outputs, data_priority, write_stderr, dir,
                                 thebe_config))
    return to_add
Esempio n. 4
0
def test_image_mimetype_uri(doctree):
    # tests the image uri paths on conversion to docutils image nodes
    priority = ['image/png', 'image/jpeg', 'text/latex', 'text/plain']
    output_dir = '/_build/jupyter_execute'
    img_locs = [
        '/_build/jupyter_execute/docs/image_1.png',
        '/_build/jupyter_execute/image_2.png'
    ]

    cells = [{
        'outputs': [{
            'data': {
                'image/png': 'Vxb6L1wAAAABJRU5ErkJggg==\n',
                'text/plain': '<Figure size 432x288 with 1 Axes>'
            },
            'metadata': {
                'filenames': {
                    'image/png': img_locs[0]
                }
            },
            'output_type': 'display_data'
        }]
    }, {
        'outputs': [{
            'data': {
                'image/png': 'iVBOJggg==\n',
                'text/plain': '<Figure size 432x288 with 1 Axes>'
            },
            'metadata': {
                'filenames': {
                    'image/png': img_locs[1]
                }
            },
            'output_type': 'display_data'
        }]
    }]

    for index, cell in enumerate(cells):
        cell = from_dict(cell)
        output_node = cell_output_to_nodes(cell, priority, True, output_dir,
                                           None)
        assert output_node[0].attributes['uri'] == img_locs[index]