Exemple #1
0
def test_df_with_many_columns_is_downsampled_preferentially_on_columns(
        df, max_bytes):
    dn = downsample(df, max_bytes=max_bytes)
    if max_bytes == 1e5:
        assert len(dn.index) == len(
            df.index) and len(dn.columns) < len(df.columns)
    else:
        # aspect ratio is close to 1
        assert 0.5 < len(dn.index) / len(dn.columns) < 2
Exemple #2
0
def _datatables_repr_(df=None, tableId=None, **kwargs):
    """Return the HTML/javascript representation of the table"""

    # Default options
    for option in dir(opt):
        if option not in kwargs and not option.startswith("__"):
            kwargs[option] = getattr(opt, option)

    # These options are used here, not in DataTable
    classes = kwargs.pop("classes")
    showIndex = kwargs.pop("showIndex")
    maxBytes = kwargs.pop("maxBytes", 0)
    maxRows = kwargs.pop("maxRows", 0)
    maxColumns = kwargs.pop("maxColumns", pd.get_option("display.max_columns") or 0)

    if isinstance(df, (np.ndarray, np.generic)):
        df = pd.DataFrame(df)

    if isinstance(df, pd.Series):
        df = df.to_frame()

    df = downsample(df, max_rows=maxRows, max_columns=maxColumns, max_bytes=maxBytes)

    # Do not show the page menu when the table has fewer rows than min length menu
    if "paging" not in kwargs and len(df.index) <= kwargs.get("lengthMenu", [10])[0]:
        kwargs["paging"] = False

    tableId = tableId or str(uuid.uuid4())
    if isinstance(classes, list):
        classes = " ".join(classes)

    if showIndex == "auto":
        showIndex = df.index.name is not None or not isinstance(df.index, pd.RangeIndex)

    if not showIndex:
        df = df.set_index(pd.RangeIndex(len(df.index)))

    # Generate table head using pandas.to_html()
    pattern = re.compile(r".*<thead>(.*)</thead>", flags=re.MULTILINE | re.DOTALL)
    match = pattern.match(df.head(0).to_html())
    thead = match.groups()[0]
    if not showIndex:
        thead = thead.replace("<th></th>", "", 1)
    html_table = (
        '<table id="'
        + tableId
        + '" class="'
        + classes
        + '"><thead>'
        + thead
        + "</thead></table>"
    )

    kwargs["data"] = _formatted_values(df.reset_index() if showIndex else df)

    try:
        dt_args = json.dumps(kwargs)
        return (
            """<div>"""
            + html_table
            + """
<script type="text/javascript">
require(["datatables"], function (datatables) {
    $(document).ready(function () {
        var dt_args = """
            + dt_args
            + """;
        dt_args = eval_functions(dt_args);
        var el = $('#"""
            + tableId
            + """')
        if ($.fn.dataTable.isDataTable(el)) {
            table = el.DataTable();
            table.destroy();
        }
        table = el.DataTable(dt_args);
    });
})
</script>
</div>
"""
        )
    except TypeError as error:
        logger.error(str(error))
        return ""
Exemple #3
0
def test_max_one_byte(df, max_bytes=1):
    dn = downsample(df, max_bytes=max_bytes)
    assert len(dn.columns) == len(dn.index) == 1
    assert dn.iloc[0, 0] == "..."
Exemple #4
0
def test_max_bytes(df, max_bytes):
    dn = downsample(df, max_bytes=max_bytes)
    assert dn.values.nbytes <= max_bytes
    assert dn.values.nbytes > max_bytes / 2
Exemple #5
0
def test_max_columns(df, max_columns):
    dn = downsample(df, max_columns=max_columns)
    pd.testing.assert_index_equal(dn.index, df.index)
    assert len(dn.columns) == max_columns
Exemple #6
0
def test_max_rows(df, max_rows):
    dn = downsample(df, max_rows=max_rows)
    assert len(dn.index) == max_rows
    pd.testing.assert_index_equal(dn.columns, df.columns)