Skip to content

Commit

Permalink
ENH: Add Styler.disable_mathjax() method (pandas-dev#19824).
Browse files Browse the repository at this point in the history
This prevents MathJax from processing table contents.
  • Loading branch information
David Hall committed Feb 23, 2018
1 parent 572476f commit 3df97e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
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
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 %}

0 comments on commit 3df97e3

Please sign in to comment.