-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor IPython display formatter usage (#1)
- Loading branch information
Showing
6 changed files
with
189 additions
and
90 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
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 |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
from .dx import * | ||
from .formatters import * | ||
|
||
__version__ = "1.0.2" | ||
__version__ = "1.0.3" |
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 |
---|---|---|
@@ -1,48 +1,48 @@ | ||
import pandas as pd | ||
from IPython import get_ipython | ||
from IPython.core.formatters import BaseFormatter | ||
from IPython.core.formatters import DisplayFormatter | ||
from pandas.io.json import build_table_schema | ||
|
||
from .dx import DATARESOURCE_MEDIA_TYPE, DX_MEDIA_TYPE | ||
DEFAULT_IPYTHON_DISPLAY_FORMATTER = get_ipython().display_formatter | ||
DX_MEDIA_TYPE = "application/vnd.dex.v1+json" | ||
|
||
|
||
class DXSchemaFormatter(BaseFormatter): | ||
# FOLLOWUP: does anything need to change here? | ||
print_method = "_repr_data_resource_" | ||
_return_type = (dict,) | ||
class DXDisplayFormatter(DisplayFormatter): | ||
def format(self, obj, **kwargs): | ||
|
||
if isinstance(obj, pd.DataFrame): | ||
return format_dx(obj) | ||
|
||
class TableSchemaFormatter(BaseFormatter): | ||
print_method = "_repr_data_resource_" | ||
_return_type = (dict,) | ||
return DEFAULT_IPYTHON_DISPLAY_FORMATTER.format(obj, **kwargs) | ||
|
||
|
||
def deregister_dx_formatting(media_type: str = DX_MEDIA_TYPE) -> None: | ||
"""Reverts IPython.display_formatter.formatters to original states""" | ||
pd.options.display.html.table_schema = False | ||
pd.options.display.max_rows = 60 | ||
def format_dx(df) -> tuple: | ||
""" | ||
Transforms the dataframe to a payload dictionary containing the table schema | ||
and column values as arrays. | ||
""" | ||
# this will include the `df.index` by default (e.g. slicing/sampling) | ||
payload = { | ||
DX_MEDIA_TYPE: { | ||
"schema": build_table_schema(df), | ||
"data": df.reset_index().transpose().values.tolist(), | ||
} | ||
} | ||
metadata = {} | ||
return (payload, metadata) | ||
|
||
formatters = get_ipython().display_formatter.formatters | ||
if media_type in formatters: | ||
formatters.pop(media_type) | ||
|
||
# this should effectively be the same as using | ||
# `pandas.io.formats.printing.enable_data_resource_formatter(True)`, | ||
# except calling that directly doesn't update the IPython formatters | ||
formatters[DATARESOURCE_MEDIA_TYPE] = TableSchemaFormatter() | ||
formatters[DATARESOURCE_MEDIA_TYPE].enabled = True | ||
def deregister() -> None: | ||
"""Reverts IPython.display_formatter to its original state""" | ||
pd.options.display.max_rows = 60 | ||
get_ipython().display_formatter = DEFAULT_IPYTHON_DISPLAY_FORMATTER | ||
|
||
|
||
def register_dx_formatter(media_type: str = DX_MEDIA_TYPE) -> None: | ||
"""Registers a media_type for IPython display formatting""" | ||
pd.options.display.html.table_schema = True | ||
def register() -> None: | ||
"""Overrides the default IPython display formatter to use DXDisplayFormatter""" | ||
pd.options.display.max_rows = 100_000 | ||
|
||
formatters = get_ipython().display_formatter.formatters | ||
formatters[media_type] = DXSchemaFormatter() | ||
# the default pandas `Dataframe._repl_html_` will not work correctly | ||
# if enabled=True here | ||
formatters[media_type].enabled = False | ||
get_ipython().display_formatter = DXDisplayFormatter() | ||
|
||
|
||
disable = deregister_dx_formatting | ||
enable = register_dx_formatter | ||
disable = deregister | ||
enable = register |
Oops, something went wrong.