Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set reasonable defaults for pandas dataframe default renderer #1238

Merged
merged 2 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions flytekit/deck/renderer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Optional
from typing import Any

import pandas
import pyarrow
Expand All @@ -14,17 +14,22 @@ def to_html(self, python_value: Any) -> str:
raise NotImplementedError


DEFAULT_MAX_ROWS = 10
DEFAULT_MAX_COLS = 100


class TopFrameRenderer:
"""
Render a DataFrame as an HTML table.
"""

def __init__(self, max_rows: Optional[int] = None):
def __init__(self, max_rows: int = DEFAULT_MAX_ROWS, max_cols: int = DEFAULT_MAX_COLS):
self._max_rows = max_rows
self._max_cols = max_cols

def to_html(self, df: pandas.DataFrame) -> str:
assert isinstance(df, pandas.DataFrame)
return df.to_html(max_rows=self._max_rows)
return df.to_html(max_rows=self._max_rows, max_cols=self._max_cols)


class ArrowRenderer:
Expand Down
29 changes: 25 additions & 4 deletions tests/flytekit/unit/deck/test_renderer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import pandas as pd
import pyarrow as pa
import pytest

from flytekit.deck.renderer import ArrowRenderer, TopFrameRenderer
from flytekit.deck.renderer import DEFAULT_MAX_COLS, DEFAULT_MAX_ROWS, ArrowRenderer, TopFrameRenderer


def test_renderer():
df = pd.DataFrame({"Name": ["Tom", "Joseph"], "Age": [1, 22]})
@pytest.mark.parametrize(
"rows, cols, max_rows, expected_max_rows, max_cols, expected_max_cols",
[
(1, 1, None, DEFAULT_MAX_ROWS, None, DEFAULT_MAX_COLS),
(10, 1, None, DEFAULT_MAX_ROWS, None, DEFAULT_MAX_COLS),
(1, 10, None, DEFAULT_MAX_ROWS, None, DEFAULT_MAX_COLS),
(DEFAULT_MAX_ROWS + 1, 10, None, DEFAULT_MAX_ROWS, None, DEFAULT_MAX_COLS),
(1, DEFAULT_MAX_COLS + 1, None, DEFAULT_MAX_ROWS, None, DEFAULT_MAX_COLS),
(10, DEFAULT_MAX_COLS + 1, None, DEFAULT_MAX_ROWS, None, DEFAULT_MAX_COLS),
(DEFAULT_MAX_ROWS + 1, DEFAULT_MAX_COLS + 1, None, DEFAULT_MAX_ROWS, None, DEFAULT_MAX_COLS),
(100_000, 10, 123, 123, 5, 5),
(10_000, 1000, DEFAULT_MAX_ROWS, DEFAULT_MAX_ROWS, DEFAULT_MAX_COLS, DEFAULT_MAX_COLS),
],
)
def test_renderer(rows, cols, max_rows, expected_max_rows, max_cols, expected_max_cols):
df = pd.DataFrame({f"abc-{k}": list(range(rows)) for k in range(cols)})
pa_df = pa.Table.from_pandas(df)

assert TopFrameRenderer().to_html(df) == df.to_html()
kwargs = {}
if max_rows is not None:
kwargs["max_rows"] = max_rows
if max_cols is not None:
kwargs["max_cols"] = max_cols

assert TopFrameRenderer(**kwargs).to_html(df) == df.to_html(max_rows=expected_max_rows, max_cols=expected_max_cols)
assert ArrowRenderer().to_html(pa_df) == pa_df.to_string()