Пример #1
0
    def to_rich_html(
        self,
        row_cell_func=None,
        header_cell_func=None,
        element_formatters=None,
        merge_identical=False,
        compact=False,
    ):
        """returns just the table html code.

        Parameters
        ----------
        row_cell_func
            callback function that formats the row values. Must
            take the row value and coordinates (row index, column index).
        header_cell_func
            callback function that formats the column headings
            must take the header label value and coordinate
        element_formatters
            a dictionary of specific callback funcs for
            formatting individual html table elements.
            e.g. {'table': lambda x: '<table border="1" class="docutils">'}
        merge_identical
            cells within a row are merged to one span.

        """
        element_formatters = element_formatters or {}
        formatted_table = self.array.tolist()
        header, formatted_table = table_format.formatted_cells(
            formatted_table,
            self.header,
            digits=self._digits,
            column_templates=self._column_templates,
            missing_data=self._missing_data,
        )
        subtables = table_format.get_continuation_tables(
            header,
            formatted_table,
            identifiers=self._row_ids,
            max_width=self._max_width,
        )
        tables = []
        title = self.title if self.title else ""
        if title:
            title = escape(title)
        legend = self.legend if self.legend else ""
        if legend:
            legend = escape(legend)
        for i, (h, t) in enumerate(subtables):
            # but we strip the cell spacing
            sh = [v.strip() for v in h]
            t = [[c.strip() for c in r] for r in t]

            if title and i == 0:
                st = element_formatters.get(
                    "caption",
                    f'<span style="font-weight:bold">{title}</span>')
            elif title:
                st = element_formatters.get(
                    "caption",
                    f'<span style="font-weight:bold">continuation</span>')
            else:
                st = None

            legend = self.legend if self.legend else ""
            if legend and i == 0:
                title = f"{st} {legend}" if st else legend

            caption = st if st else None
            subtable = table_format.rich_html(
                t,
                row_cell_func=row_cell_func,
                header=sh,
                header_cell_func=header_cell_func,
                element_formatters=element_formatters,
                merge_identical=merge_identical,
                compact=compact,
                caption=caption,
            )
            tables.append(subtable)
        return "\n".join(tables)
Пример #2
0
    def _get_repr_(self, html=False):

        obs = self.observed.array.tolist()
        exp = self.expected.array.tolist()
        res = self.residuals.array.tolist()

        ndim = len(self.observed.shape)
        if ndim == 1:
            row_labels = "Observed", "Expected", "Residuals"
            row_cell_func = _format_row_cell(row_labels)
            col_labels = [str(c) for c in self.observed.template.names[0]]
            rows = []
            # format floats for expecteds and resid
            for row_label, row in zip(row_labels, [obs, exp, res]):
                if row_label == "Observed":
                    row = [row_label] + [f"{v:,}" for v in row]
                else:
                    row = [row_label] + [f"{v:,.2f}" for v in row]
                rows.append(row)

            if html:
                rows = rich_html(
                    rows,
                    header=[""] + col_labels,
                    row_cell_func=row_cell_func,
                    merge_identical=False,
                )
            else:
                header, rows = formatted_cells(rows, header=[""] + col_labels)
                rows = simple_format(header, rows)

        else:
            row_labels = self.observed.template.names[0]
            col_labels = self.observed.template.names[1]
            row_cell_func = _format_row_cell(row_labels)
            result = []
            for caption, table in zip(("Observed", "Expected", "Residuals"),
                                      (obs, exp, res)):
                rows = []
                for i, r in enumerate(table):
                    if caption == "Observed":
                        r = [f"{v:,}" for v in r]
                    else:
                        r = [f"{v:,.2f}" for v in r]
                    rows.append([row_labels[i]] + r)
                if html:
                    result.append(
                        rich_html(
                            rows,
                            header=[""] + col_labels,
                            caption=f"<b>{caption}</b>",
                            row_cell_func=row_cell_func,
                            merge_identical=False,
                        ))
                else:
                    header, rows = formatted_cells(rows,
                                                   header=[""] + col_labels)
                    result.append(simple_format(header, rows, title=caption))
            joiner = "<br>" if html else "\n"
            rows = joiner.join(result)
        return rows