-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from all commits
4799d86
2eb5702
a0bcbd1
5135a91
3e9903d
766b758
e558a6a
00f8a6e
2c440ed
dd6785e
e8d0629
42cefe6
80097cb
10f007c
c876bf6
1e5061a
130fba4
5079922
339ca37
c701d3d
3f074aa
e1818f2
eeeaf54
b14e7af
2a48f4d
dda33de
b55db71
a5c0ec7
1d3ba20
4e4c76d
f43961d
a3ce1ef
8899fb5
8375f99
f5b84fa
66ba741
d2e2731
792ab44
2575058
c0bc29c
489030f
2d996ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -739,6 +739,74 @@ def to_latex( | |
|
||
return save_to_buffer(latex, buf=buf, encoding=encoding) | ||
|
||
def to_html( | ||
self, | ||
buf: FilePathOrBuffer[str] | None = None, | ||
*, | ||
table_uuid: str | None = None, | ||
table_attributes: str | None = None, | ||
encoding: str | None = None, | ||
doctype_html: bool = False, | ||
exclude_styles: bool = False, | ||
): | ||
""" | ||
Write Styler to a file, buffer or string in HTML-CSS format. | ||
|
||
.. versionadded:: 1.3.0 | ||
|
||
Parameters | ||
---------- | ||
buf : str, Path, or StringIO-like, optional, default None | ||
Buffer to write to. If ``None``, the output is returned as a string. | ||
table_uuid: str, optional | ||
Id attribute assigned to the <table> HTML element in the format: | ||
|
||
``<table id="T_<table_uuid>" ..>`` | ||
|
||
If not given uses Styler's initially assigned value. | ||
table_attributes: str, optional | ||
Attributes to assign within the `<table>` HTML element in the format: | ||
|
||
``<table .. <table_attributes> >`` | ||
|
||
If not given defaults to Styler's preexisting value. | ||
encoding : str, optional | ||
Character encoding setting for file output, and HTML meta tags, | ||
defaults to "utf-8" if None. | ||
doctype_html : bool, default False | ||
Whether to output a fully structured HTML file including all | ||
HTML elements, or just the core ``<style>`` and ``<table>`` elements. | ||
exclude_styles : bool, default False | ||
Whether to include the ``<style>`` element and all associated element | ||
``class`` and ``id`` identifiers, or solely the ``<table>`` element without | ||
styling identifiers. | ||
|
||
Returns | ||
------- | ||
str or None | ||
If `buf` is None, returns the result as a string. Otherwise returns `None`. | ||
|
||
See Also | ||
-------- | ||
DataFrame.to_html: Write a DataFrame to a file, buffer or string in HTML format. | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a See Also back to DataFrame.to_html (or are we deprecating this in this PR?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't deprecate yet. working towards but needs a gap analysis: there are some things mising. |
||
if table_uuid: | ||
self.set_uuid(table_uuid) | ||
|
||
if table_attributes: | ||
self.set_table_attributes(table_attributes) | ||
|
||
# Build HTML string.. | ||
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) | ||
) | ||
|
||
def set_td_classes(self, classes: DataFrame) -> Styler: | ||
""" | ||
Set the DataFrame of strings added to the ``class`` attribute of ``<td>`` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,16 @@ | ||
{# 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 %}> | ||
{% block caption %} | ||
{% if caption %} | ||
<caption>{{caption}}</caption> | ||
{# 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> | ||
{% include "html_table.tpl" %} | ||
</body> | ||
</html> | ||
{% elif not doctype_html %} | ||
{% if not exclude_styles %}{% include "html_style.tpl" %}{% endif %} | ||
{% include "html_table.tpl" %} | ||
{% endif %} | ||
{% endblock caption %} | ||
{% block thead %} | ||
<thead> | ||
{% block before_head_rows %}{% endblock %} | ||
{% for r in head %} | ||
{% block head_tr scoped %} | ||
<tr> | ||
{% for c in r %} | ||
{% if c.is_visible != False %} | ||
<{{c.type}} class="{{c.class}}" {{c.attributes}}>{{c.value}}</{{c.type}}> | ||
{% endif %} | ||
{% endfor %} | ||
</tr> | ||
{% endblock head_tr %} | ||
{% endfor %} | ||
{% block after_head_rows %}{% endblock %} | ||
</thead> | ||
{% endblock thead %} | ||
{% block tbody %} | ||
<tbody> | ||
{% block before_rows %}{% endblock before_rows %} | ||
{% for r in body %} | ||
{% block tr scoped %} | ||
<tr> | ||
{% for c in r %} | ||
{% if c.is_visible != False %} | ||
<{{c.type}} {% if c.id is defined -%} id="T_{{uuid}}{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.display_value}}</{{c.type}}> | ||
{% endif %} | ||
{% endfor %} | ||
</tr> | ||
{% endblock tr %} | ||
{% endfor %} | ||
{% block after_rows %}{% endblock after_rows %} | ||
</tbody> | ||
{% endblock tbody %} | ||
</table> | ||
{% endblock table %} | ||
{% block after_table %}{% endblock after_table %} |
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 %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{% block before_table %}{% endblock before_table %} | ||
{% block table %} | ||
{% if exclude_styles %} | ||
<table> | ||
{% else %} | ||
<table id="T_{{uuid}}"{% if table_attributes %} {{table_attributes}}{% endif %}> | ||
{% endif %} | ||
{% block caption %} | ||
{% if caption %} | ||
<caption>{{caption}}</caption> | ||
{% endif %} | ||
{% endblock caption %} | ||
{% block thead %} | ||
<thead> | ||
{% block before_head_rows %}{% endblock %} | ||
{% for r in head %} | ||
{% block head_tr scoped %} | ||
<tr> | ||
{% if exclude_styles %} | ||
{% for c in r %} | ||
{% if c.is_visible != False %} | ||
<{{c.type}} {{c.attributes}}>{{c.value}}</{{c.type}}> | ||
{% endif %} | ||
{% endfor %} | ||
{% else %} | ||
{% for c in r %} | ||
{% if c.is_visible != False %} | ||
<{{c.type}} class="{{c.class}}" {{c.attributes}}>{{c.value}}</{{c.type}}> | ||
{% endif %} | ||
{% endfor %} | ||
{% endif %} | ||
</tr> | ||
{% endblock head_tr %} | ||
{% endfor %} | ||
{% block after_head_rows %}{% endblock %} | ||
</thead> | ||
{% endblock thead %} | ||
{% block tbody %} | ||
<tbody> | ||
{% block before_rows %}{% endblock before_rows %} | ||
{% for r in body %} | ||
{% block tr scoped %} | ||
<tr> | ||
{% if exclude_styles %} | ||
{% for c in r %}{% if c.is_visible != False %} | ||
<{{c.type}} {{c.attributes}}>{{c.display_value}}</{{c.type}}> | ||
{% endif %}{% endfor %} | ||
{% else %} | ||
{% for c in r %}{% if c.is_visible != False %} | ||
<{{c.type}} {% if c.id is defined -%} id="T_{{uuid}}{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.display_value}}</{{c.type}}> | ||
{% endif %}{% endfor %} | ||
{% endif %} | ||
</tr> | ||
{% endblock tr %} | ||
{% endfor %} | ||
{% block after_rows %}{% endblock after_rows %} | ||
</tbody> | ||
{% endblock tbody %} | ||
</table> | ||
{% endblock table %} | ||
{% block after_table %}{% endblock after_table %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a versionadded 1.3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done