Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2eb3b0

Browse files
committedNov 18, 2015
CLN: Followup to formatting
- CSS update - whatsnew example - print_versions - requirements_all
1 parent b54f2db commit a2eb3b0

File tree

10 files changed

+6918
-6252
lines changed

10 files changed

+6918
-6252
lines changed
 

‎ci/requirements_all.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ lxml
2020
sqlalchemy
2121
bottleneck
2222
pymysql
23+
Jinja2

‎doc/make.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,14 @@ def clean():
104104

105105
def html():
106106
check_build()
107+
os.system('jupyter nbconvert --to=html --template=basic '
108+
'--output=source/html-styling.html source/html-styling.ipynb')
107109
if os.system('sphinx-build -P -b html -d build/doctrees '
108110
'source build/html'):
109111
raise SystemExit("Building HTML failed.")
110112
try:
111113
# remove stale file
114+
os.system('rm source/html-styling.html')
112115
os.system('cd build; rm -f html/pandas.zip;')
113116
except:
114117
pass

‎doc/source/html-styling.html

Lines changed: 2981 additions & 3262 deletions
Large diffs are not rendered by default.

‎doc/source/html-styling.ipynb

Lines changed: 2999 additions & 2986 deletions
Large diffs are not rendered by default.

‎doc/source/themes/nature_with_gtoc/static/nature.css_t

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,19 @@ td.field-body blockquote {
299299
padding-left: 30px;
300300
}
301301

302+
.rendered_html table {
303+
margin-left: auto;
304+
margin-right: auto;
305+
border-right: 1px solid #cbcbcb;
306+
border-bottom: 1px solid #cbcbcb;
307+
}
308+
309+
.rendered_html td, th {
310+
border-left: 1px solid #cbcbcb;
311+
border-top: 1px solid #cbcbcb;
312+
margin: 0;
313+
padding: 0.5em .75em;
314+
}
302315

303316
/**
304317
* See also

‎doc/source/whatsnew/v0.17.1.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,25 @@ New features
2525
Conditional HTML Formatting
2626
^^^^^^^^^^^^^^^^^^^^^^^^^^^
2727

28-
We've added *experimental* support for conditional HTML formatting,
28+
We've added *experimental* support for conditional HTML formatting:
2929
the visual styling of a DataFrame based on the data.
3030
The styling is accomplished with HTML and CSS.
3131
Acesses the styler class with :attr:`pandas.DataFrame.style`, attribute,
3232
an instance of :class:`~pandas.core.style.Styler` with your data attached.
33+
34+
Here's a quick example:
35+
36+
.. ipython:: python
37+
38+
np.random.seed(123)
39+
df = DataFrame(np.random.randn(10, 5), columns=list('abcde'))
40+
html = df.style.background_gradient(cmap='viridis', low=.5)
41+
42+
We can render the HTML to get the following table.
43+
44+
.. raw:: html
45+
:file: whatsnew_0171_html_table.html
46+
3347
See the :ref:`example notebook <style>` for more.
3448

3549
.. _whatsnew_0171.enhancements:

‎doc/source/whatsnew/whatsnew_0171_html_table.html

Lines changed: 873 additions & 0 deletions
Large diffs are not rendered by default.

‎pandas/core/style.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Styler(object):
9393
{% endfor %}
9494
</style>
9595
96-
<table id="T_{{uuid}}">
96+
<table id="T_{{uuid}}" {{ table_attributes }}>
9797
{% if caption %}
9898
<caption>{{caption}}</caption>
9999
{% endif %}
@@ -125,7 +125,7 @@ class Styler(object):
125125
""")
126126

127127
def __init__(self, data, precision=None, table_styles=None, uuid=None,
128-
caption=None):
128+
caption=None, table_attributes=None):
129129
self.ctx = defaultdict(list)
130130
self._todo = []
131131

@@ -146,6 +146,7 @@ def __init__(self, data, precision=None, table_styles=None, uuid=None,
146146
if precision is None:
147147
precision = pd.options.display.precision
148148
self.precision = precision
149+
self.table_attributes = table_attributes
149150

150151
def _repr_html_(self):
151152
'''
@@ -230,7 +231,7 @@ def _translate(self):
230231

231232
return dict(head=head, cellstyle=cellstyle, body=body, uuid=uuid,
232233
precision=precision, table_styles=table_styles,
233-
caption=caption)
234+
caption=caption, table_attributes=self.table_attributes)
234235

235236
def render(self):
236237
"""
@@ -415,6 +416,25 @@ def set_precision(self, precision):
415416
self.precision = precision
416417
return self
417418

419+
def set_table_attributes(self, attributes):
420+
"""
421+
Set the table attributes. These are the items
422+
that show up in the opening <table> tag in addition
423+
to to automatic (by default) id.
424+
425+
.. versionadded:: 0.17.1
426+
427+
Parameters
428+
----------
429+
precision: int
430+
431+
Returns
432+
-------
433+
self
434+
"""
435+
self.table_attributes = attributes
436+
return self
437+
418438
def export(self):
419439
"""
420440
Export the styles to applied to the current Styler.

‎pandas/tests/test_style.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,15 @@ def test_table_styles(self):
277277
self.assertTrue(styler is result)
278278
self.assertEqual(styler.table_styles, style)
279279

280+
def test_table_attributes(self):
281+
attributes = 'class="foo" data-bar'
282+
styler = Styler(self.df, table_attributes=attributes)
283+
result = styler.render()
284+
self.assertTrue('class="foo" data-bar' in result)
285+
286+
result = self.df.style.set_table_attributes(attributes).render()
287+
self.assertTrue('class="foo" data-bar' in result)
288+
280289
def test_precision(self):
281290
with pd.option_context('display.precision', 10):
282291
s = Styler(self.df)

‎pandas/util/print_versions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def show_versions(as_json=False):
8989
("sqlalchemy", lambda mod: mod.__version__),
9090
("pymysql", lambda mod: mod.__version__),
9191
("psycopg2", lambda mod: mod.__version__),
92+
("Jinja2", lambda mod: mod.__version__)
9293
]
9394

9495
deps_blob = list()

0 commit comments

Comments
 (0)
Please sign in to comment.