Skip to content

Commit

Permalink
Make the indirect dependency on jinja2 optional (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 7, 2023
1 parent a44c644 commit 500d36c
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ jobs:
- python-version: "3.11"
pandas-version: pre
polars: true
- python-version: "3.11"
uninstall_jinja2: true
runs-on: ubuntu-20.04
steps:
- name: Checkout
Expand Down Expand Up @@ -83,6 +85,10 @@ jobs:
if: matrix.polars
run: pip install -e .[polars]

- name: Uninstall jinja2
if: matrix.uninstall_jinja2
run: pip uninstall jinja2 -y

- name: Install a Jupyter Kernel
run: python -m ipykernel install --name itables --user

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Interactive Tables

![CI](https://github.com/mwouts/itables/workflows/CI/badge.svg)
![CI](https://github.com/mwouts/itables/actions/workflows/continuous-integration.yml/badge.svg?branch=main)
[![codecov.io](https://codecov.io/github/mwouts/itables/coverage.svg?branch=main)](https://codecov.io/github/mwouts/itables?branch=main)
[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/mwouts/itables.svg)](https://lgtm.com/projects/g/mwouts/itables/context:python)
[![Pypi](https://img.shields.io/pypi/v/itables.svg)](https://pypi.python.org/pypi/itables)
Expand Down
4 changes: 2 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
codecov:
notify:
after_n_builds: 15
after_n_builds: 10

comment:
after_n_builds: 15
after_n_builds: 10

coverage:
status:
Expand Down
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
ITables ChangeLog
=================

1.6.2 (2023-10-07)
------------------

**Fixed**
- We have removed an indirect dependency on `jinja2` caused by the Pandas style objects ([#202](https://github.com/mwouts/itables/issues/202))


1.6.1 (2023-10-01)
------------------

Expand Down
2 changes: 1 addition & 1 deletion docs/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ kernelspec:

# Quick Start

![CI](https://github.com/mwouts/itables/workflows/CI/badge.svg)
![CI](https://github.com/mwouts/itables/actions/workflows/continuous-integration.yml/badge.svg?branch=main)
[![codecov.io](https://codecov.io/github/mwouts/itables/coverage.svg?branch=main)](https://codecov.io/github/mwouts/itables?branch=main)
[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/mwouts/itables.svg)](https://lgtm.com/projects/g/mwouts/itables/context:python)
[![Pypi](https://img.shields.io/pypi/v/itables.svg)](https://pypi.python.org/pypi/itables)
Expand Down
20 changes: 14 additions & 6 deletions itables/javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

import numpy as np
import pandas as pd
import pandas.io.formats.style as pd_style

try:
import pandas.io.formats.style as pd_style
except ImportError:
pd_style = None

try:
import polars as pl
Expand Down Expand Up @@ -40,7 +44,9 @@
"warn_on_int_to_str_conversion",
}
_ORIGINAL_DATAFRAME_REPR_HTML = pd.DataFrame._repr_html_
_ORIGINAL_DATAFRAME_STYLE_REPR_HTML = pd_style.Styler._repr_html_
_ORIGINAL_DATAFRAME_STYLE_REPR_HTML = (
None if pd_style is None else pd_style.Styler._repr_html_
)
_ORIGINAL_POLARS_DATAFRAME_REPR_HTML = pl.DataFrame._repr_html_
_CONNECTED = True

Expand Down Expand Up @@ -92,12 +98,14 @@ def init_notebook_mode(
if all_interactive:
pd.DataFrame._repr_html_ = _datatables_repr_
pd.Series._repr_html_ = _datatables_repr_
pd_style.Styler._repr_html_ = _datatables_repr_
if pd_style is not None:
pd_style.Styler._repr_html_ = _datatables_repr_
pl.DataFrame._repr_html_ = _datatables_repr_
pl.Series._repr_html_ = _datatables_repr_
else:
pd.DataFrame._repr_html_ = _ORIGINAL_DATAFRAME_REPR_HTML
pd_style.Styler._repr_html_ = _ORIGINAL_DATAFRAME_STYLE_REPR_HTML
if pd_style is not None:
pd_style.Styler._repr_html_ = _ORIGINAL_DATAFRAME_STYLE_REPR_HTML
pl.DataFrame._repr_html_ = _ORIGINAL_POLARS_DATAFRAME_REPR_HTML
if hasattr(pd.Series, "_repr_html_"):
del pd.Series._repr_html_
Expand Down Expand Up @@ -267,7 +275,7 @@ def to_html_datatable(
use_to_html=False,
**kwargs
):
if use_to_html or isinstance(df, pd_style.Styler):
if use_to_html or (pd_style is not None and isinstance(df, pd_style.Styler)):
return to_html_datatable_using_to_html(
df=df,
caption=caption,
Expand Down Expand Up @@ -445,7 +453,7 @@ def to_html_datatable_using_to_html(
# default UUID in Pandas styler objects has uuid_len=5
or str(uuid.uuid4())[:5]
)
if isinstance(df, pd_style.Styler):
if pd_style is not None and isinstance(df, pd_style.Styler):
if not showIndex:
try:
df = df.hide()
Expand Down
2 changes: 1 addition & 1 deletion itables/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""ITables' version number"""

__version__ = "1.6.1"
__version__ = "1.6.2"
3 changes: 3 additions & 0 deletions tests/test_documentation_notebooks_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

from itables import init_notebook_mode
from itables.javascript import pd_style

try:
import polars as pl
Expand All @@ -27,6 +28,8 @@ def list_doc_notebooks():
def test_run_documentation_notebooks(notebook):
if "polars" in notebook.stem and pl is None:
pytest.skip(msg="Polars is not available")
if "pandas_style" in notebook.stem and pd_style is None:
pytest.skip(msg="Pandas Style is not available")

nb = jupytext.read(notebook)
py_notebook = jupytext.writes(nb, "py:percent")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_sample_dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from itables import show, to_html_datatable
from itables.datatables_format import _format_column, generate_encoder
from itables.javascript import pd_style
from itables.sample_dfs import (
COLUMN_TYPES,
PANDAS_VERSION_MAJOR,
Expand Down Expand Up @@ -58,6 +59,10 @@ def test_get_indicators(connected, use_to_html):
sys.version_info < (3, 7),
reason="AttributeError: 'Styler' object has no attribute 'to_html'",
)
@pytest.mark.skipif(
pd_style is None,
reason="Missing optional dependency 'Jinja2'. DataFrame.style requires jinja2.",
)
def test_get_pandas_styler(connected, use_to_html):
styler = get_pandas_styler()
show(styler, connected=connected, use_to_html=use_to_html)
Expand Down

0 comments on commit 500d36c

Please sign in to comment.