Skip to content

Commit 3df97e3

Browse files
author
David Hall
committed
ENH: Add Styler.disable_mathjax() method (pandas-dev#19824).
This prevents MathJax from processing table contents.
1 parent 572476f commit 3df97e3

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

pandas/io/formats/style.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class Styler(object):
6363
a unique identifier to avoid CSS collisons; generated automatically
6464
caption: str, default None
6565
caption to attach to the table
66+
disabled_mathjax: bool, default False
67+
prevent MathJax from processing table contents
6668
6769
Attributes
6870
----------
@@ -111,7 +113,7 @@ class Styler(object):
111113
template = env.get_template("html.tpl")
112114

113115
def __init__(self, data, precision=None, table_styles=None, uuid=None,
114-
caption=None, table_attributes=None):
116+
caption=None, disabled_mathjax=False, table_attributes=None):
115117
self.ctx = defaultdict(list)
116118
self._todo = []
117119

@@ -129,6 +131,7 @@ def __init__(self, data, precision=None, table_styles=None, uuid=None,
129131
self.uuid = uuid
130132
self.table_styles = table_styles
131133
self.caption = caption
134+
self.disabled_mathjax = disabled_mathjax
132135
if precision is None:
133136
precision = get_option('display.precision')
134137
self.precision = precision
@@ -181,6 +184,7 @@ def _translate(self):
181184
"""
182185
table_styles = self.table_styles or []
183186
caption = self.caption
187+
disabled_mathjax = self.disabled_mathjax
184188
ctx = self.ctx
185189
precision = self.precision
186190
hidden_index = self.hidden_index
@@ -327,7 +331,8 @@ def format_attr(pair):
327331

328332
return dict(head=head, cellstyle=cellstyle, body=body, uuid=uuid,
329333
precision=precision, table_styles=table_styles,
330-
caption=caption, table_attributes=self.table_attributes)
334+
caption=caption, disabled_mathjax=disabled_mathjax,
335+
table_attributes=self.table_attributes)
331336

332337
def format(self, formatter, subset=None):
333338
"""
@@ -431,6 +436,7 @@ def render(self, **kwargs):
431436
* precision
432437
* table_styles
433438
* caption
439+
* disabled_mathjax
434440
* table_attributes
435441
"""
436442
self._compute()
@@ -465,6 +471,7 @@ def _update_ctx(self, attrs):
465471
def _copy(self, deepcopy=False):
466472
styler = Styler(self.data, precision=self.precision,
467473
caption=self.caption, uuid=self.uuid,
474+
disabled_mathjax=self.disabled_mathjax,
468475
table_styles=self.table_styles)
469476
if deepcopy:
470477
styler.ctx = copy.deepcopy(self.ctx)
@@ -794,6 +801,17 @@ def set_table_styles(self, table_styles):
794801
self.table_styles = table_styles
795802
return self
796803

804+
def disable_mathjax(self):
805+
"""
806+
Prevent MathJax from processing table contents.
807+
808+
Returns
809+
-------
810+
self : Styler
811+
"""
812+
self.disabled_mathjax = True
813+
return self
814+
797815
def hide_index(self):
798816
"""
799817
Hide any indices from rendering.

pandas/io/formats/templates/html.tpl

+10-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
{%- endblock cellstyle %}
2424
</style>
2525
{%- endblock style %}
26-
{%- block before_table %}{% endblock before_table %}
26+
{%- block before_table %}
27+
{%- if disabled_mathjax -%}
28+
<div class="tex2jax_ignore">
29+
{%- endif -%}
30+
{% endblock before_table %}
2731
{%- block table %}
2832
<table id="T_{{uuid}}" {% if table_attributes %}{{ table_attributes }}{% endif %}>
2933
{%- block caption %}
@@ -67,4 +71,8 @@
6771
{%- endblock tbody %}
6872
</table>
6973
{%- endblock table %}
70-
{%- block after_table %}{% endblock after_table %}
74+
{%- block after_table %}
75+
{%- if disabled_mathjax -%}
76+
</div>
77+
{%- endif -%}
78+
{% endblock after_table %}

0 commit comments

Comments
 (0)