Skip to content

Commit

Permalink
add ALLOW_NOTEABLE_ATTRS setting (#122)
Browse files Browse the repository at this point in the history
* update metadata if `noteable` key is present
  • Loading branch information
shouples committed Jan 6, 2023
1 parent b7ca3e4 commit 72525d7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/dx/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Settings(BaseSettings):
DB_LOCATION: str = ":memory:"

GENERATE_DEX_METADATA: bool = False
ALLOW_NOTEABLE_ATTRS: bool = True

@validator("RENDERABLE_OBJECTS", pre=True, always=True)
def validate_renderables(cls, vals):
Expand Down
3 changes: 1 addition & 2 deletions src/dx/types/dex_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ class DEXConditionalFormatRule(DEXBaseModel):
class DEXDecoration(BaseModel):
footer: str = ""
subtitle: str = ""
# TODO: change this back to "Table" before merging
title: str = "🐼 dx grid"
title: str = "Table"


class DEXDashboardViewConfig(BaseModel):
Expand Down
3 changes: 2 additions & 1 deletion src/dx/utils/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ def generate_metadata(
"display_id": display_id,
}

if settings.GENERATE_DEX_METADATA:
using_noteable_attrs = settings.ALLOW_NOTEABLE_ATTRS and "noteable" in df.attrs
if settings.GENERATE_DEX_METADATA or using_noteable_attrs:
metadata = add_dex_metadata(
display_id=display_id,
variable_name=variable_name,
Expand Down
40 changes: 39 additions & 1 deletion tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ def test_setting_disabled_will_not_update_metadata(
sample_random_dataframe.attrs = {"noteable": sample_dex_view_metadata}

params = dict(display_mode=display_mode, enable_datalink=datalink_enabled)
with settings_context(generate_dex_metadata=False, **params):
with settings_context(
generate_dex_metadata=False,
allow_noteable_attrs=False,
**params,
):
_, metadata = handle_format(sample_random_dataframe, ipython_shell=get_ipython)
display_metadata = metadata[settings.MEDIA_TYPE]
assert "dx" not in display_metadata
Expand Down Expand Up @@ -143,6 +147,40 @@ def test_dex_metadata_parsing(
assert "views" in metadata["dx"]
assert metadata["dx"][key] == dex_metadata_dict[key]

@pytest.mark.parametrize("allow_noteable_attrs", [True, False])
def test_noteable_attrs(
self,
sample_random_dataframe: pd.DataFrame,
allow_noteable_attrs: bool,
sample_dex_view_metadata: DEXView,
):
"""
Test that calling generate_metadata() can properly update
DEX metadata given a key/value pair belonging to the
"noteable" key as long as settings.ALLOW_NOTEABLE_ATTRS is True.
"""
sample_random_dataframe.attrs = {"noteable": sample_dex_view_metadata}
display_id = str(uuid.uuid4())
with settings_context(
generate_dex_metadata=False,
allow_noteable_attrs=allow_noteable_attrs,
):
metadata = generate_metadata(
sample_random_dataframe,
display_id,
extra_metadata={"noteable": {"decoration": {"title": "test title"}}},
)
if allow_noteable_attrs:
assert "dx" in metadata
assert "views" in metadata["dx"]
assert len(metadata["dx"]["views"]) == 1
assert (
metadata["dx"]["views"][0]["decoration"]["title"]
== sample_dex_view_metadata.decoration.title
)
else:
assert "dx" not in metadata


class TestPlottingMetadata:
pass

0 comments on commit 72525d7

Please sign in to comment.