16
16
from pandas .core .config import get_option
17
17
18
18
from pandas .io .common import _is_url
19
- from pandas .io .formats .format import (
20
- TableFormatter , buffer_put_lines , get_level_lengths )
19
+ from pandas .io .formats .format import TableFormatter , get_level_lengths
21
20
from pandas .io .formats .printing import pprint_thing
22
21
23
22
24
23
class HTMLFormatter (TableFormatter ):
24
+ """
25
+ Internal class for formatting output data in html.
26
+ This class is intended for shared functionality between
27
+ DataFrame.to_html() and DataFrame._repr_html_().
28
+ Any logic in common with other output formatting methods
29
+ should ideally be inherited from classes in format.py
30
+ and this class responsible for only producing html markup.
31
+ """
25
32
26
33
indent_delta = 2
27
34
28
- def __init__ (self , formatter , classes = None , notebook = False , border = None ,
29
- table_id = None , render_links = False ):
35
+ def __init__ (self , formatter , classes = None , border = None ):
30
36
self .fmt = formatter
31
37
self .classes = classes
32
38
@@ -36,12 +42,11 @@ def __init__(self, formatter, classes=None, notebook=False, border=None,
36
42
self .bold_rows = self .fmt .kwds .get ('bold_rows' , False )
37
43
self .escape = self .fmt .kwds .get ('escape' , True )
38
44
self .show_dimensions = self .fmt .show_dimensions
39
- self .notebook = notebook
40
45
if border is None :
41
46
border = get_option ('display.html.border' )
42
47
self .border = border
43
- self .table_id = table_id
44
- self .render_links = render_links
48
+ self .table_id = self . fmt . table_id
49
+ self .render_links = self . fmt . render_links
45
50
46
51
@property
47
52
def show_row_idx_names (self ):
@@ -137,48 +142,7 @@ def write_tr(self, line, indent=0, indent_delta=0, header=False,
137
142
indent -= indent_delta
138
143
self .write ('</tr>' , indent )
139
144
140
- def write_style (self ):
141
- # We use the "scoped" attribute here so that the desired
142
- # style properties for the data frame are not then applied
143
- # throughout the entire notebook.
144
- template_first = """\
145
- <style scoped>"""
146
- template_last = """\
147
- </style>"""
148
- template_select = """\
149
- .dataframe %s {
150
- %s: %s;
151
- }"""
152
- element_props = [('tbody tr th:only-of-type' ,
153
- 'vertical-align' ,
154
- 'middle' ),
155
- ('tbody tr th' ,
156
- 'vertical-align' ,
157
- 'top' )]
158
- if isinstance (self .columns , ABCMultiIndex ):
159
- element_props .append (('thead tr th' ,
160
- 'text-align' ,
161
- 'left' ))
162
- if self .show_row_idx_names :
163
- element_props .append (('thead tr:last-of-type th' ,
164
- 'text-align' ,
165
- 'right' ))
166
- else :
167
- element_props .append (('thead th' ,
168
- 'text-align' ,
169
- 'right' ))
170
- template_mid = '\n \n ' .join (map (lambda t : template_select % t ,
171
- element_props ))
172
- template = dedent ('\n ' .join ((template_first ,
173
- template_mid ,
174
- template_last )))
175
- self .write (template )
176
-
177
- def write_result (self , buf ):
178
- if self .notebook :
179
- self .write ('<div>' )
180
- self .write_style ()
181
-
145
+ def render (self ):
182
146
self ._write_table ()
183
147
184
148
if self .should_show_dimensions :
@@ -188,10 +152,7 @@ def write_result(self, buf):
188
152
by = by ,
189
153
cols = len (self .frame .columns )))
190
154
191
- if self .notebook :
192
- self .write ('</div>' )
193
-
194
- buffer_put_lines (buf , self .elements )
155
+ return self .elements
195
156
196
157
def _write_table (self , indent = 0 ):
197
158
_classes = ['dataframe' ] # Default class.
@@ -516,3 +477,55 @@ def _write_hierarchical_rows(self, fmt_values, indent):
516
477
row .insert (self .row_levels + self .fmt .tr_col_num , '...' )
517
478
self .write_tr (row , indent , self .indent_delta , tags = None ,
518
479
nindex_levels = frame .index .nlevels )
480
+
481
+
482
+ class NotebookFormatter (HTMLFormatter ):
483
+ """
484
+ Internal class for formatting output data in html for display in Jupyter
485
+ Notebooks. This class is intended for functionality specific to
486
+ DataFrame._repr_html_() and DataFrame.to_html(notebook=True)
487
+ """
488
+
489
+ def write_style (self ):
490
+ # We use the "scoped" attribute here so that the desired
491
+ # style properties for the data frame are not then applied
492
+ # throughout the entire notebook.
493
+ template_first = """\
494
+ <style scoped>"""
495
+ template_last = """\
496
+ </style>"""
497
+ template_select = """\
498
+ .dataframe %s {
499
+ %s: %s;
500
+ }"""
501
+ element_props = [('tbody tr th:only-of-type' ,
502
+ 'vertical-align' ,
503
+ 'middle' ),
504
+ ('tbody tr th' ,
505
+ 'vertical-align' ,
506
+ 'top' )]
507
+ if isinstance (self .columns , ABCMultiIndex ):
508
+ element_props .append (('thead tr th' ,
509
+ 'text-align' ,
510
+ 'left' ))
511
+ if self .show_row_idx_names :
512
+ element_props .append (('thead tr:last-of-type th' ,
513
+ 'text-align' ,
514
+ 'right' ))
515
+ else :
516
+ element_props .append (('thead th' ,
517
+ 'text-align' ,
518
+ 'right' ))
519
+ template_mid = '\n \n ' .join (map (lambda t : template_select % t ,
520
+ element_props ))
521
+ template = dedent ('\n ' .join ((template_first ,
522
+ template_mid ,
523
+ template_last )))
524
+ self .write (template )
525
+
526
+ def render (self ):
527
+ self .write ('<div>' )
528
+ self .write_style ()
529
+ super (NotebookFormatter , self ).render ()
530
+ self .write ('</div>' )
531
+ return self .elements
0 commit comments