API: Add Styler.to_html, for saving output to HTML file by attack68 · Pull Request #40312 · pandas-dev/pandas (original) (raw)
- closes CLN/API: implemented to_html in terms of .style #11700 and closes ENH: write Styler rendered output to file #13379
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.