Beispiel #1
0
    def _write_plot(self, container, container_id, id_gen, static_output):
        """
        Write out the chart construction machinery.
        """
        js_timer_var_name = "_ins_timer_" + next(id_gen)

        # Plumbing to make sure script rendering waits until the container element is ready
        stream = StringIO()

        chart_cfg = self._chart_cfg.override(Chart(render_to=container_id))

        if static_output:
            # Chart load wait handles for static output.
            stream.write("registerWaitHandle('%s');" % container_id)
            overrides = [
                Chart(
                    Events(load=Expr("function(){setLoaded('%s');}" %
                                     container_id))),
                Exporting(enabled=False),
                Navigator(enabled=False),
                Scrollbar(enabled=False),
                PlotOptions(
                    Series(enable_mouse_tracking=False,
                           shadow=False,
                           animation=False)),
                RangeSelector(enabled=False)
            ]

            chart_cfg = chart_cfg.override_many(*overrides)

        stream.write(("var %s=setInterval(function(){"
                      "var container=document.getElementById('%s');"
                      "if(container){clearInterval(%s);") %
                     (js_timer_var_name, container_id, js_timer_var_name))

        # Write out the chart script into a separate buffer before running it through
        # the encoding/compression
        chart_buf = StringIO()
        chart_buf.write("var cfg=")
        self._write_dict(chart_buf, chart_cfg)
        chart_buf.write(";")

        chart_buf.write("var chart = new Highcharts." + self._chart_cls +
                        "(cfg);")

        self._write_plot_postprocess(chart_buf)

        JScript.write_compressed(stream, chart_buf.getvalue())

        stream.write("}},10);")

        js_elem(container, stream.getvalue())
Beispiel #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
Beispiel #3
0
    def write(self, parent=None):
        stream = StringIO()

        # Write out init condition
        stream.write("if(typeof(%s) == 'undefined'){" %
                     self._sentinel_var_name)

        with open(self._local_path, "rb") as f:
            if self._encode:
                self.write_compressed(stream, f.read())
            else:
                stream.write(f.read().decode('utf-8'))

        stream.write("%s = true;" % self._sentinel_var_name)

        stream.write("}")

        return js_elem(parent, stream.getvalue())
Beispiel #4
0
    def write(self, parent=None):
        stream = StringIO()

        # Wrapper to make accidental multiple inclusion if the same code (e.g. with different file names) safe to load.
        sentinel_var_name = "_pybloqs_load_sentinel_{}".format(
            self.name.replace("-", "_"))
        stream.write(
            "if(typeof({}) == 'undefined'){{".format(sentinel_var_name))

        if self.encode:
            self.write_compressed(stream, self.content_string)
        else:
            stream.write(self.content_string)

        # Second part of wrapper
        stream.write("{} = true;".format(sentinel_var_name))
        stream.write("}")

        return js_elem(parent, stream.getvalue())
Beispiel #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
Beispiel #6
0
def test_js_elem():
    el = h.js_elem(None, 'test_script')
    assert str(el) == '<script type="text/javascript">test_script</script>'