Skip to content

ENH: styler.html.mathjax option #43283

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

Merged
merged 10 commits into from
Sep 1, 2021
3 changes: 3 additions & 0 deletions doc/source/user_guide/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@ styler.format.thousands None String representation for t
integers, and floating point and complex numbers.
styler.format.escape None Whether to escape "html" or "latex" special
characters in the display representation.
styler.html.mathjax True If set to False will render specific CSS classes to
table attributes that will prevent Mathjax from rendering
in Jupyter Notebook.
======================================= ============ ==================================


Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Styler
- :meth:`.Styler.to_latex` introduces keyword argument ``environment``, which also allows a specific "longtable" entry through a separate jinja2 template (:issue:`41866`).
- :meth:`.Styler.to_html` introduces keyword arguments ``sparse_index`` and ``sparse_columns`` (:issue:`41946`)
- Keyword argument ``level`` is added to :meth:`.Styler.hide_index` and :meth:`.Styler.hide_columns` for optionally controlling hidden levels in a MultiIndex (:issue:`25475`)
- Global options have been extended to configure default ``Styler`` properties including formatting options (:issue:`41395`)
- Global options have been extended to configure default ``Styler`` properties including formatting and mathjax options (:issue:`41395`)

There are also bug fixes and deprecations listed below.

Expand Down
12 changes: 10 additions & 2 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,11 +793,17 @@ def register_converter_cb(key):
A formatter object to be used as default within ``Styler.format``.
"""

styler_mathjax = """
: bool
If False will render special CSS classes to table attributes that indicate Mathjax
will not be used in Jupyter Notebook.
"""

with cf.config_prefix("styler"):
cf.register_option("sparse.index", True, styler_sparse_index_doc, validator=bool)
cf.register_option("sparse.index", True, styler_sparse_index_doc, validator=is_bool)

cf.register_option(
"sparse.columns", True, styler_sparse_columns_doc, validator=bool
"sparse.columns", True, styler_sparse_columns_doc, validator=is_bool
)

cf.register_option(
Expand Down Expand Up @@ -840,3 +846,5 @@ def register_converter_cb(key):
styler_formatter,
validator=is_instance_factory([type(None), dict, callable, str]),
)

cf.register_option("html.mathjax", True, styler_mathjax, validator=is_bool)
3 changes: 1 addition & 2 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ def _translate(self, sparse_index: bool, sparse_cols: bool, blank: str = " 
d.update({k: map})

table_attr = self.table_attributes
use_mathjax = get_option("display.html.use_mathjax")
if not use_mathjax:
if not get_option("styler.html.mathjax"):
Copy link
Contributor

Choose a reason for hiding this comment

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

so this is technically a change in the option name? (its ok to merge this, just mention in the whatsnew)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done. I will edit the whats new before 1.4.0 release.

table_attr = table_attr or ""
if 'class="' in table_attr:
table_attr = table_attr.replace('class="', 'class="tex2jax_ignore ')
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,10 @@ def test_repr_html_ok(self):
self.styler._repr_html_()

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

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

def test_update_ctx(self):
Expand Down