From 4a442501923cf7a168d05478307ce4eb492be523 Mon Sep 17 00:00:00 2001 From: Michael Chow Date: Thu, 14 Dec 2023 15:34:40 -0500 Subject: [PATCH 1/2] fix: allow lists of styles and lists of locations --- great_tables/_tab_create_modify.py | 2 +- great_tables/_utils_render_html.py | 13 ++++--------- .../__snapshots__/test_utils_render_html.ambr | 18 ++++++++++++++++++ tests/test_utils_render_html.py | 12 ++++++++++++ 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/great_tables/_tab_create_modify.py b/great_tables/_tab_create_modify.py index 542ce6fb8..1153832b7 100644 --- a/great_tables/_tab_create_modify.py +++ b/great_tables/_tab_create_modify.py @@ -82,7 +82,7 @@ def tab_style( new_data = self for loc in locations: - new_data = set_style(loc, self, style) + new_data = set_style(loc, new_data, style) return new_data diff --git a/great_tables/_utils_render_html.py b/great_tables/_utils_render_html.py index dc1be0f0d..110f34eed 100644 --- a/great_tables/_utils_render_html.py +++ b/great_tables/_utils_render_html.py @@ -409,8 +409,6 @@ def create_columns_component_h(data: GTData) -> str: def create_body_component_h(data: GTData) -> str: - import pandas as pd - # for now, just coerce everything in the original data to a string # so we can fill in the body data with it _str_orig_data = cast_frame_to_string(data._tbl_data) @@ -482,13 +480,10 @@ def create_body_component_h(data: GTData) -> str: # Develop the `style` attribute for the current cell if len(styles_i) > 0: - style_entries = [x.styles for x in styles_i] - cell_styles_list: List[str] = [] - for style in style_entries: - style_element = style[0] - rendered_style = style_element._to_html_style() - cell_styles_list.append(rendered_style) - cell_styles = f'style="{" ".join(cell_styles_list)}"' + " " + # flatten all StyleInfo.styles lists + style_entries = list(chain(*[x.styles for x in styles_i])) + rendered_styles = [el._to_html_style() for el in style_entries] + cell_styles = f'style="{" ".join(rendered_styles)}"' + " " else: cell_styles = "" diff --git a/tests/__snapshots__/test_utils_render_html.ambr b/tests/__snapshots__/test_utils_render_html.ambr index 7de77b5ed..942f3d080 100644 --- a/tests/__snapshots__/test_utils_render_html.ambr +++ b/tests/__snapshots__/test_utils_render_html.ambr @@ -1,4 +1,22 @@ # serializer version: 1 +# name: test_body_multiple_locations + ''' + + + 0.1111 + apricot + + + 2.222 + banana + + + 33.33 + coconut + + + ''' +# --- # name: test_source_notes_snap ''' diff --git a/tests/test_utils_render_html.py b/tests/test_utils_render_html.py index b59c9ee4d..29cc6b721 100644 --- a/tests/test_utils_render_html.py +++ b/tests/test_utils_render_html.py @@ -30,6 +30,18 @@ def test_source_notes_snap(snapshot): assert_rendered_source_notes(snapshot, new_gt) +def test_body_multiple_locations(snapshot): + new_gt = GT(small_exibble).tab_style( + style=style.fill(color="red"), + locations=[ + loc.body(columns="num", rows=[0, 2]), + loc.body(columns="char", rows=[1]), + ], + ) + + assert_rendered_body(snapshot, new_gt) + + def test_styling_data_01(snapshot): new_gt = GT(small_exibble).tab_style( style=style.text(color="red"), From 74facd1fc474d09a5a9ddb2056a486a1fb0efc6b Mon Sep 17 00:00:00 2001 From: Michael Chow Date: Thu, 14 Dec 2023 15:50:41 -0500 Subject: [PATCH 2/2] tests: snapshot of rendering html for multiple styles --- .../__snapshots__/test_utils_render_html.ambr | 18 ++++++++++++++++++ tests/test_utils_render_html.py | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/tests/__snapshots__/test_utils_render_html.ambr b/tests/__snapshots__/test_utils_render_html.ambr index 942f3d080..d0eba6311 100644 --- a/tests/__snapshots__/test_utils_render_html.ambr +++ b/tests/__snapshots__/test_utils_render_html.ambr @@ -17,6 +17,24 @@ ''' # --- +# name: test_body_multiple_styles + ''' + + + 0.1111 + apricot + + + 2.222 + banana + + + 33.33 + coconut + + + ''' +# --- # name: test_source_notes_snap ''' diff --git a/tests/test_utils_render_html.py b/tests/test_utils_render_html.py index 29cc6b721..db2c302b7 100644 --- a/tests/test_utils_render_html.py +++ b/tests/test_utils_render_html.py @@ -42,6 +42,15 @@ def test_body_multiple_locations(snapshot): assert_rendered_body(snapshot, new_gt) +def test_body_multiple_styles(snapshot): + new_gt = GT(small_exibble).tab_style( + style=[style.fill(color="red"), style.borders("left")], + locations=loc.body(columns="num", rows=[0]), + ) + + assert_rendered_body(snapshot, new_gt) + + def test_styling_data_01(snapshot): new_gt = GT(small_exibble).tab_style( style=style.text(color="red"),