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

Conversation

attack68
Copy link
Contributor

@attack68 attack68 commented Mar 8, 2021

Styler.to_html method

This PR adds the above method to Styler.

This is a minimal introduction with limited arguments to provide just the core file io functionality and basic options, either to write a fully structured HTML file or just the Styler.render output. It is very easier to extend in follow ons.

Jinja 2 template inheritance

Due to the more complex logic, the jinja2 templates were split into smaller components:

  • html_style.tpl: this just deals with the <style> html element
  • html_table.tpl: this just deals with the core <table> element including (or excluding) style identifiers
  • html.tpl: this is the controlling template which imports the above based on what is required.

It is more performant to have a separate temple that excludes styling elements by about 33%, rather than include the logic in a single template. I think it is easier to maintain as well.

Tests

A new test_to_html.py file is added. Some tests are moved from test_style.py if they more relate to HTML generation. Some new tests are added.

Objective

Ultimately the medium-term aim is to deprecate DataFrame.to_html. Styler is currently faster at rendering HTML than DataFrame.to_html (#39972), and this is further improved in this PR.
There is a standalone purpose for wanting to create HTML to file from Styler.
This PR complements Styler.to_latex whose aim is also to deprecate DataFrame.to_latex.

@attack68 attack68 marked this pull request as draft March 8, 2021 21:05
@simonjayhawkins simonjayhawkins added IO HTML read_html, to_html, Styler.apply, Styler.applymap Styler conditional formatting using DataFrame.style labels Mar 14, 2021
@attack68 attack68 marked this pull request as ready for review April 23, 2021 14:27
@attack68 attack68 changed the title [WIP] API: Styler.to_html can ultimately replace DataFrame.to_html API: Styler.to_html can ultimately replace DataFrame.to_html Apr 23, 2021
@attack68 attack68 changed the title API: Styler.to_html can ultimately replace DataFrame.to_html API: Add Styler.to_html, for saving output to HTML file Apr 23, 2021
encoding : str, optional
Character encoding setting for file output, and HTML meta tags,
defaults to "utf-8" if None.
doctype_html : bool, default True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the utility of these last 2 options?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doctype_html specifies whether to output a properly formed HTML document with appropriate <html>, <body> and <head> tags, or just the <style> and <table> tags.

exclude_styles provides an option in keeping with DataFrame.to_html()and it excludes all HTML classes and HTML ids and the <style> tag. (It is also more performant)

@jreback jreback added this to the 1.3 milestone May 7, 2021
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great very minor comment, ping on green.

exclude_styles: bool = False,
):
"""
Write Styler to a file, buffer or string in HTML-CSS format.
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

-------
str or None
If `buf` is None, returns the result as a string. Otherwise returns `None`.
"""
Copy link
Contributor

Choose a reason for hiding this comment

The 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?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@attack68
Copy link
Contributor Author

@jreback ping: greenish, failure unrelated

@attack68 attack68 requested a review from jreback May 25, 2021 13:49
@jreback jreback merged commit 427a493 into pandas-dev:master May 26, 2021
@jreback
Copy link
Contributor

jreback commented May 26, 2021

thanks @attack68

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO HTML read_html, to_html, Styler.apply, Styler.applymap Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CLN/API: implemented to_html in terms of .style ENH: write Styler rendered output to file
3 participants