Skip to content

Commit

Permalink
working on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timkpaine committed May 15, 2024
1 parent 61bd42b commit 04788fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
25 changes: 16 additions & 9 deletions great_tables/_utils_render_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ def _flatten_styles(styles: Styles, wrap: bool = False) -> str:
# TODO dedupe rendered styles in sequence

if wrap:
style_to_return = f'style="{" ".join(rendered_styles)}"' + " "
else:
style_to_return = " ".join(rendered_styles)
return style_to_return
if rendered_styles:
# return style html attribute
return f'style="{" ".join(rendered_styles)}"' + " "
# if no rendered styles, just return a blank
return ""
if rendered_styles:
# return space-separated list of rendered styles
return " ".join(rendered_styles)
# if not wrapping the styles for html element,
# return None so htmltools omits a style attribute
return None


def create_heading_component_h(data: GTData) -> StringBuilder:
Expand Down Expand Up @@ -64,20 +71,20 @@ def create_heading_component_h(data: GTData) -> StringBuilder:
if has_subtitle:
heading = f"""
<tr>
<th colspan="{n_cols_total}" class="gt_heading gt_title gt_font_normal" {title_style}>{title}</th>
<th colspan="{n_cols_total}" class="gt_heading gt_title gt_font_normal"{title_style}>{title}</th>
</tr>
<tr>
<th colspan="{n_cols_total}" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border" {subtitle_style}>{subtitle}</th>
<th colspan="{n_cols_total}" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border"{subtitle_style}>{subtitle}</th>
</tr>"""
else:
heading = f"""
<tr>
<th colspan="{n_cols_total}" class="gt_heading gt_title gt_font_normal" {title_style}>{title}</th>
<th colspan="{n_cols_total}" class="gt_heading gt_title gt_font_normal"{title_style}>{title}</th>
</tr>"""

result.append(heading)

return StringBuilder(f"""<thead class="gt_header" {header_style}>""", result, "\n</thead>")
return StringBuilder(f"""<thead class="gt_header"{header_style}>""", result, "\n</thead>")


def create_columns_component_h(data: GTData) -> str:
Expand Down Expand Up @@ -490,7 +497,7 @@ def create_body_component_h(data: GTData) -> str:

if is_stub_cell:
body_cells.append(
f""" <th {stub_style} class="gt_row gt_left gt_stub">{cell_str}</th>"""
f""" <th {stub_style}class="gt_row gt_left gt_stub">{cell_str}</th>"""
)
else:
body_cells.append(
Expand Down
8 changes: 4 additions & 4 deletions tests/test_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from great_tables._locations import (
CellPos,
LocBody,
LocColumnSpanners,
LocSpannerLabel,
LocTitle,
resolve,
resolve_cols_i,
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_resolve_column_spanners_simple():
ids = ["a", "b", "c"]

spanners = Spanners.from_ids(ids)
loc = LocColumnSpanners(ids=["a", "c"])
loc = LocSpannerLabel(ids=["a", "c"])

new_loc = resolve(loc, spanners)

Expand All @@ -93,7 +93,7 @@ def test_resolve_column_spanners_error_missing():
ids = ["a", "b", "c"]

spanners = Spanners.from_ids(ids)
loc = LocColumnSpanners(ids=["a", "d"])
loc = LocSpannerLabel(ids=["a", "d"])

with pytest.raises(ValueError):
resolve(loc, spanners)
Expand Down Expand Up @@ -121,7 +121,7 @@ def test_set_style_loc_body_from_column(expr):
new_gt = set_style(loc, gt_df, [style])

# 1 style info added
assert len(new_gt._styles) == 1
assert len(new_gt._styles) == 2
cell_info = new_gt._styles[0]

# style info has single cell style, with new color
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tab_create_modify.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_tab_style(gt: GT):
new_gt = tab_style(gt, style, LocBody(["x"], [0]))

assert len(gt._styles) == 0
assert len(new_gt._styles) == 1
assert len(new_gt._styles) == 2

assert len(new_gt._styles[0].styles) == 1
assert new_gt._styles[0].styles[0] is style
Expand Down

0 comments on commit 04788fa

Please sign in to comment.