Skip to content

Commit 072545d

Browse files
davidchalljreback
authored andcommitted
ENH: Add option to disable MathJax (pandas-dev#19824). (pandas-dev#19856)
1 parent d44a6ec commit 072545d

File tree

7 files changed

+41
-1
lines changed

7 files changed

+41
-1
lines changed

doc/source/options.rst

+4
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,10 @@ display.html.table_schema False Whether to publish a Table
402402
display.html.border 1 A ``border=value`` attribute is
403403
inserted in the ``<table>`` tag
404404
for the DataFrame HTML repr.
405+
display.html.use_mathjax True When True, Jupyter notebook will process
406+
table contents using MathJax, rendering
407+
mathematical expressions enclosed by the
408+
dollar symbol.
405409
io.excel.xls.writer xlwt The default Excel writer engine for
406410
'xls' files.
407411
io.excel.xlsm.writer openpyxl The default Excel writer engine for

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ Other Enhancements
337337
- Added :func:`SeriesGroupBy.is_monotonic_increasing` and :func:`SeriesGroupBy.is_monotonic_decreasing` (:issue:`17015`)
338338
- For subclassed ``DataFrames``, :func:`DataFrame.apply` will now preserve the ``Series`` subclass (if defined) when passing the data to the applied function (:issue:`19822`)
339339
- :func:`DataFrame.from_dict` now accepts a ``columns`` argument that can be used to specify the column names when ``orient='index'`` is used (:issue:`18529`)
340+
- Added option ``display.html.use_mathjax`` so `MathJax <https://www.mathjax.org/>`_ can be disabled when rendering tables in ``Jupyter`` notebooks (:issue:`19856`, :issue:`19824`)
340341

341342

342343
.. _whatsnew_0230.api_breaking:

pandas/core/config_init.py

+8
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ def use_numexpr_cb(key):
207207
(currently both are identical)
208208
"""
209209

210+
pc_html_use_mathjax_doc = """\
211+
: boolean
212+
When True, Jupyter notebook will process table contents using MathJax,
213+
rendering mathematical expressions enclosed by the dollar symbol.
214+
(default: True)
215+
"""
210216

211217
pc_width_doc = """
212218
: int
@@ -358,6 +364,8 @@ def table_schema_cb(key):
358364
validator=is_bool, cb=table_schema_cb)
359365
cf.register_option('html.border', 1, pc_html_border_doc,
360366
validator=is_int)
367+
cf.register_option('html.use_mathjax', True, pc_html_use_mathjax_doc,
368+
validator=is_bool)
361369

362370
with cf.config_prefix('html'):
363371
cf.register_option('border', 1, pc_html_border_doc,

pandas/io/formats/format.py

+3
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,9 @@ def write_result(self, buf):
12091209
frame = self.frame
12101210

12111211
_classes = ['dataframe'] # Default class.
1212+
use_mathjax = get_option("display.html.use_mathjax")
1213+
if not use_mathjax:
1214+
_classes.append('tex2jax_ignore')
12121215
if self.classes is not None:
12131216
if isinstance(self.classes, str):
12141217
self.classes = self.classes.split()

pandas/io/formats/style.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,19 @@ def format_attr(pair):
325325
.format(row=r, col=c)})
326326
body.append(row_es)
327327

328+
table_attr = self.table_attributes
329+
use_mathjax = get_option("display.html.use_mathjax")
330+
if not use_mathjax:
331+
table_attr = table_attr or ''
332+
if 'class="' in table_attr:
333+
table_attr = table_attr.replace('class="',
334+
'class="tex2jax_ignore ')
335+
else:
336+
table_attr += ' class="tex2jax_ignore"'
337+
328338
return dict(head=head, cellstyle=cellstyle, body=body, uuid=uuid,
329339
precision=precision, table_styles=table_styles,
330-
caption=caption, table_attributes=self.table_attributes)
340+
caption=caption, table_attributes=table_attr)
331341

332342
def format(self, formatter, subset=None):
333343
"""

pandas/tests/io/formats/test_format.py

+7
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,13 @@ def test_repr_html(self):
14341434

14351435
tm.reset_display_options()
14361436

1437+
def test_repr_html_mathjax(self):
1438+
df = DataFrame([[1, 2], [3, 4]])
1439+
assert 'tex2jax_ignore' not in df._repr_html_()
1440+
1441+
with pd.option_context('display.html.use_mathjax', False):
1442+
assert 'tex2jax_ignore' in df._repr_html_()
1443+
14371444
def test_repr_html_wide(self):
14381445
max_cols = get_option('display.max_columns')
14391446
df = DataFrame(tm.rands_array(25, size=(10, max_cols - 1)))

pandas/tests/io/formats/test_style.py

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ def test_init_series(self):
4646
def test_repr_html_ok(self):
4747
self.styler._repr_html_()
4848

49+
def test_repr_html_mathjax(self):
50+
# gh-19824
51+
assert 'tex2jax_ignore' not in self.styler._repr_html_()
52+
53+
with pd.option_context('display.html.use_mathjax', False):
54+
assert 'tex2jax_ignore' in self.styler._repr_html_()
55+
4956
def test_update_ctx(self):
5057
self.styler._update_ctx(self.attrs)
5158
expected = {(0, 0): ['color: red'],

0 commit comments

Comments
 (0)