Skip to content

REF: io/formats/html.py (and io/formats/format.py) #24651

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 6 commits into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
11 changes: 5 additions & 6 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,15 +730,14 @@ def to_html(self, classes=None, notebook=False, border=None):

.. versionadded:: 0.19.0
"""
from pandas.io.formats.html import HTMLFormatter
html_renderer = HTMLFormatter(self, classes=classes, notebook=notebook,
border=border, table_id=self.table_id,
render_links=self.render_links)
from pandas.io.formats.html import HTMLFormatter, NotebookFormatter
Klass = NotebookFormatter if notebook else HTMLFormatter
html = Klass(self, classes=classes, border=border).render()
if hasattr(self.buf, 'write'):
html_renderer.write_result(self.buf)
buffer_put_lines(self.buf, html)
elif isinstance(self.buf, compat.string_types):
with open(self.buf, 'w') as f:
html_renderer.write_result(f)
buffer_put_lines(f, html)
else:
raise TypeError('buf is not a file name and it has no write '
' method')
Expand Down
106 changes: 53 additions & 53 deletions pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
from pandas.core.config import get_option

from pandas.io.common import _is_url
from pandas.io.formats.format import (
TableFormatter, buffer_put_lines, get_level_lengths)
from pandas.io.formats.format import TableFormatter, get_level_lengths
from pandas.io.formats.printing import pprint_thing


class HTMLFormatter(TableFormatter):

indent_delta = 2

def __init__(self, formatter, classes=None, notebook=False, border=None,
table_id=None, render_links=False):
def __init__(self, formatter, classes=None, border=None):
self.fmt = formatter
self.classes = classes

Expand All @@ -36,12 +34,11 @@ def __init__(self, formatter, classes=None, notebook=False, border=None,
self.bold_rows = self.fmt.kwds.get('bold_rows', False)
self.escape = self.fmt.kwds.get('escape', True)
self.show_dimensions = self.fmt.show_dimensions
self.notebook = notebook
if border is None:
border = get_option('display.html.border')
self.border = border
self.table_id = table_id
self.render_links = render_links
self.table_id = self.fmt.table_id
self.render_links = self.fmt.render_links

@property
def show_row_idx_names(self):
Expand Down Expand Up @@ -137,48 +134,7 @@ def write_tr(self, line, indent=0, indent_delta=0, header=False,
indent -= indent_delta
self.write('</tr>', indent)

def write_style(self):
# We use the "scoped" attribute here so that the desired
# style properties for the data frame are not then applied
# throughout the entire notebook.
template_first = """\
<style scoped>"""
template_last = """\
</style>"""
template_select = """\
.dataframe %s {
%s: %s;
}"""
element_props = [('tbody tr th:only-of-type',
'vertical-align',
'middle'),
('tbody tr th',
'vertical-align',
'top')]
if isinstance(self.columns, ABCMultiIndex):
element_props.append(('thead tr th',
'text-align',
'left'))
if self.show_row_idx_names:
element_props.append(('thead tr:last-of-type th',
'text-align',
'right'))
else:
element_props.append(('thead th',
'text-align',
'right'))
template_mid = '\n\n'.join(map(lambda t: template_select % t,
element_props))
template = dedent('\n'.join((template_first,
template_mid,
template_last)))
self.write(template)

def write_result(self, buf):
if self.notebook:
self.write('<div>')
self.write_style()

def render(self):
self._write_table()

if self.should_show_dimensions:
Expand All @@ -188,10 +144,7 @@ def write_result(self, buf):
by=by,
cols=len(self.frame.columns)))

if self.notebook:
self.write('</div>')

buffer_put_lines(buf, self.elements)
return self.elements

def _write_table(self, indent=0):
_classes = ['dataframe'] # Default class.
Expand Down Expand Up @@ -516,3 +469,50 @@ def _write_hierarchical_rows(self, fmt_values, indent):
row.insert(self.row_levels + self.fmt.tr_col_num, '...')
self.write_tr(row, indent, self.indent_delta, tags=None,
nindex_levels=frame.index.nlevels)


class NotebookFormatter(HTMLFormatter):

def write_style(self):
# We use the "scoped" attribute here so that the desired
# style properties for the data frame are not then applied
# throughout the entire notebook.
template_first = """\
<style scoped>"""
template_last = """\
</style>"""
template_select = """\
.dataframe %s {
%s: %s;
}"""
element_props = [('tbody tr th:only-of-type',
'vertical-align',
'middle'),
('tbody tr th',
'vertical-align',
'top')]
if isinstance(self.columns, ABCMultiIndex):
element_props.append(('thead tr th',
'text-align',
'left'))
if self.show_row_idx_names:
element_props.append(('thead tr:last-of-type th',
'text-align',
'right'))
else:
element_props.append(('thead th',
'text-align',
'right'))
template_mid = '\n\n'.join(map(lambda t: template_select % t,
element_props))
template = dedent('\n'.join((template_first,
template_mid,
template_last)))
self.write(template)

def render(self):
self.write('<div>')
self.write_style()
super(NotebookFormatter, self).render()
self.write('</div>')
return self.elements