Skip to content

Commit 5bff74f

Browse files
committed
fixup! ENH: Add blocks to Styler template
1 parent ef4ed5a commit 5bff74f

File tree

11 files changed

+59
-9
lines changed

11 files changed

+59
-9
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ global-exclude *.png
2525
# recursive-include LICENSES *
2626
include versioneer.py
2727
include pandas/_version.py
28+
include pandas/formats/templates/*.tpl

doc/source/style.ipynb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,8 @@
883883
"outputs": [],
884884
"source": [
885885
"from jinja2 import Environment, ChoiceLoader, FileSystemLoader\n",
886-
"from IPython.display import HTML"
886+
"from IPython.display import HTML\n",
887+
"from pandas.io.api import Styler"
887888
]
888889
},
889890
{
@@ -983,7 +984,7 @@
983984
"cell_type": "markdown",
984985
"metadata": {},
985986
"source": [
986-
"For convenience, we provide the `styler_factory` function that does the same as the custom subclass."
987+
"For convenience, we provide the `Styler.from_custom_template` method that does the same as the custom subclass."
987988
]
988989
},
989990
{

doc/source/template_structure.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,17 @@
4343
<pre>&lt;table ...&gt;</pre>
4444
<div class="template_block">caption</div>
4545

46-
<div class="template_block" >thead</div>
47-
48-
<div class="template_block" >tbody</div>
46+
<div class="template_block" >thead
47+
<div class="template_block" >before_head_rows</div>
48+
<div class="template_block">head_tr (loop over headers)</div>
49+
<div class="template_block" >after_head_rows</div>
50+
</div>
51+
52+
<div class="template_block" >tbody
53+
<div class="template_block" >before_rows</div>
54+
<div class="template_block">tr (loop over data rows)</div>
55+
<div class="template_block" >after_rows</div>
56+
</div>
4957
<pre>&lt;/table&gt;</pre>
5058
</div><!-- /table -->
5159

pandas/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
from pandas.util.print_versions import show_versions
5757
from pandas.io.api import *
5858
from pandas.util._tester import test
59-
from pandas.formats.style import Styler
6059

6160
# extension module deprecations
6261
from pandas.util.depr_module import _DeprecatedModule

pandas/api/io/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
""" Public IO API """
2+
from pandas.formats.style import Styler # noqa

pandas/formats/style.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,11 @@ def render(self, **kwargs):
370370
-------
371371
rendered: str
372372
the rendered HTML
373+
**kwargs:
374+
Any additional keyword arguments are passed through
375+
to ``self.template.render``. This is useful when you
376+
need to provide additional variables for a custom
377+
template.
373378
374379
Notes
375380
-----
@@ -378,6 +383,19 @@ def render(self, **kwargs):
378383
last item in a Notebook cell. When calling ``Styler.render()``
379384
directly, wrap the result in ``IPython.display.HTML`` to view
380385
the rendered HTML in the notebook.
386+
387+
Pandas uses the following keys in render. Arguments passed
388+
in ``**kwargs`` take precedence, so think carefuly if you want
389+
to override them:
390+
391+
* head
392+
* cellstyle
393+
* body
394+
* uuid
395+
* precision
396+
* table_styles
397+
* caption
398+
* table_attributes
381399
"""
382400
self._compute()
383401
# TODO: namespace all the pandas keys

pandas/formats/templates/html.tpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{# Update the template_structure.html document too #}
12
{%- block before_style -%}{%- endblock before_style -%}
23
{% block style %}
34
<style type="text/css" >
@@ -32,28 +33,36 @@
3233
{%- endblock caption %}
3334
{%- block thead %}
3435
<thead>
36+
{%- block before_head_rows %}{% endblock %}
3537
{%- for r in head %}
38+
{%- block head_tr scoped %}
3639
<tr>
3740
{%- for c in r %}
3841
{%- if c.is_visible != False %}
3942
<{{ c.type }} class="{{c.class}}" {{ c.attributes|join(" ") }}>{{c.value}}</{{ c.type }}>
4043
{%- endif %}
4144
{%- endfor %}
4245
</tr>
46+
{%- endblock head_tr %}
4347
{%- endfor %}
48+
{%- block after_head_rows %}{% endblock %}
4449
</thead>
4550
{%- endblock thead %}
4651
{%- block tbody %}
4752
<tbody>
53+
{%- block before_rows %}{%- endblock before_rows %}
4854
{%- for r in body %}
55+
{%- block tr scoped %}
4956
<tr>
5057
{%- for c in r %}
5158
{%- if c.is_visible != False %}
5259
<{{ c.type }} id="T_{{ uuid }}{{ c.id }}" class="{{ c.class }}" {{ c.attributes|join(" ") }}>{{ c.display_value }}</{{ c.type }}>
5360
{%- endif %}
5461
{%- endfor %}
5562
</tr>
63+
{%- endblock tr %}
5664
{%- endfor %}
65+
{%- block after_rows %}{%- endblock after_rows %}
5766
</tbody>
5867
{%- endblock tbody %}
5968
</table>

pandas/io/api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
from pandas.io.pickle import read_pickle, to_pickle
1818
from pandas.io.packers import read_msgpack, to_msgpack
1919
from pandas.io.gbq import read_gbq
20+
try:
21+
from pandas.formats.style import Styler
22+
except:
23+
pass
2024

2125
# deprecation, xref #13790
2226
def Term(*args, **kwargs):

pandas/tests/api/test_api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class TestPDApi(Base, tm.TestCase):
4848
'Grouper', 'HDFStore', 'Index', 'Int64Index', 'MultiIndex',
4949
'Period', 'PeriodIndex', 'RangeIndex', 'UInt64Index',
5050
'Series', 'SparseArray', 'SparseDataFrame',
51-
'SparseSeries', 'Styler', 'TimeGrouper', 'Timedelta',
51+
'SparseSeries', 'TimeGrouper', 'Timedelta',
5252
'TimedeltaIndex', 'Timestamp']
5353

5454
# these are already deprecated; awaiting removal
@@ -127,6 +127,12 @@ def test_api(self):
127127
self.check(api, self.allowed)
128128

129129

130+
class TestApiIO(Base, tm.TestCase):
131+
132+
def test_api(self):
133+
self.check(api.io, self.allowed)
134+
135+
130136
class TestDatetoolsDeprecation(tm.TestCase):
131137

132138
def test_deprecation_access_func(self):

pandas/tests/formats/test_style.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,8 @@ def test_block_names():
725725
expected = {
726726
'before_style', 'style', 'table_styles', 'before_cellstyle',
727727
'cellstyle', 'before_table', 'table', 'caption', 'thead', 'tbody',
728-
'after_table',
728+
'after_table', 'before_head_rows', 'head_tr', 'after_head_rows',
729+
'before_rows', 'tr', 'after_rows',
729730
}
730731
result = set(Styler.template.blocks)
731732
assert result == expected

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,8 @@ def pxd(name):
699699
'data/html_encoding/*.html',
700700
'json/data/*.json'],
701701
'pandas.tests.tools': ['data/*.csv'],
702-
'pandas.tests.tseries': ['data/*.pickle']
702+
'pandas.tests.tseries': ['data/*.pickle'],
703+
'pandas.formats': ['templates/*.tpl']
703704
},
704705
ext_modules=extensions,
705706
maintainer_email=EMAIL,

0 commit comments

Comments
 (0)