8000 API: Add `Styler.to_html`, for saving output to HTML file by attack68 · Pull Request #40312 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

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
8000 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
8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor template inheritance
  • Loading branch information
attack68 committed Apr 23, 2021
commit e1818f277da672640369eef0ba4680055e6ac558
21 changes: 6 additions & 15 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,23 +39 8000 4,14 @@ def to_html(
self.set_table_attributes(table_attributes)

# Build HTML string..
styler_html = self.render(exclude_styles=exclude_styles).split("</style>\n")
if exclude_styles:
style, body = "", styler_html[0]
else:
style, body = "\n " + styler_html[0] + " </style>", styler_html[1]

if doctype_html:
html = (
f'<!DOCTYPE html>\n<html>\n<head>\n <meta charset="'
f'{encoding if encoding is not None else "utf-8"}">'
f"{style}\n</head>\n<body>\n{body}</body>\n</html>\n"
)
else:
html = style + body
styler_html = self.render(
exclude_styles=exclude_styles,
encoding=encoding if encoding else "utf-8",
doctype_html=doctype_html,
)

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

def set_td_classes(self, classes: DataFrame) -> Styler:
Expand Down
18 changes: 16 additions & 2 deletions pandas/io/formats/templates/html.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
{# Update the template_structure.html documentation too #}
{% if doctype_html %}
<!DOCTYPE html>
<html>
<head>
<meta charset="{{encoding}}">
{% if not exclude_styles %}{% include "html_style.tpl" %}{% endif %}
</head>
<body>
{% if not exclude_styles %}{% include "html_body_inc.tpl" %}{% else %}{% include "html_body_exc.tpl" %}{% endif %}
</body>
</html>
{% elif not doctype_html %}
{% if exclude_styles %}
{% extends "html_basic.tpl" %}
{% include "html_body_exc.tpl" %}
{% else %}
{% extends "html_styles.tpl" %}
{% include "html_style.tpl" %}
{% include "html_body_inc.tpl" %}
{% endif %}
{% endif %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{# this template excludes all style, ids and classes #}
{% block before_table %}{% endblock before_table %}
{% block table %}
<table>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
{# Update the template_structure.html document too #}
{%- block before_style -%}{%- endblock before_style -%}
{% block style %}
<style type="text/css">
{% block table_styles %}
{% for s in table_styles %}
#T_{{uuid}} {{s.selector}} {
{% for p,val in s.props %}
{{p}}: {{val}};
{% endfor %}
}
{% endfor %}
{% endblock table_styles %}
{% block before_cellstyle %}{% endblock before_cellstyle %}
{% block cellstyle %}
{% for s in cellstyle %}
{% for selector in s.selectors %}{% if not loop.first %}, {% endif %}#T_{{uuid}}{{selector}}{% endfor %} {
{% for p,val in s.props %}
{{p}}: {{val}};
{% endfor %}
}
{% endfor %}
{% endblock cellstyle %}
</style>
{% endblock style %}
{% block before_table %}{% endblock before_table %}
{% block table %}
<table id="T_{{uuid}}"{% if table_attributes %} {{table_attributes}}{% endif %}>
Expand Down
24 changes: 24 additions & 0 deletions pandas/io/formats/templates/html_style.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{%- block before_style -%}{%- endblock before_style -%}
{% block style %}
<style type="text/css">
{% block table_styles %}
{% for s in table_styles %}
#T_{{uuid}} {{s.selector}} {
{% for p,val in s.props %}
{{p}}: {{val}};
{% endfor %}
}
{% endfor %}
{% endblock table_styles %}
{% block before_cellstyle %}{% endblock before_cellstyle %}
{% block cellstyle %}
{% for s in cellstyle %}
{% for selector in s.selectors %}{% if not loop.first %}, {% endif %}#T_{{uuid}}{{selector}}{% endfor %} {
{% for p,val in s.props %}
{{p}}: {{val}};
{% endfor %}
}
{% endfor %}
{% endblock cellstyle %}
</style>
{% endblock style %}
51 changes: 31 additions & 20 deletions pandas/tests/io/formats/style/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,28 @@


@pytest.fixture
def tpl_styles():
return env.get_template("html_styles.tpl")
def tpl_style():
return env.get_template("html_style.tpl")


@pytest.fixture
def tpl_basic():
return env.get_template("html_basic.tpl")
def tpl_body_exc():
return env.get_template("html_body_exc.tpl")


@pytest.fixture
def tpl_body_inc():
return env.get_template("html_body_inc.tpl")


def test_html_template_extends_options():
# make sure if templates are edited tests are updated as are setup fixtures
# to understand the dependency
with open("pandas/io/formats/templates/html.tpl") as file:
result = file.read()
expected = (
'{% if exclude_styles %}\n{% extends "html_basic.tpl" %}\n'
'{% else %}\n{% extends "html_styles.tpl" %}\n'
)
assert expected in result
assert '{% include "html_body_exc.tpl" %}' in result
assert '{% include "html_style.tpl" %}' in result
assert '{% include "html_body_inc.tpl" %}' in result


def test_exclude_styles():
Expand All @@ -41,7 +44,7 @@ def test_exclude_styles():
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta charset="utf-8">
</head>
<body>
<table>
Expand Down Expand Up @@ -135,15 +138,19 @@ def test_rowspan_w3():

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()
expected = dedent(
"""\
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
</style>
<meta charset="utf-8">
<style type="text/css">
#T_abc_ td {
color: red;
}
</style>
</head>
<body>
<table id="T_abc_">
Expand Down Expand Up @@ -171,14 +178,16 @@ def test_styles():
assert result == expected


def test_block_names(tpl_styles, tpl_basic):
def test_block_names(tpl_style, tpl_body_inc, tpl_body_exc):
# catch accidental removal of a block
expected1 = {
expected_style = {
"before_style",
"style",
"table_styles",
"before_cellstyle",
"cellstyle",
}
expected_body = {
"before_table",
"table",
"caption",
Expand All @@ -192,12 +201,14 @@ def test_block_names(tpl_styles, tpl_basic):
"tr",
"after_rows",
}
result1 = set(tpl_styles.blocks)
assert result1 == expected1
result1 = set(tpl_style.blocks)
assert result1 == expected_style

result2 = set(tpl_body_exc.blocks)
assert result2 == expected_body

expected2 = {v for v in expected1 if "style" not in v}
result2 = set(tpl_basic.blocks)
assert result2 == expected2
result3 = set(tpl_body_inc.blocks)
assert result3 == expected_body


def test_from_custom_template(tmpdir):
Expand Down
0