Skip to content

Commit

Permalink
ENH: Add option to disable MathJax (pandas-dev#19824). (pandas-dev#19856
Browse files Browse the repository at this point in the history
)
  • Loading branch information
davidchall authored and jreback committed Mar 1, 2018
1 parent d44a6ec commit 072545d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/source/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,10 @@ display.html.table_schema False Whether to publish a Table
display.html.border 1 A ``border=value`` attribute is
inserted in the ``<table>`` tag
for the DataFrame HTML repr.
display.html.use_mathjax True When True, Jupyter notebook will process
table contents using MathJax, rendering
mathematical expressions enclosed by the
dollar symbol.
io.excel.xls.writer xlwt The default Excel writer engine for
'xls' files.
io.excel.xlsm.writer openpyxl The default Excel writer engine for
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ Other Enhancements
- Added :func:`SeriesGroupBy.is_monotonic_increasing` and :func:`SeriesGroupBy.is_monotonic_decreasing` (:issue:`17015`)
- For subclassed ``DataFrames``, :func:`DataFrame.apply` will now preserve the ``Series`` subclass (if defined) when passing the data to the applied function (:issue:`19822`)
- :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`)
- 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`)


.. _whatsnew_0230.api_breaking:
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ def use_numexpr_cb(key):
(currently both are identical)
"""

pc_html_use_mathjax_doc = """\
: boolean
When True, Jupyter notebook will process table contents using MathJax,
rendering mathematical expressions enclosed by the dollar symbol.
(default: True)
"""

pc_width_doc = """
: int
Expand Down Expand Up @@ -358,6 +364,8 @@ def table_schema_cb(key):
validator=is_bool, cb=table_schema_cb)
cf.register_option('html.border', 1, pc_html_border_doc,
validator=is_int)
cf.register_option('html.use_mathjax', True, pc_html_use_mathjax_doc,
validator=is_bool)

with cf.config_prefix('html'):
cf.register_option('border', 1, pc_html_border_doc,
Expand Down
3 changes: 3 additions & 0 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,9 @@ def write_result(self, buf):
frame = self.frame

_classes = ['dataframe'] # Default class.
use_mathjax = get_option("display.html.use_mathjax")
if not use_mathjax:
_classes.append('tex2jax_ignore')
if self.classes is not None:
if isinstance(self.classes, str):
self.classes = self.classes.split()
Expand Down
12 changes: 11 additions & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,19 @@ def format_attr(pair):
.format(row=r, col=c)})
body.append(row_es)

table_attr = self.table_attributes
use_mathjax = get_option("display.html.use_mathjax")
if not use_mathjax:
table_attr = table_attr or ''
if 'class="' in table_attr:
table_attr = table_attr.replace('class="',
'class="tex2jax_ignore ')
else:
table_attr += ' class="tex2jax_ignore"'

return dict(head=head, cellstyle=cellstyle, body=body, uuid=uuid,
precision=precision, table_styles=table_styles,
caption=caption, table_attributes=self.table_attributes)
caption=caption, table_attributes=table_attr)

def format(self, formatter, subset=None):
"""
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,13 @@ def test_repr_html(self):

tm.reset_display_options()

def test_repr_html_mathjax(self):
df = DataFrame([[1, 2], [3, 4]])
assert 'tex2jax_ignore' not in df._repr_html_()

with pd.option_context('display.html.use_mathjax', False):
assert 'tex2jax_ignore' in df._repr_html_()

def test_repr_html_wide(self):
max_cols = get_option('display.max_columns')
df = DataFrame(tm.rands_array(25, size=(10, max_cols - 1)))
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/io/formats/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def test_init_series(self):
def test_repr_html_ok(self):
self.styler._repr_html_()

def test_repr_html_mathjax(self):
# gh-19824
assert 'tex2jax_ignore' not in self.styler._repr_html_()

with pd.option_context('display.html.use_mathjax', False):
assert 'tex2jax_ignore' in self.styler._repr_html_()

def test_update_ctx(self):
self.styler._update_ctx(self.attrs)
expected = {(0, 0): ['color: red'],
Expand Down

0 comments on commit 072545d

Please sign in to comment.