Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

API: Add Styler.to_html, for saving output to HTML file #40312

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

Merged
merged 42 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
4799d86
json on styler
attack68 Sep 28, 2020
2eb5702
Merge remote-tracking branch 'upstream/master' into json_styler
attack68 Feb 24, 2021
a0bcbd1
meth: to_html
attack68 Feb 25, 2021
5135a91
Merge remote-tracking branch 'upstream/master' into json_styler
attack68 Mar 8, 2021
3e9903d
remove_json
attack68 Mar 8, 2021
766b758
html tests
attack68 Mar 8, 2021
e558a6a
html tests
attack68 Mar 8, 2021
00f8a6e
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 Mar 12, 2021
2c440ed
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 Mar 21, 2021
dd6785e
to_html parameters
attack68 Mar 22, 2021
e8d0629
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 Apr 1, 2021
42cefe6
remove formatting code fro to_html
attack68 Apr 1, 2021
80097cb
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 Apr 14, 2021
10f007c
reduce pr scope to simpler initial additions
attack68 Apr 14, 2021
c876bf6
move html tests to html
attack68 Apr 14, 2021
1e5061a
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 Apr 15, 2021
130fba4
use dedent
attack68 Apr 15, 2021
5079922
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 Apr 18, 2021
339ca37
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 Apr 20, 2021
c701d3d
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 Apr 21, 2021
3f074aa
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 Apr 22, 2021
e1818f2
refactor template inheritance
attack68 Apr 23, 2021
eeeaf54
template inheritance
attack68 Apr 23, 2021
b14e7af
improve tests
attack68 Apr 24, 2021
2a48f4d
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 Apr 26, 2021
dda33de
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 Apr 26, 2021
b55db71
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 Apr 27, 2021
a5c0ec7
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 Apr 29, 2021
1d3ba20
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 May 1, 2021
4e4c76d
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 May 6, 2021
f43961d
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 May 7, 2021
a3ce1ef
make keyword only: simialar to Styler.to_latex()
attack68 May 7, 2021
8899fb5
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 May 9, 2021
8375f99
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 May 14, 2021
f5b84fa
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 May 17, 2021
66ba741
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 May 18, 2021
d2e2731
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 May 20, 2021
792ab44
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 May 22, 2021
2575058
doc comments
attack68 May 22, 2021
c0bc29c
change default doctyoe_html from True to False
attack68 May 22, 2021
489030f
Merge remote-tracking branch 'upstream/master' into styler_to_html
attack68 May 24, 2021
2d996ce
whats new
attack68 May 24, 2021
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
Prev Previous commit
Next Next commit
improve tests
  • Loading branch information
attack68 committed Apr 24, 2021
commit b14e7afa8d3f5abe6b95ca62d5df753eca9922b0
4 changes: 2 additions & 2 deletions 4 pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,14 @@ def to_html(
self.set_table_attributes(table_attributes)

# Build HTML string..
styler_html = self.render(
html = self.render(
exclude_styles=exclude_styles,
encoding=encoding if encoding else "utf-8",
doctype_html=doctype_html,
)

return save_to_buffer(
styler_html, buf=buf, encoding=(encoding if buf is not None else None)
html, buf=buf, encoding=(encoding if buf is not None else None)
)

def set_td_classes(self, classes: DataFrame) -> Styler:
Expand Down
73 changes: 42 additions & 31 deletions 73 pandas/tests/io/formats/style/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
env = jinja2.Environment(loader=loader, trim_blocks=True)


@pytest.fixture
def styler():
return Styler(DataFrame([[2.61], [2.69]], index=["a", "b"], columns=["A"]))


@pytest.fixture
def tpl_style():
return env.get_template("html_style.tpl")
Expand All @@ -30,9 +35,8 @@ def test_html_template_extends_options():
assert '{% include "html_table.tpl" %}' in result


def test_exclude_styles():
s = Styler(DataFrame([[2.61], [2.69]], index=["a", "b"], columns=["A"]))
result = s.to_html(exclude_styles=True)
def test_exclude_styles(styler):
result = styler.to_html(exclude_styles=True)
expected = dedent(
"""\
<!DOCTYPE html>
Expand Down Expand Up @@ -66,30 +70,29 @@ def test_exclude_styles():
assert result == expected


def test_w3_html_format():
s = (
Styler(
DataFrame([[2.61], [2.69]], index=["a", "b"], columns=["A"]),
uuid_len=0,
)
.set_table_styles([{"selector": "th", "props": "att2:v2;"}])
.applymap(lambda x: "att1:v1;")
.set_table_attributes('class="my-cls1" style="attr3:v3;"')
.set_td_classes(DataFrame(["my-cls2"], index=["a"], columns=["A"]))
.format("{:.1f}")
.set_caption("A comprehensive test")
def test_w3_html_format(styler):
styler.set_uuid("").set_table_styles(
[{"selector": "th", "props": "att2:v2;"}]
).applymap(lambda x: "att1:v1;").set_table_attributes(
'class="my-cls1" style="attr3:v3;"'
).set_td_classes(
DataFrame(["my-cls2"], index=["a"], columns=["A"])
).format(
"{:.1f}"
).set_caption(
"A comprehensive test"
)
expected = dedent(
"""\
<style type="text/css">
#T__ th {
#T_ th {
att2: v2;
}
#T__row0_col0, #T__row1_col0 {
#T_row0_col0, #T_row1_col0 {
att1: v1;
}
</style>
<table id="T__" class="my-cls1" style="attr3:v3;">
<table id="T_" class="my-cls1" style="attr3:v3;">
<caption>A comprehensive test</caption>
<thead>
<tr>
Expand All @@ -99,41 +102,41 @@ def test_w3_html_format():
</thead>
<tbody>
<tr>
<th id="T__level0_row0" class="row_heading level0 row0" >a</th>
<td id="T__row0_col0" class="data row0 col0 my-cls2" >2.6</td>
<th id="T_level0_row0" class="row_heading level0 row0" >a</th>
<td id="T_row0_col0" class="data row0 col0 my-cls2" >2.6</td>
</tr>
<tr>
<th id="T__level0_row1" class="row_heading level0 row1" >b</th>
<td id="T__row1_col0" class="data row1 col0" >2.7</td>
<th id="T_level0_row1" class="row_heading level0 row1" >b</th>
<td id="T_row1_col0" class="data row1 col0" >2.7</td>
</tr>
</tbody>
</table>
"""
)
assert expected == s.render()
assert expected == styler.render()


def test_colspan_w3():
# GH 36223
df = DataFrame(data=[[1, 2]], columns=[["l0", "l0"], ["l1a", "l1b"]])
s = Styler(df, uuid="_", cell_ids=False)
assert '<th class="col_heading level0 col0" colspan="2">l0</th>' in s.render()
styler = Styler(df, uuid="_", cell_ids=False)
assert '<th class="col_heading level0 col0" colspan="2">l0</th>' in styler.render()


def test_rowspan_w3():
# GH 38533
df = DataFrame(data=[[1, 2]], index=[["l0", "l0"], ["l1a", "l1b"]])
s = Styler(df, uuid="_", cell_ids=False)
styler = Styler(df, uuid="_", cell_ids=False)
assert (
'<th id="T___level0_row0" class="row_heading '
'level0 row0" rowspan="2">l0</th>' in s.render()
'level0 row0" rowspan="2">l0</th>' in styler.render()
)


def test_styles():
s = Styler(DataFrame([[2.61], [2.69]], index=["a", "b"], columns=["A"]), uuid="abc")
s.set_table_styles([{"selector": "td", "props": "color: red;"}])
result = s.to_html()
def test_styles(styler):
styler.set_uuid("abc_")
styler.set_table_styles([{"selector": "td", "props": "color: red;"}])
result = styler.to_html()
expected = dedent(
"""\
<!DOCTYPE html>
Expand Down Expand Up @@ -172,6 +175,14 @@ def test_styles():
assert result == expected


def test_doctype(styler):
result = styler.to_html(doctype_html=False)
assert "<html>" not in result
assert "<body>" not in result
assert "<!DOCTYPE html>" not in result
assert "<head>" not in result


def test_block_names(tpl_style, tpl_table):
# catch accidental removal of a block
expected_style = {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.