Example #1
0
def test_render():
    el = h.root()
    h.append_to(el, 'tag1')
    output = h.render(el, pretty=False)
    # Non-pretty output should not have line breaks
    assert output.find('\n') < 0

    output = h.render(el, pretty=True)
    assert output.find('\n') > 0
Example #2
0
    def render_html(self,
                    pretty=True,
                    static_output=False,
                    header_block=None,
                    footer_block=None,
                    pdf_page_size="A4"):
        """Returns html output of the block
        :param pretty: Toggles pretty printing of the resulting HTML. Not applicable for non-HTML output.
        :return html-code of the block
        """
        # Render the contents
        html = root("html", doctype="html")
        head = append_to(html, "head")
        head = append_to(head, "meta", charset='utf-8')
        body = append_to(html, "body")

        # Make sure that the main style sheet is always included
        resource_deps = DependencyTracker(default_css_main)
        if header_block is not None:
            header_block._write_block(body,
                                      Cfg(),
                                      id_generator(),
                                      resource_deps=resource_deps,
                                      static_output=static_output)

        self._write_block(body,
                          Cfg(),
                          id_generator(),
                          resource_deps=resource_deps,
                          static_output=static_output)

        if footer_block is not None:
            footer_block._write_block(body,
                                      Cfg(),
                                      id_generator(),
                                      resource_deps=resource_deps,
                                      static_output=static_output)

        script_inflate.write(head)
        script_block_core.write(head)

        if static_output:
            # Add the load wait poller if there are any JS resources
            js_elem(body, "var loadWaitPoller=runWaitPoller();")

        # Write out resources
        for res in resource_deps:
            res.write(head)

        # Render the whole document (the parent of the html tag)
        content = render(html.parent, pretty=pretty)
        return content
Example #3
0
    def data(self):
        """
        Function required to support interactive IPython plotting.

        Should not be used directly.

        :return: Data to be displayed
        """
        container = root("div")
        self._write_block(container, Cfg(), id_generator())

        # Write children into the output
        output = BytesIO()

        for child in container.children:
            output.write(render(child))

        return output.getvalue()
Example #4
0
def write_interactive(stream):
    for script in _registered_resources:
        stream.write(render(script.write(), pretty=False))
Example #5
0
    def render_html(self,
                    pretty=True,
                    static_output=False,
                    header_block=None,
                    footer_block=None):
        """Returns html output of the block
        :param pretty: Toggles pretty printing of the resulting HTML. Not applicable for non-HTML output.
        :param static_output: Passed down to _write_block. Will render static version of blocks which support this.
        :param header_block: If not None, header is inlined into a HTML body as table.
        :param footer_block: If not None, header is inlined into a HTML body as table.
        :return html-code of the block
        """
        # Render the contents
        html = root("html", doctype="html")
        head = append_to(html, "head")
        append_to(head, "meta", charset='utf-8')

        body = append_to(html, "body")

        # Make sure that the main style sheet is always included
        resource_deps = DependencyTracker(default_css_main)

        # If header or footer are passed into this function, inline them in the following structure:
        #
        # <body>
        # <table>
        #    <thead><tr><td>Header html</td></tr></thead>
        #    <tfoot><tr><td>Footer html</td></tr></tfoot>
        #    <tbody><tr><td>Body html</td></tr></tbody>
        # </table>
        # </body>
        if header_block is not None or footer_block is not None:
            content_table = append_to(body, "table")
            if header_block is not None:
                header_thead = append_to(content_table, "thead")
                header_tr = append_to(header_thead, "tr")
                header_td = append_to(header_tr, "th")
                header_block._write_block(header_td,
                                          Cfg(),
                                          id_generator(),
                                          resource_deps=resource_deps,
                                          static_output=static_output)

            if footer_block is not None:
                footer_tfoot = append_to(content_table, "tfoot", id='footer')
                footer_tr = append_to(footer_tfoot, "tr")
                footer_td = append_to(footer_tr, "td")
                footer_block._write_block(footer_td,
                                          Cfg(),
                                          id_generator(),
                                          resource_deps=resource_deps,
                                          static_output=static_output)

            body_tbody = append_to(content_table, "tbody")
            body_tr = append_to(body_tbody, "tr")
            body_td = append_to(body_tr, "td")
            self._write_block(body_td,
                              Cfg(),
                              id_generator(),
                              resource_deps=resource_deps,
                              static_output=static_output)
        else:
            self._write_block(body,
                              Cfg(),
                              id_generator(),
                              resource_deps=resource_deps,
                              static_output=static_output)

        script_inflate.write(head)
        script_block_core.write(head)

        if static_output:
            # Add the load wait poller if there are any JS resources
            js_elem(body, "var loadWaitPoller=runWaitPoller();")

        # Write out resources
        for res in resource_deps:
            res.write(head)

        # Render the whole document (the parent of the html tag)
        content = render(html.parent, pretty=pretty)
        return content