示例#1
0
def test_root():
    el = h.root()
    assert str(el) == '<html></html>'

    el = h.root('script')
    assert str(el) == '<script></script>'

    el = h.root('tag', 'doc')
    assert str(el.soup) == '<!DOCTYPE doc>\n<tag></tag>'
示例#2
0
def test_construct_element():
    el = h.construct_element(container=None, content='content', tag='tag', element_type='text/tag')
    assert str(el) == '<tag type="text/tag">content</tag>'

    p = h.root()
    el = h.construct_element(container=p, content='content', tag='tag', element_type='text/tag')
    assert str(p) == '<html><tag type="text/tag">content</tag></html>'
示例#3
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
示例#4
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
示例#5
0
    def write(self, parent=None):
        if parent is None:
            el = root("style")
        else:
            el = append_to(parent, "style")

        el["type"] = "text/css"

        if self._tag_id is not None:
            el["id"] = self._tag_id

        with open(self._local_path, "r") as f:
            el.string = f.read()

        return el
示例#6
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()
示例#7
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
示例#8
0
def test_append_to():
    p = h.root()
    h.append_to(p, 'script')
    assert str(p) == '<html><script></script></html>'