Skip to content

Commit

Permalink
Merge pull request #65 from ACCESS-NRI/64-catalog-display
Browse files Browse the repository at this point in the history
Added a _display module with a singleton to configure the display options.
  • Loading branch information
rbeucher authored Nov 6, 2024
2 parents 7430da6 + 2d2e14a commit b273fca
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
65 changes: 65 additions & 0 deletions src/intake_dataframe_catalog/_display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from enum import Enum


class _DisplayType(Enum):
JUPYTER_NOTEBOOK = 0
IPYTHON_REPL = 1
REGULAR_REPL = 2


class _DisplayOptions:
"""
Singleton class to set display options for Pandas DataFrames.
"""

_instance = None

def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super().__new__(cls)
return cls._instance

def __init__(self):
if hasattr(self, "initialized"):
return None
self.set_pd_options()
self.initialized = True

def __str__(self):
return f"DisplayOptions(display_type={self.display_type})"

def __repr__(self):
return str(self)

def set_pd_options(self) -> None:
"""
Set display.max_colwidth to 200 and max_rows to None - but only if we are
in a Jupyter Notebook. Otherwise, leave the defaults.
"""

if self.display_type == _DisplayType.JUPYTER_NOTEBOOK:
import pandas as pd

pd.set_option("display.max_colwidth", 200)
pd.set_option("display.max_rows", None)

return None

@property
def display_type(self) -> _DisplayType:
try:
# Check for Jupyter Notebook
ipy = get_ipython()
if hasattr(ipy, "kernel"):
return _DisplayType.JUPYTER_NOTEBOOK
elif hasattr(ipy, "config"):
return _DisplayType.IPYTHON_REPL
except NameError:
return _DisplayType.REGULAR_REPL

@property
def is_notebook(self) -> bool:
return self.display_type == _DisplayType.JUPYTER_NOTEBOOK


display_options = _DisplayOptions()
13 changes: 7 additions & 6 deletions src/intake_dataframe_catalog/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
from intake.catalog.local import LocalCatalogEntry

from . import __version__
from ._display import display_options as _display_opts
from ._search import search

pd.set_option("display.max_colwidth", 200)
pd.set_option("display.max_rows", None)


class DfFileCatalogError(Exception):
pass
Expand Down Expand Up @@ -194,10 +192,13 @@ def _ipython_display_(self):
"""
Display the dataframe catalog object as a rich object in an IPython session.
"""
from IPython.display import HTML, display
if _display_opts.is_notebook:
from IPython.display import HTML, display

contents = self._repr_html_()
display(HTML(contents))
contents = self._repr_html_()
display(HTML(contents))
else:
print(self)

def keys(self) -> list[str]:
"""
Expand Down
12 changes: 12 additions & 0 deletions tests/test_display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from intake_dataframe_catalog._display import _DisplayOptions


def test_display_opts_singleton():
opts1 = _DisplayOptions()
opts2 = _DisplayOptions()
assert opts1 is opts2


# Create a test that checks if get_ipython() has a kernel attribute, then the display_type should be JUPYTER_NOTEBOOK
# If get_ipython() has a config attribute, then the display_type should be IPYTHON_REPL
# If get_ipython() raises a NameError, then the display_type should be REGULAR_REPL

0 comments on commit b273fca

Please sign in to comment.