-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from ACCESS-NRI/64-catalog-display
Added a _display module with a singleton to configure the display options.
- Loading branch information
Showing
3 changed files
with
84 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |