Skip to content

Commit

Permalink
[CHORE] Left-align headers in reprs (#1880)
Browse files Browse the repository at this point in the history
Co-authored-by: Jay Chia <[email protected]@users.noreply.github.com>
  • Loading branch information
jaychia and Jay Chia authored Feb 14, 2024
1 parent 195174c commit cf77fd2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/daft-core/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ impl Schema {
res.push_str("<thead><tr>");

for (name, field) in &self.fields {
res.push_str("<th style=\"text-wrap: nowrap; max-width:192px; overflow:auto\">");
res.push_str(
"<th style=\"text-wrap: nowrap; max-width:192px; overflow:auto; text-align:left\">",
);
res.push_str(&html_escape::encode_text(name));
res.push_str("<br />");
res.push_str(&html_escape::encode_text(&format!("{}", field.dtype)));
Expand Down
4 changes: 3 additions & 1 deletion src/daft-table/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,9 @@ impl Table {
res.push_str("<thead><tr>");

for (name, field) in &self.schema.fields {
res.push_str("<th style=\"text-wrap: nowrap; max-width:192px; overflow:auto\">");
res.push_str(
"<th style=\"text-wrap: nowrap; max-width:192px; overflow:auto; text-align:left\">",
);
res.push_str(&html_escape::encode_text(name));
res.push_str("<br />");
res.push_str(&html_escape::encode_text(&format!("{}", field.dtype)));
Expand Down
21 changes: 11 additions & 10 deletions tests/dataframe/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
UNMATERIALIZED_REGEX = re.compile(r".*\(No data to display: Dataframe not materialized\).*")
MATERIALIZED_NO_ROWS_REGEX = re.compile(r".*\(No data to display: Materialized dataframe has no rows\).*")
TD_STYLE = 'style="text-align:left; max-width:192px; max-height:64px; overflow:auto"'
TH_STYLE = 'style="text-wrap: nowrap; max-width:192px; overflow:auto; text-align:left"'


def parse_str_table(
Expand Down Expand Up @@ -93,9 +94,9 @@ def test_empty_df_repr(make_df):
assert parse_str_table(df.__repr__(), expected_user_msg_regex=UNMATERIALIZED_REGEX) == expected_data
assert (
df._repr_html_()
== """<div>
== f"""<div>
<table class="dataframe">
<thead><tr><th style="text-wrap: nowrap; max-width:192px; overflow:auto">A<br />Int64</th><th style="text-wrap: nowrap; max-width:192px; overflow:auto">B<br />Utf8</th></tr></thead>
<thead><tr><th {TH_STYLE}>A<br />Int64</th><th {TH_STYLE}>B<br />Utf8</th></tr></thead>
</table>
<small>(No data to display: Dataframe not materialized)</small>
</div>"""
Expand All @@ -115,9 +116,9 @@ def test_empty_df_repr(make_df):
assert parse_str_table(df.__repr__(), expected_user_msg_regex=MATERIALIZED_NO_ROWS_REGEX) == expected_data
assert (
df._repr_html_()
== """<div>
== f"""<div>
<table class="dataframe">
<thead><tr><th style="text-wrap: nowrap; max-width:192px; overflow:auto">A<br />Int64</th><th style="text-wrap: nowrap; max-width:192px; overflow:auto">B<br />Utf8</th></tr></thead>
<thead><tr><th {TH_STYLE}>A<br />Int64</th><th {TH_STYLE}>B<br />Utf8</th></tr></thead>
<tbody>
</tbody>
</table>
Expand All @@ -134,9 +135,9 @@ def test_alias_repr(make_df):
assert parse_str_table(df.__repr__(), expected_user_msg_regex=UNMATERIALIZED_REGEX) == expected_data
assert (
df._repr_html_()
== """<div>
== f"""<div>
<table class="dataframe">
<thead><tr><th style="text-wrap: nowrap; max-width:192px; overflow:auto">A2<br />Int64</th><th style="text-wrap: nowrap; max-width:192px; overflow:auto">B<br />Utf8</th></tr></thead>
<thead><tr><th {TH_STYLE}>A2<br />Int64</th><th {TH_STYLE}>B<br />Utf8</th></tr></thead>
</table>
<small>(No data to display: Dataframe not materialized)</small>
</div>"""
Expand All @@ -162,7 +163,7 @@ def test_alias_repr(make_df):
df._repr_html_()
== f"""<div>
<table class="dataframe">
<thead><tr><th style="text-wrap: nowrap; max-width:192px; overflow:auto">A2<br />Int64</th><th style="text-wrap: nowrap; max-width:192px; overflow:auto">B<br />Utf8</th></tr></thead>
<thead><tr><th {TH_STYLE}>A2<br />Int64</th><th {TH_STYLE}>B<br />Utf8</th></tr></thead>
<tbody>
<tr><td><div {TD_STYLE}>1</div></td><td><div {TD_STYLE}>a</div></td></tr>
<tr><td><div {TD_STYLE}>2</div></td><td><div {TD_STYLE}>b</div></td></tr>
Expand All @@ -189,15 +190,15 @@ def test_repr_with_unicode(make_df, data_source):
}

string_array = ["🔥a", "b🔥", "🦁🔥" * 60] # we dont truncate for html
expected_html_unmaterialized = """<div>
expected_html_unmaterialized = f"""<div>
<table class="dataframe">
<thead><tr><th style="text-wrap: nowrap; max-width:192px; overflow:auto">🔥<br />Int64</th><th style="text-wrap: nowrap; max-width:192px; overflow:auto">🦁<br />Utf8</th></tr></thead>
<thead><tr><th {TH_STYLE}>🔥<br />Int64</th><th {TH_STYLE}>🦁<br />Utf8</th></tr></thead>
</table>
<small>(No data to display: Dataframe not materialized)</small>
</div>"""
expected_html_materialized = f"""<div>
<table class="dataframe">
<thead><tr><th style="text-wrap: nowrap; max-width:192px; overflow:auto">🔥<br />Int64</th><th style="text-wrap: nowrap; max-width:192px; overflow:auto">🦁<br />Utf8</th></tr></thead>
<thead><tr><th {TH_STYLE}>🔥<br />Int64</th><th {TH_STYLE}>🦁<br />Utf8</th></tr></thead>
<tbody>
<tr><td><div {TD_STYLE}>1</div></td><td><div {TD_STYLE}>{string_array[0]}</div></td></tr>
<tr><td><div {TD_STYLE}>2</div></td><td><div {TD_STYLE}>{string_array[1]}</div></td></tr>
Expand Down

0 comments on commit cf77fd2

Please sign in to comment.