Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add option to disable MathJax (#19824). #19856

Merged
merged 11 commits into from
Mar 1, 2018
22 changes: 20 additions & 2 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class Styler(object):
a unique identifier to avoid CSS collisons; generated automatically
caption: str, default None
caption to attach to the table
disabled_mathjax: bool, default False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than an option prefer this to be an options as @TomAugspurger discussed in the orgiinal issue, and IIRC this should only be handled in the to_html, and NOT in the styler

prevent MathJax from processing table contents

Attributes
----------
Expand Down Expand Up @@ -111,7 +113,7 @@ class Styler(object):
template = env.get_template("html.tpl")

def __init__(self, data, precision=None, table_styles=None, uuid=None,
caption=None, table_attributes=None):
caption=None, disabled_mathjax=False, table_attributes=None):
self.ctx = defaultdict(list)
self._todo = []

Expand All @@ -129,6 +131,7 @@ def __init__(self, data, precision=None, table_styles=None, uuid=None,
self.uuid = uuid
self.table_styles = table_styles
self.caption = caption
self.disabled_mathjax = disabled_mathjax
if precision is None:
precision = get_option('display.precision')
self.precision = precision
Expand Down Expand Up @@ -181,6 +184,7 @@ def _translate(self):
"""
table_styles = self.table_styles or []
caption = self.caption
disabled_mathjax = self.disabled_mathjax
ctx = self.ctx
precision = self.precision
hidden_index = self.hidden_index
Expand Down Expand Up @@ -327,7 +331,8 @@ def format_attr(pair):

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, disabled_mathjax=disabled_mathjax,
table_attributes=self.table_attributes)

def format(self, formatter, subset=None):
"""
Expand Down Expand Up @@ -431,6 +436,7 @@ def render(self, **kwargs):
* precision
* table_styles
* caption
* disabled_mathjax
* table_attributes
"""
self._compute()
Expand Down Expand Up @@ -465,6 +471,7 @@ def _update_ctx(self, attrs):
def _copy(self, deepcopy=False):
styler = Styler(self.data, precision=self.precision,
caption=self.caption, uuid=self.uuid,
disabled_mathjax=self.disabled_mathjax,
table_styles=self.table_styles)
if deepcopy:
styler.ctx = copy.deepcopy(self.ctx)
Expand Down Expand Up @@ -794,6 +801,17 @@ def set_table_styles(self, table_styles):
self.table_styles = table_styles
return self

def disable_mathjax(self):
"""
Prevent MathJax from processing table contents.

Returns
-------
self : Styler
"""
self.disabled_mathjax = True
return self

def hide_index(self):
"""
Hide any indices from rendering.
Expand Down
12 changes: 10 additions & 2 deletions pandas/io/formats/templates/html.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
{%- endblock cellstyle %}
</style>
{%- endblock style %}
{%- block before_table %}{% endblock before_table %}
{%- block before_table %}
{%- if disabled_mathjax -%}
<div class="tex2jax_ignore">
{%- endif -%}
{% endblock before_table %}
{%- block table %}
<table id="T_{{uuid}}" {% if table_attributes %}{{ table_attributes }}{% endif %}>
{%- block caption %}
Expand Down Expand Up @@ -67,4 +71,8 @@
{%- endblock tbody %}
</table>
{%- endblock table %}
{%- block after_table %}{% endblock after_table %}
{%- block after_table %}
{%- if disabled_mathjax -%}
</div>
{%- endif -%}
{% endblock after_table %}