flexablelib 2.0.9__tar.gz → 2.0.11__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {flexablelib-2.0.9 → flexablelib-2.0.11}/CHANGELOG.md +16 -0
- {flexablelib-2.0.9/flexablelib.egg-info → flexablelib-2.0.11}/PKG-INFO +2 -2
- {flexablelib-2.0.9 → flexablelib-2.0.11}/README.md +1 -1
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/colors.py +98 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/merges.py +68 -5
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/style_compositions.py +23 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/table.py +207 -15
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/writers.py +144 -9
- {flexablelib-2.0.9 → flexablelib-2.0.11/flexablelib.egg-info}/PKG-INFO +2 -2
- {flexablelib-2.0.9 → flexablelib-2.0.11}/package.json +1 -1
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tests/test_features.py +83 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tests/test_styles.py +1 -1
- {flexablelib-2.0.9 → flexablelib-2.0.11}/LICENSE +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/MANIFEST.in +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/__init__.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/_speedups.c +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/_width_tables.h +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/cells.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/columns.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/fast.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/options.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/readers.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/table_strings.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/text_width.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib/utils.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib.egg-info/SOURCES.txt +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib.egg-info/dependency_links.txt +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib.egg-info/requires.txt +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/flexablelib.egg-info/top_level.txt +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/setup.cfg +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/setup.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tests/test_cells.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tests/test_columns.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tests/test_excel.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tests/test_issues.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tests/test_merge_edge_matrix.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tests/test_merges.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tests/test_readme_examples.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tests/test_text_width.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tests/test_two_pass_measurement.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tests/test_upstream_issues.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tools/benchmark.py +0 -0
- {flexablelib-2.0.9 → flexablelib-2.0.11}/tools/generate_width_tables.py +0 -0
|
@@ -2,6 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [2.0.11] - 2026-10-28
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
- Horizontal rule cell backgrounds: dash runs on H-separators inherit the cell-above fill so coloured cells meet ─/═ without a dark band (generic; recommended with `flush`).
|
|
9
|
+
- `paint_rule_cell_backgrounds` helper; ANSI-aware junction glyph detection for painted rules.
|
|
10
|
+
|
|
11
|
+
## [2.0.10] - 2026-10-28
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
- ``flush`` style: full unicode grid whose cell backgrounds meet the border
|
|
15
|
+
glyphs with no interior gap and never paint over them (pad-then-paint
|
|
16
|
+
contract; recommended for coloured cells and coloured merges).
|
|
17
|
+
- ``merge_cells(..., color=...)`` paints the whole merge span (ANSI + HTML +
|
|
18
|
+
Excel). When omitted, merges inherit the top-left cell's row/column/cell
|
|
19
|
+
colour so existing colour APIs fill merges instead of leaving dark holes.
|
|
20
|
+
|
|
5
21
|
## [2.0.9] - 2026-10-28
|
|
6
22
|
|
|
7
23
|
### Added
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flexablelib
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.11
|
|
4
4
|
Summary: Flexible console and HTML tables for Python
|
|
5
5
|
Home-page: https://github.com/Opus-Perpetuus/flexablelib
|
|
6
6
|
Download-URL:
|
|
@@ -355,7 +355,7 @@ flexablelib/ # GitHub repository
|
|
|
355
355
|
├── docs/ issue history and design notes
|
|
356
356
|
├── logos/ brand assets — see logos/README.md
|
|
357
357
|
├── ARCHITECTURE.md how it all fits together
|
|
358
|
-
├── style_examples.md all
|
|
358
|
+
├── style_examples.md all 44 styles rendered
|
|
359
359
|
├── package.json canonical version number; drives the release
|
|
360
360
|
└── setup.py reads the version from package.json
|
|
361
361
|
```
|
|
@@ -306,7 +306,7 @@ flexablelib/ # GitHub repository
|
|
|
306
306
|
├── docs/ issue history and design notes
|
|
307
307
|
├── logos/ brand assets — see logos/README.md
|
|
308
308
|
├── ARCHITECTURE.md how it all fits together
|
|
309
|
-
├── style_examples.md all
|
|
309
|
+
├── style_examples.md all 44 styles rendered
|
|
310
310
|
├── package.json canonical version number; drives the release
|
|
311
311
|
└── setup.py reads the version from package.json
|
|
312
312
|
```
|
|
@@ -193,3 +193,101 @@ __all__ = [
|
|
|
193
193
|
'ATTRIBUTES',
|
|
194
194
|
'RESET',
|
|
195
195
|
]
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
# Named ANSI background → approximate sRGB for HTML / Excel fills.
|
|
199
|
+
_BG_HEX = {
|
|
200
|
+
'black': '1F2937',
|
|
201
|
+
'red': 'DC2626',
|
|
202
|
+
'green': '16A34A',
|
|
203
|
+
'yellow': 'CA8A04',
|
|
204
|
+
'blue': '2563EB',
|
|
205
|
+
'magenta': 'C026D3',
|
|
206
|
+
'cyan': '0891B2',
|
|
207
|
+
'white': 'F3F4F6',
|
|
208
|
+
'bright_black': '4B5563',
|
|
209
|
+
'grey': '6B7280',
|
|
210
|
+
'gray': '6B7280',
|
|
211
|
+
'bright_red': 'EF4444',
|
|
212
|
+
'bright_green': '22C55E',
|
|
213
|
+
'bright_yellow': 'EAB308',
|
|
214
|
+
'bright_blue': '3B82F6',
|
|
215
|
+
'bright_magenta': 'E879F9',
|
|
216
|
+
'bright_cyan': '22D3EE',
|
|
217
|
+
'bright_white': 'FFFFFF',
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
_FG_HEX = {
|
|
221
|
+
'black': '111827',
|
|
222
|
+
'red': '991B1B',
|
|
223
|
+
'green': '14532D',
|
|
224
|
+
'yellow': '713F12',
|
|
225
|
+
'blue': '1E3A8A',
|
|
226
|
+
'magenta': '86198F',
|
|
227
|
+
'cyan': '155E75',
|
|
228
|
+
'white': 'F9FAFB',
|
|
229
|
+
'bright_black': '374151',
|
|
230
|
+
'grey': '6B7280',
|
|
231
|
+
'gray': '6B7280',
|
|
232
|
+
'bright_red': 'B91C1C',
|
|
233
|
+
'bright_green': '15803D',
|
|
234
|
+
'bright_yellow': 'A16207',
|
|
235
|
+
'bright_blue': '1D4ED8',
|
|
236
|
+
'bright_magenta': 'A21CAF',
|
|
237
|
+
'bright_cyan': '0E7490',
|
|
238
|
+
'bright_white': 'FFFFFF',
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def _split_spec(spec: Union[str, int, None]):
|
|
243
|
+
if spec is None or spec == '':
|
|
244
|
+
return [], None
|
|
245
|
+
if isinstance(spec, int):
|
|
246
|
+
return [], None
|
|
247
|
+
foreground_part, _, background_part = str(spec).lower().partition(' on ')
|
|
248
|
+
fg_tokens = foreground_part.split()
|
|
249
|
+
bg_token = None
|
|
250
|
+
if background_part:
|
|
251
|
+
parts = background_part.split()
|
|
252
|
+
bg_token = parts[0] if parts else None
|
|
253
|
+
return fg_tokens, bg_token
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def background_hex(spec: Union[str, int, None]) -> Optional[str]:
|
|
257
|
+
"""sRGB hex (no ``#``) for the background half of a colour spec, if any."""
|
|
258
|
+
_fg, bg = _split_spec(spec)
|
|
259
|
+
if bg is None:
|
|
260
|
+
return None
|
|
261
|
+
hex_match = _HEX_RE.match(bg)
|
|
262
|
+
if hex_match:
|
|
263
|
+
return hex_match.group(1).upper()
|
|
264
|
+
return _BG_HEX.get(bg)
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def foreground_hex(spec: Union[str, int, None]) -> Optional[str]:
|
|
268
|
+
"""sRGB hex (no ``#``) for the first named foreground colour in a spec."""
|
|
269
|
+
fg_tokens, _bg = _split_spec(spec)
|
|
270
|
+
for token in fg_tokens:
|
|
271
|
+
if token in ATTRIBUTES:
|
|
272
|
+
continue
|
|
273
|
+
hex_match = _HEX_RE.match(token)
|
|
274
|
+
if hex_match:
|
|
275
|
+
return hex_match.group(1).upper()
|
|
276
|
+
if token in _FG_HEX:
|
|
277
|
+
return _FG_HEX[token]
|
|
278
|
+
return None
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def spec_to_css(spec: Union[str, int, None]) -> str:
|
|
282
|
+
"""Inline CSS ``color`` / ``background-color`` from a colour spec."""
|
|
283
|
+
parts = []
|
|
284
|
+
bg = background_hex(spec)
|
|
285
|
+
fg = foreground_hex(spec)
|
|
286
|
+
if bg:
|
|
287
|
+
parts.append(f'background-color:#{bg}')
|
|
288
|
+
if fg:
|
|
289
|
+
parts.append(f'color:#{fg}')
|
|
290
|
+
fg_tokens, _ = _split_spec(spec)
|
|
291
|
+
if 'bold' in fg_tokens:
|
|
292
|
+
parts.append('font-weight:600')
|
|
293
|
+
return ';'.join(parts)
|
|
@@ -19,11 +19,13 @@ than the span it was given is truncated, not accommodated.
|
|
|
19
19
|
import re
|
|
20
20
|
from typing import List, NamedTuple, Optional
|
|
21
21
|
|
|
22
|
+
from .colors import background_hex, colorize
|
|
22
23
|
from .fast import (
|
|
23
24
|
has_ansi,
|
|
24
25
|
pad_to_width,
|
|
25
26
|
partition_by_width,
|
|
26
27
|
slice_by_width,
|
|
28
|
+
strip_ansi,
|
|
27
29
|
strip_by_width,
|
|
28
30
|
truncate_to_width,
|
|
29
31
|
visible_width,
|
|
@@ -51,6 +53,11 @@ class MergedRegion(NamedTuple):
|
|
|
51
53
|
|
|
52
54
|
Coordinates are inclusive and zero-based, in the table's own row and
|
|
53
55
|
column numbering -- the index column, when shown, is not counted.
|
|
56
|
+
|
|
57
|
+
``color`` is an optional colour spec (same language as ``column_colors`` /
|
|
58
|
+
``row_colors``). When set, the whole painted span -- text, padding and
|
|
59
|
+
blank continuation lines -- carries that background so a merge reads as
|
|
60
|
+
one solid fill rather than a dark hole.
|
|
54
61
|
"""
|
|
55
62
|
|
|
56
63
|
first_row: int
|
|
@@ -59,6 +66,7 @@ class MergedRegion(NamedTuple):
|
|
|
59
66
|
last_column: int
|
|
60
67
|
value: Optional[str]
|
|
61
68
|
align: str
|
|
69
|
+
color: Optional[str] = None
|
|
62
70
|
|
|
63
71
|
@property
|
|
64
72
|
def row_count(self) -> int:
|
|
@@ -82,7 +90,7 @@ class MergedRegion(NamedTuple):
|
|
|
82
90
|
|
|
83
91
|
|
|
84
92
|
def normalise(first_row, first_column, last_row, last_column, value, align,
|
|
85
|
-
row_count, column_count, existing):
|
|
93
|
+
row_count, column_count, existing, color=None):
|
|
86
94
|
"""
|
|
87
95
|
Validate a merge request and turn it into a MergedRegion.
|
|
88
96
|
|
|
@@ -115,7 +123,7 @@ def normalise(first_row, first_column, last_row, last_column, value, align,
|
|
|
115
123
|
raise ValueError(f"align must be 'l', 'r' or 'c', not {align!r}")
|
|
116
124
|
|
|
117
125
|
region = MergedRegion(first_row, first_column, last_row, last_column,
|
|
118
|
-
value, align)
|
|
126
|
+
value, align, color)
|
|
119
127
|
|
|
120
128
|
for other in existing:
|
|
121
129
|
if region.overlaps(other):
|
|
@@ -267,13 +275,22 @@ _ASCII = {
|
|
|
267
275
|
}
|
|
268
276
|
|
|
269
277
|
|
|
278
|
+
def _glyph(ch: str) -> str:
|
|
279
|
+
"""Visible character from a slice that may still carry SGR wraps."""
|
|
280
|
+
if not ch:
|
|
281
|
+
return ''
|
|
282
|
+
plain = strip_ansi(ch)
|
|
283
|
+
return plain[:1] if plain else ''
|
|
284
|
+
|
|
285
|
+
|
|
270
286
|
def _is_h(ch: str) -> bool:
|
|
271
|
-
return ch in _H_CHARS
|
|
287
|
+
return _glyph(ch) in _H_CHARS
|
|
272
288
|
|
|
273
289
|
|
|
274
290
|
def _visible_vertical(ch: str) -> bool:
|
|
275
291
|
"""True when a style's vertical glyph is a real column rule, not a spacer."""
|
|
276
|
-
|
|
292
|
+
g = _glyph(ch)
|
|
293
|
+
return bool(g) and not g.isspace()
|
|
277
294
|
|
|
278
295
|
|
|
279
296
|
def _char_at(line: str, index: int) -> str:
|
|
@@ -480,7 +497,7 @@ def _repair_rule_junctions(line, layout, composition, h_glyph, missing_south=Non
|
|
|
480
497
|
continue
|
|
481
498
|
here = _char_at(rewritten, x)
|
|
482
499
|
# Interior of a multi-column vertical blank: junction was wiped to space.
|
|
483
|
-
if here
|
|
500
|
+
if _glyph(here) in ('', ' '):
|
|
484
501
|
continue
|
|
485
502
|
left = _char_at(rewritten, x - 1)
|
|
486
503
|
right = _char_at(rewritten, x + 1)
|
|
@@ -496,6 +513,45 @@ def _repair_rule_junctions(line, layout, composition, h_glyph, missing_south=Non
|
|
|
496
513
|
return rewritten
|
|
497
514
|
|
|
498
515
|
|
|
516
|
+
|
|
517
|
+
def paint_rule_cell_backgrounds(line, layout, column_specs, border_color=None):
|
|
518
|
+
"""
|
|
519
|
+
Paint each column's dash run on a horizontal rule with that column's cell
|
|
520
|
+
background.
|
|
521
|
+
|
|
522
|
+
Terminal grid tables put H-rules on their own row. Without a background,
|
|
523
|
+
that row is a dark band between coloured cells (the ─ glyph is only a thin
|
|
524
|
+
stroke). Colouring the dash run with the cell above makes the fill meet the
|
|
525
|
+
rule. Junctions and outer corners stay outside the cell fill (same contract
|
|
526
|
+
as vertical borders in ``flush``).
|
|
527
|
+
|
|
528
|
+
``column_specs`` is aligned with ``layout.spans`` (drawn columns). Specs
|
|
529
|
+
without a background are skipped. Paint right-to-left so ANSI insertion
|
|
530
|
+
does not shift later visible offsets.
|
|
531
|
+
"""
|
|
532
|
+
if not line:
|
|
533
|
+
return line
|
|
534
|
+
if not column_specs or not any(
|
|
535
|
+
spec and background_hex(spec) is not None for spec in column_specs):
|
|
536
|
+
return colorize(line, border_color) if border_color else line
|
|
537
|
+
|
|
538
|
+
rewritten = line
|
|
539
|
+
for (start, end), spec in reversed(list(zip(layout.spans, column_specs))):
|
|
540
|
+
if end <= start:
|
|
541
|
+
continue
|
|
542
|
+
if not spec or background_hex(spec) is None:
|
|
543
|
+
continue
|
|
544
|
+
before, chunk, after = partition_by_width(rewritten, start, end)
|
|
545
|
+
plain = strip_ansi(chunk) if has_ansi(chunk) else chunk
|
|
546
|
+
if not plain:
|
|
547
|
+
continue
|
|
548
|
+
painted = colorize(plain, spec)
|
|
549
|
+
if has_ansi(chunk) and not painted.startswith(_RESET):
|
|
550
|
+
painted = _RESET + painted
|
|
551
|
+
rewritten = before + painted + after
|
|
552
|
+
return rewritten
|
|
553
|
+
|
|
554
|
+
|
|
499
555
|
def apply(lines, row_line_map, regions, column_widths, composition,
|
|
500
556
|
index_offset, hidden_columns=(), filler=' '):
|
|
501
557
|
"""
|
|
@@ -581,6 +637,13 @@ def apply(lines, row_line_map, regions, column_widths, composition,
|
|
|
581
637
|
+ pad_to_width(text, span_width, region.align, filler)
|
|
582
638
|
+ filler * margin)
|
|
583
639
|
blank = filler * (end - start)
|
|
640
|
+
# Colour the entire span (text line + blank continuations) so a merge
|
|
641
|
+
# fill is flush across its rectangle -- same pad-then-paint contract
|
|
642
|
+
# as ordinary cells. Borders stay outside _repaint's range.
|
|
643
|
+
spec = getattr(region, 'color', None)
|
|
644
|
+
if spec:
|
|
645
|
+
painted = colorize(painted, spec)
|
|
646
|
+
blank = colorize(blank, spec)
|
|
584
647
|
|
|
585
648
|
text_line = target_lines[len(target_lines) // 2]
|
|
586
649
|
for line_index in target_lines:
|
|
@@ -133,6 +133,7 @@ Compositions = namedtuple(
|
|
|
133
133
|
|
|
134
134
|
# Fill-only (background colours, no border glyphs)
|
|
135
135
|
'blocks',
|
|
136
|
+
'flush',
|
|
136
137
|
]
|
|
137
138
|
)
|
|
138
139
|
|
|
@@ -890,5 +891,27 @@ __style_compositions = Compositions(
|
|
|
890
891
|
lines_with_indicator=[],
|
|
891
892
|
align_indicator_margin=0,
|
|
892
893
|
always_center_indicator=False
|
|
894
|
+
),
|
|
895
|
+
# Flush cell fills: full unicode grid whose cell backgrounds (pad-then-paint)
|
|
896
|
+
# meet the border glyphs with no interior gap and never paint over them.
|
|
897
|
+
# Horizontal rules also inherit the cell-above background on their dash
|
|
898
|
+
# runs so fills meet ─/═ without a dark band. Recommended for coloured
|
|
899
|
+
# cells and coloured merges. Same box as pretty_grid.
|
|
900
|
+
flush=TableComposition(
|
|
901
|
+
superior_header_line=SeparatorLine('╒', '═', '╤', '╕'),
|
|
902
|
+
inferior_header_line=SeparatorLine('╞', '═', '╪', '╡'),
|
|
903
|
+
superior_header_line_no_header=SeparatorLine('╒', '═', '╤', '╕'),
|
|
904
|
+
table_body_line=SeparatorLine('├', '─', '┼', '┤'),
|
|
905
|
+
table_end_line=SeparatorLine('╘', '═', '╧', '╛'),
|
|
906
|
+
vertical_header_lines=SeparatorLine('│', '│', None, '│'),
|
|
907
|
+
vertical_table_body_lines=SeparatorLine('│', '│', None, '│'),
|
|
908
|
+
margin=CELL_MARGIN,
|
|
909
|
+
align_sensitive=False,
|
|
910
|
+
align_indicator=None,
|
|
911
|
+
centered_indicator_on_sides=False,
|
|
912
|
+
left_and_right_when_centered=False,
|
|
913
|
+
lines_with_indicator=[],
|
|
914
|
+
align_indicator_margin=0,
|
|
915
|
+
always_center_indicator=False
|
|
893
916
|
)
|
|
894
917
|
)
|
|
@@ -55,7 +55,7 @@ from .cells import (
|
|
|
55
55
|
_wrap_rows,
|
|
56
56
|
_zip_wrapped_rows
|
|
57
57
|
)
|
|
58
|
-
from .colors import colorize, supports_color
|
|
58
|
+
from .colors import background_hex, colorize, supports_color
|
|
59
59
|
|
|
60
60
|
from copy import deepcopy
|
|
61
61
|
from typing import (
|
|
@@ -1995,7 +1995,7 @@ class Table(object):
|
|
|
1995
1995
|
return list(self.__merged_regions)
|
|
1996
1996
|
|
|
1997
1997
|
def merge_cells(self, first_row, first_column, last_row=None,
|
|
1998
|
-
last_column=None, value=None, align='c'):
|
|
1998
|
+
last_column=None, value=None, align='c', color=None):
|
|
1999
1999
|
"""
|
|
2000
2000
|
Render a rectangular block of cells as a single cell.
|
|
2001
2001
|
|
|
@@ -2007,9 +2007,15 @@ class Table(object):
|
|
|
2007
2007
|
table.merge_cells(0, 0, last_column=2) # across three columns
|
|
2008
2008
|
table.merge_cells(1, 0, last_row=3) # down four rows
|
|
2009
2009
|
table.merge_cells(0, 0, 2, 2, value='Total') # a 3x3 block
|
|
2010
|
+
table.merge_cells(0, 0, last_row=2, value='A',
|
|
2011
|
+
color='black on green') # coloured merge
|
|
2010
2012
|
|
|
2011
2013
|
``value`` replaces the text; left as None, the top-left cell's own
|
|
2012
2014
|
content is kept. ``align`` is 'l', 'r' or 'c' within the merged span.
|
|
2015
|
+
``color`` paints the whole merge span (text + padding + blank lines)
|
|
2016
|
+
with a colour spec; when omitted, the top-left cell's row/column/
|
|
2017
|
+
cell colour is inherited at render time so existing colour APIs light
|
|
2018
|
+
up merges without an extra argument.
|
|
2013
2019
|
|
|
2014
2020
|
Raises ValueError for a merge covering a single cell, coordinates
|
|
2015
2021
|
outside the table, or an overlap with a merge already in place.
|
|
@@ -2028,6 +2034,7 @@ class Table(object):
|
|
|
2028
2034
|
region = normalise(
|
|
2029
2035
|
first_row, first_column, last_row, last_column, value, align,
|
|
2030
2036
|
self.row_count, self.column_count, self.__merged_regions,
|
|
2037
|
+
color=color,
|
|
2031
2038
|
)
|
|
2032
2039
|
self.__merged_regions.append(region)
|
|
2033
2040
|
return region
|
|
@@ -2160,6 +2167,7 @@ class Table(object):
|
|
|
2160
2167
|
or self.__column_colors
|
|
2161
2168
|
or self.__row_colors
|
|
2162
2169
|
or self.__color_rule
|
|
2170
|
+
or any(getattr(r, 'color', None) for r in self.__merged_regions)
|
|
2163
2171
|
)
|
|
2164
2172
|
|
|
2165
2173
|
@staticmethod
|
|
@@ -3830,7 +3838,10 @@ class Table(object):
|
|
|
3830
3838
|
),
|
|
3831
3839
|
)
|
|
3832
3840
|
aligned_columns = self.__zip_columns(aligned_columns)
|
|
3833
|
-
# String separators and data rows are joined
|
|
3841
|
+
# String separators and data rows are joined. Horizontal rules stay
|
|
3842
|
+
# plain here; cell-background fills (and optional border_color) are
|
|
3843
|
+
# applied per segment below so coloured cells do not sit above a dark
|
|
3844
|
+
# H-rule band.
|
|
3834
3845
|
separators: HorizontalComposition = _get_separators(
|
|
3835
3846
|
self.__style_composition,
|
|
3836
3847
|
column_widths_list,
|
|
@@ -3838,11 +3849,6 @@ class Table(object):
|
|
|
3838
3849
|
self.__show_empty_columns,
|
|
3839
3850
|
column_alignments_list,
|
|
3840
3851
|
)
|
|
3841
|
-
if colors_on and self.__border_color:
|
|
3842
|
-
separators = HorizontalComposition(*[
|
|
3843
|
-
colorize(line, self.__border_color) if line is not None else None
|
|
3844
|
-
for line in separators
|
|
3845
|
-
])
|
|
3846
3852
|
empty_rows_for_render = (
|
|
3847
3853
|
self.__compose_empty_row_indexes
|
|
3848
3854
|
if self.__compose_empty_row_indexes is not None
|
|
@@ -3858,6 +3864,22 @@ class Table(object):
|
|
|
3858
3864
|
self.__border_color if colors_on else None,
|
|
3859
3865
|
)
|
|
3860
3866
|
|
|
3867
|
+
header_row = data_rows.header_row
|
|
3868
|
+
body_rows = data_rows.body_rows
|
|
3869
|
+
gap_separator = None
|
|
3870
|
+
if colors_on:
|
|
3871
|
+
separators, gap_separator = self.__colour_horizontal_rules(
|
|
3872
|
+
separators,
|
|
3873
|
+
column_widths_list,
|
|
3874
|
+
column_titles,
|
|
3875
|
+
len(body_rows),
|
|
3876
|
+
)
|
|
3877
|
+
elif self.__border_color:
|
|
3878
|
+
separators = HorizontalComposition(*[
|
|
3879
|
+
colorize(line, self.__border_color) if line is not None else None
|
|
3880
|
+
for line in separators
|
|
3881
|
+
])
|
|
3882
|
+
|
|
3861
3883
|
# Table string is formed
|
|
3862
3884
|
no_header_superior = separators.superior_header_line_no_header
|
|
3863
3885
|
body_line = ''.join(['\n', separators.table_body_line]) if (
|
|
@@ -3865,8 +3887,6 @@ class Table(object):
|
|
|
3865
3887
|
) else NONE_VALUE_REPLACEMENT
|
|
3866
3888
|
|
|
3867
3889
|
end_line = separators.table_end_line
|
|
3868
|
-
header_row = data_rows.header_row
|
|
3869
|
-
body_rows = data_rows.body_rows
|
|
3870
3890
|
|
|
3871
3891
|
if self.__show_headers:
|
|
3872
3892
|
superior_row = '\n'.join([
|
|
@@ -3885,7 +3905,8 @@ class Table(object):
|
|
|
3885
3905
|
# joined in as an empty string, which is what used to put a blank
|
|
3886
3906
|
# line through the middle of a table with no rows.
|
|
3887
3907
|
body, body_line_map = self.__join_body_rows(
|
|
3888
|
-
body_rows, body_line, separators.table_body_line
|
|
3908
|
+
body_rows, body_line, separators.table_body_line,
|
|
3909
|
+
gap_separator=gap_separator,
|
|
3889
3910
|
)
|
|
3890
3911
|
title_line = self.__title_line(column_widths_list, table_with_i)
|
|
3891
3912
|
|
|
@@ -3912,7 +3933,8 @@ class Table(object):
|
|
|
3912
3933
|
|
|
3913
3934
|
return self.__apply_table_align(table_string)
|
|
3914
3935
|
|
|
3915
|
-
def __join_body_rows(self, body_rows, body_line, separator_line
|
|
3936
|
+
def __join_body_rows(self, body_rows, body_line, separator_line,
|
|
3937
|
+
gap_separator=None):
|
|
3916
3938
|
"""
|
|
3917
3939
|
Join body rows, inserting extra divider rules where requested.
|
|
3918
3940
|
|
|
@@ -3921,6 +3943,10 @@ class Table(object):
|
|
|
3921
3943
|
``add_divider`` inserts a dashed line after the chosen row index
|
|
3922
3944
|
(display order after filter/sort).
|
|
3923
3945
|
|
|
3946
|
+
``gap_separator``, when set, is called as ``gap_separator(after_row)``
|
|
3947
|
+
to build the rule between that display row and the next (used to paint
|
|
3948
|
+
cell backgrounds onto H-rules).
|
|
3949
|
+
|
|
3924
3950
|
Returns the joined body and a map from display row index to the
|
|
3925
3951
|
physical lines that row occupies, numbered from the start of the body.
|
|
3926
3952
|
Building the map here rather than reconstructing it afterwards is what
|
|
@@ -3951,8 +3977,11 @@ class Table(object):
|
|
|
3951
3977
|
for display_i, row in enumerate(body_rows):
|
|
3952
3978
|
if display_i:
|
|
3953
3979
|
if separator_line is not None:
|
|
3954
|
-
|
|
3955
|
-
|
|
3980
|
+
if gap_separator is not None:
|
|
3981
|
+
pieces.append(gap_separator(display_i - 1))
|
|
3982
|
+
else:
|
|
3983
|
+
pieces.append(body_line[1:] if body_line.startswith('\n')
|
|
3984
|
+
else body_line)
|
|
3956
3985
|
cursor += separator_lines
|
|
3957
3986
|
elif (display_i - 1) in explicit_dividers:
|
|
3958
3987
|
pieces.append(rule)
|
|
@@ -4026,10 +4055,11 @@ class Table(object):
|
|
|
4026
4055
|
() if self.__show_empty_columns else self.__empty_column_indexes
|
|
4027
4056
|
)
|
|
4028
4057
|
|
|
4058
|
+
regions = self.__regions_with_colors()
|
|
4029
4059
|
merged = apply_merges(
|
|
4030
4060
|
table_string.split('\n'),
|
|
4031
4061
|
row_line_map,
|
|
4032
|
-
|
|
4062
|
+
regions,
|
|
4033
4063
|
column_widths_list,
|
|
4034
4064
|
self.__style_composition,
|
|
4035
4065
|
1 if table_with_i else 0,
|
|
@@ -4037,6 +4067,168 @@ class Table(object):
|
|
|
4037
4067
|
)
|
|
4038
4068
|
return '\n'.join(merged)
|
|
4039
4069
|
|
|
4070
|
+
|
|
4071
|
+
def __rule_spec_for_cell(self, row_index, column_index, column_titles):
|
|
4072
|
+
"""
|
|
4073
|
+
Colour that should back the H-rule segment under a cell.
|
|
4074
|
+
|
|
4075
|
+
Explicit merge colours win (so a wide merge paints one continuous
|
|
4076
|
+
band under its span). Otherwise the cell's resolved colour is used.
|
|
4077
|
+
"""
|
|
4078
|
+
for region in self.__regions_with_colors():
|
|
4079
|
+
if (region.first_row <= row_index <= region.last_row
|
|
4080
|
+
and region.first_column <= column_index <= region.last_column
|
|
4081
|
+
and region.color):
|
|
4082
|
+
return region.color
|
|
4083
|
+
name = (
|
|
4084
|
+
column_titles[column_index]
|
|
4085
|
+
if column_index < len(column_titles) else ''
|
|
4086
|
+
)
|
|
4087
|
+
try:
|
|
4088
|
+
raw = self.__raw_value_at(column_titles, column_index, row_index)
|
|
4089
|
+
except Exception:
|
|
4090
|
+
raw = None
|
|
4091
|
+
return self.__color_for_cell(raw, row_index, name)
|
|
4092
|
+
|
|
4093
|
+
def __rule_specs_for_row(self, row_index, column_titles, column_widths):
|
|
4094
|
+
"""One colour spec per *drawn* column for the rule under ``row_index``."""
|
|
4095
|
+
hidden = (
|
|
4096
|
+
() if self.__show_empty_columns else self.__empty_column_indexes
|
|
4097
|
+
)
|
|
4098
|
+
hidden = set(hidden)
|
|
4099
|
+
specs = []
|
|
4100
|
+
for col_i, _width in enumerate(column_widths):
|
|
4101
|
+
if col_i in hidden:
|
|
4102
|
+
continue
|
|
4103
|
+
specs.append(
|
|
4104
|
+
self.__rule_spec_for_cell(row_index, col_i, column_titles)
|
|
4105
|
+
)
|
|
4106
|
+
return specs
|
|
4107
|
+
|
|
4108
|
+
def __colour_horizontal_rules(self, separators, column_widths,
|
|
4109
|
+
column_titles, body_row_count):
|
|
4110
|
+
"""
|
|
4111
|
+
Paint cell backgrounds onto horizontal rules.
|
|
4112
|
+
|
|
4113
|
+
Body gaps use the row above. The header/body rule uses the first body
|
|
4114
|
+
row (fill meets the header separator from below). The end rule uses
|
|
4115
|
+
the last body row. Superior header keeps ``border_color`` only unless
|
|
4116
|
+
``header_color`` carries a background.
|
|
4117
|
+
Returns the updated HorizontalComposition and a gap painter callable
|
|
4118
|
+
(or None when the style has no body rule).
|
|
4119
|
+
"""
|
|
4120
|
+
from .merges import compute_layout, paint_rule_cell_backgrounds
|
|
4121
|
+
|
|
4122
|
+
hidden = (
|
|
4123
|
+
() if self.__show_empty_columns else self.__empty_column_indexes
|
|
4124
|
+
)
|
|
4125
|
+
layout = compute_layout(
|
|
4126
|
+
column_widths, self.__style_composition, hidden
|
|
4127
|
+
)
|
|
4128
|
+
border = self.__border_color
|
|
4129
|
+
|
|
4130
|
+
def paint(line, specs):
|
|
4131
|
+
if line is None:
|
|
4132
|
+
return None
|
|
4133
|
+
return paint_rule_cell_backgrounds(line, layout, specs, border)
|
|
4134
|
+
|
|
4135
|
+
first_specs = (
|
|
4136
|
+
self.__rule_specs_for_row(0, column_titles, column_widths)
|
|
4137
|
+
if body_row_count else []
|
|
4138
|
+
)
|
|
4139
|
+
last_specs = (
|
|
4140
|
+
self.__rule_specs_for_row(
|
|
4141
|
+
body_row_count - 1, column_titles, column_widths
|
|
4142
|
+
)
|
|
4143
|
+
if body_row_count else []
|
|
4144
|
+
)
|
|
4145
|
+
header_specs = []
|
|
4146
|
+
if self.__header_color and background_hex(self.__header_color):
|
|
4147
|
+
drawn = sum(
|
|
4148
|
+
1 for i, _ in enumerate(column_widths)
|
|
4149
|
+
if i not in set(hidden)
|
|
4150
|
+
)
|
|
4151
|
+
header_specs = [self.__header_color] * drawn
|
|
4152
|
+
|
|
4153
|
+
superior = separators.superior_header_line
|
|
4154
|
+
if header_specs:
|
|
4155
|
+
superior = paint(superior, header_specs)
|
|
4156
|
+
elif border and superior is not None:
|
|
4157
|
+
superior = colorize(superior, border)
|
|
4158
|
+
|
|
4159
|
+
inferior = separators.inferior_header_line
|
|
4160
|
+
if first_specs:
|
|
4161
|
+
inferior = paint(inferior, first_specs)
|
|
4162
|
+
elif border and inferior is not None:
|
|
4163
|
+
inferior = colorize(inferior, border)
|
|
4164
|
+
|
|
4165
|
+
superior_nh = separators.superior_header_line_no_header
|
|
4166
|
+
if first_specs:
|
|
4167
|
+
superior_nh = paint(superior_nh, first_specs)
|
|
4168
|
+
elif border and superior_nh is not None:
|
|
4169
|
+
superior_nh = colorize(superior_nh, border)
|
|
4170
|
+
|
|
4171
|
+
end_line = separators.table_end_line
|
|
4172
|
+
if last_specs:
|
|
4173
|
+
end_line = paint(end_line, last_specs)
|
|
4174
|
+
elif border and end_line is not None:
|
|
4175
|
+
end_line = colorize(end_line, border)
|
|
4176
|
+
|
|
4177
|
+
plain_body = separators.table_body_line
|
|
4178
|
+
body_template = plain_body
|
|
4179
|
+
if border and body_template is not None and not first_specs:
|
|
4180
|
+
body_template = colorize(body_template, border)
|
|
4181
|
+
|
|
4182
|
+
painted = HorizontalComposition(
|
|
4183
|
+
superior,
|
|
4184
|
+
inferior,
|
|
4185
|
+
superior_nh,
|
|
4186
|
+
body_template if not first_specs else plain_body,
|
|
4187
|
+
end_line,
|
|
4188
|
+
)
|
|
4189
|
+
|
|
4190
|
+
gap_separator = None
|
|
4191
|
+
if plain_body is not None and body_row_count:
|
|
4192
|
+
def gap_separator(after_row, _plain=plain_body):
|
|
4193
|
+
specs = self.__rule_specs_for_row(
|
|
4194
|
+
after_row, column_titles, column_widths
|
|
4195
|
+
)
|
|
4196
|
+
return paint_rule_cell_backgrounds(
|
|
4197
|
+
_plain, layout, specs, border
|
|
4198
|
+
)
|
|
4199
|
+
|
|
4200
|
+
return painted, gap_separator
|
|
4201
|
+
|
|
4202
|
+
def __regions_with_colors(self):
|
|
4203
|
+
"""
|
|
4204
|
+
Merged regions with an effective colour for this render.
|
|
4205
|
+
|
|
4206
|
+
Explicit ``merge_cells(..., color=...)`` wins. Otherwise, when colour
|
|
4207
|
+
output is on, the top-left cell's resolved colour is inherited so
|
|
4208
|
+
``column_colors`` / ``row_colors`` / ``cell_color`` fill the whole
|
|
4209
|
+
merge span instead of leaving a dark hole.
|
|
4210
|
+
"""
|
|
4211
|
+
colors_on = self.__colors_active and self.__has_any_color()
|
|
4212
|
+
headers = list(self.__headers)
|
|
4213
|
+
out = []
|
|
4214
|
+
for region in self.__merged_regions:
|
|
4215
|
+
if region.color is not None or not colors_on:
|
|
4216
|
+
out.append(region)
|
|
4217
|
+
continue
|
|
4218
|
+
col_name = (
|
|
4219
|
+
headers[region.first_column]
|
|
4220
|
+
if region.first_column < len(headers) else ''
|
|
4221
|
+
)
|
|
4222
|
+
try:
|
|
4223
|
+
raw = self.__raw_value_at(
|
|
4224
|
+
headers, region.first_column, region.first_row
|
|
4225
|
+
)
|
|
4226
|
+
except Exception:
|
|
4227
|
+
raw = None
|
|
4228
|
+
spec = self.__color_for_cell(raw, region.first_row, col_name)
|
|
4229
|
+
out.append(region if not spec else region._replace(color=spec))
|
|
4230
|
+
return out
|
|
4231
|
+
|
|
4040
4232
|
def __get_processed_columns_data(self, header=False, columns_with_i=False):
|
|
4041
4233
|
if columns_with_i:
|
|
4042
4234
|
columns_with_headers = self.__processed_columns_with_i.values()
|
|
@@ -25,6 +25,7 @@ import json
|
|
|
25
25
|
from typing import Any, List, Optional, Sequence
|
|
26
26
|
|
|
27
27
|
from .fast import strip_ansi
|
|
28
|
+
from .colors import background_hex, foreground_hex, spec_to_css
|
|
28
29
|
from .options import FLT_FILTER, INT_FILTER
|
|
29
30
|
|
|
30
31
|
|
|
@@ -50,6 +51,85 @@ def _table_data(table, include_index: bool):
|
|
|
50
51
|
return headers, [[cell for cell in row] for row in rows]
|
|
51
52
|
|
|
52
53
|
|
|
54
|
+
def _effective_region_color(table, region):
|
|
55
|
+
"""Explicit merge colour, else inherit from row/column colour maps."""
|
|
56
|
+
if getattr(region, 'color', None):
|
|
57
|
+
return region.color
|
|
58
|
+
try:
|
|
59
|
+
row_colors = getattr(table, 'row_colors', None) or {}
|
|
60
|
+
if region.first_row in row_colors:
|
|
61
|
+
return row_colors[region.first_row]
|
|
62
|
+
headers = list(getattr(table, 'headers', []) or [])
|
|
63
|
+
if region.first_column < len(headers):
|
|
64
|
+
col_colors = getattr(table, 'column_colors', None) or {}
|
|
65
|
+
return col_colors.get(headers[region.first_column])
|
|
66
|
+
except Exception:
|
|
67
|
+
return None
|
|
68
|
+
return None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _align_style(align: str) -> str:
|
|
72
|
+
return {'l': 'left', 'r': 'right', 'c': 'center'}.get(align, 'center')
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _html_body_with_merges(table, include_index: bool = False) -> str:
|
|
76
|
+
"""
|
|
77
|
+
Build ``<tr>…</tr>`` rows that honour ``merged_regions`` via rowspan/colspan.
|
|
78
|
+
|
|
79
|
+
Covered cells are omitted so the browser paints one merged cell. Merge
|
|
80
|
+
coordinates address data columns; an optional index column shifts them by 1.
|
|
81
|
+
"""
|
|
82
|
+
headers, rows = _table_data(table, include_index)
|
|
83
|
+
col_offset = 1 if include_index else 0
|
|
84
|
+
regions = list(getattr(table, 'merged_regions', []) or [])
|
|
85
|
+
|
|
86
|
+
# Map every covered (row, col-in-output) cell -> owning region (or None for top-left).
|
|
87
|
+
covered = {}
|
|
88
|
+
origin = {}
|
|
89
|
+
for region in regions:
|
|
90
|
+
r0, r1 = region.first_row, region.last_row
|
|
91
|
+
c0, c1 = region.first_column + col_offset, region.last_column + col_offset
|
|
92
|
+
origin[(r0, c0)] = region
|
|
93
|
+
for r in range(r0, r1 + 1):
|
|
94
|
+
for c in range(c0, c1 + 1):
|
|
95
|
+
if (r, c) != (r0, c0):
|
|
96
|
+
covered[(r, c)] = region
|
|
97
|
+
|
|
98
|
+
parts = []
|
|
99
|
+
n_cols = len(headers)
|
|
100
|
+
for r_i, row in enumerate(rows):
|
|
101
|
+
parts.append('<tr>')
|
|
102
|
+
for c_i in range(n_cols):
|
|
103
|
+
if (r_i, c_i) in covered:
|
|
104
|
+
continue
|
|
105
|
+
region = origin.get((r_i, c_i))
|
|
106
|
+
if region is not None:
|
|
107
|
+
rowspan = region.last_row - region.first_row + 1
|
|
108
|
+
colspan = region.last_column - region.first_column + 1
|
|
109
|
+
text = _plain(region.value if region.value is not None else row[c_i])
|
|
110
|
+
attrs = []
|
|
111
|
+
if rowspan > 1:
|
|
112
|
+
attrs.append(f'rowspan="{rowspan}"')
|
|
113
|
+
if colspan > 1:
|
|
114
|
+
attrs.append(f'colspan="{colspan}"')
|
|
115
|
+
style_bits = [
|
|
116
|
+
f'text-align:{_align_style(region.align)}',
|
|
117
|
+
'vertical-align:middle',
|
|
118
|
+
]
|
|
119
|
+
color_css = spec_to_css(_effective_region_color(table, region))
|
|
120
|
+
if color_css:
|
|
121
|
+
style_bits.append(color_css)
|
|
122
|
+
attrs.append(f'style="{";".join(style_bits)}"')
|
|
123
|
+
attr_s = (' ' + ' '.join(attrs)) if attrs else ''
|
|
124
|
+
parts.append(f'<td{attr_s}>{_html.escape(text)}</td>')
|
|
125
|
+
else:
|
|
126
|
+
cell = row[c_i] if c_i < len(row) else ''
|
|
127
|
+
parts.append(f'<td>{_html.escape(_plain(cell))}</td>')
|
|
128
|
+
parts.append('</tr>')
|
|
129
|
+
return ''.join(parts)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
53
133
|
def to_records(table, include_index: bool = False) -> List[dict]:
|
|
54
134
|
"""The table as a list of dicts, one per row."""
|
|
55
135
|
headers, rows = _table_data(table, include_index)
|
|
@@ -61,9 +141,10 @@ def to_simple_html(table, include_index: bool = False) -> str:
|
|
|
61
141
|
A bare HTML ``<table>`` for notebook ``_repr_html_`` hooks.
|
|
62
142
|
|
|
63
143
|
No document chrome, scripts or pagination -- just thead/tbody so Jupyter
|
|
64
|
-
and friends can embed the table in a cell output.
|
|
144
|
+
and friends can embed the table in a cell output. Merged regions become
|
|
145
|
+
``rowspan`` / ``colspan`` attributes.
|
|
65
146
|
"""
|
|
66
|
-
headers,
|
|
147
|
+
headers, _rows = _table_data(table, include_index)
|
|
67
148
|
parts = ['<table>']
|
|
68
149
|
title = getattr(table, 'title', None)
|
|
69
150
|
if title:
|
|
@@ -78,13 +159,7 @@ def to_simple_html(table, include_index: bool = False) -> str:
|
|
|
78
159
|
)
|
|
79
160
|
parts.append('</tr></thead>')
|
|
80
161
|
parts.append('<tbody>')
|
|
81
|
-
|
|
82
|
-
parts.append('<tr>')
|
|
83
|
-
for cell in row:
|
|
84
|
-
parts.append(
|
|
85
|
-
'<td>{0}</td>'.format(_html.escape(_plain(cell)))
|
|
86
|
-
)
|
|
87
|
-
parts.append('</tr>')
|
|
162
|
+
parts.append(_html_body_with_merges(table, include_index))
|
|
88
163
|
parts.append('</tbody></table>')
|
|
89
164
|
return ''.join(parts)
|
|
90
165
|
|
|
@@ -388,6 +463,47 @@ _HTML_SCRIPT = """
|
|
|
388
463
|
"""
|
|
389
464
|
|
|
390
465
|
|
|
466
|
+
|
|
467
|
+
def _to_html_merged(table, target, title: str, include_index: bool,
|
|
468
|
+
encoding: str) -> Optional[str]:
|
|
469
|
+
"""Static self-contained HTML page that preserves merged cells."""
|
|
470
|
+
headers, _rows = _table_data(table, include_index)
|
|
471
|
+
head_cells = ''.join(
|
|
472
|
+
f'<th>{_html.escape(_plain(h))}</th>' for h in headers
|
|
473
|
+
)
|
|
474
|
+
body = _html_body_with_merges(table, include_index)
|
|
475
|
+
markup = f"""<!DOCTYPE html>
|
|
476
|
+
<html lang="en">
|
|
477
|
+
<head>
|
|
478
|
+
<meta charset="utf-8">
|
|
479
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
480
|
+
<title>{_html.escape(title)}</title>
|
|
481
|
+
<style>{_HTML_STYLE}</style>
|
|
482
|
+
</head>
|
|
483
|
+
<body>
|
|
484
|
+
<div class="wrap">
|
|
485
|
+
<h1>{_html.escape(title)}</h1>
|
|
486
|
+
<p class="count">Merged cells preserved (static view).</p>
|
|
487
|
+
<div class="scroll">
|
|
488
|
+
<table>
|
|
489
|
+
<thead><tr>{head_cells}</tr></thead>
|
|
490
|
+
<tbody>{body}</tbody>
|
|
491
|
+
</table>
|
|
492
|
+
</div>
|
|
493
|
+
</div>
|
|
494
|
+
</body>
|
|
495
|
+
</html>
|
|
496
|
+
"""
|
|
497
|
+
if target is None:
|
|
498
|
+
return markup
|
|
499
|
+
if hasattr(target, 'write'):
|
|
500
|
+
target.write(markup)
|
|
501
|
+
return None
|
|
502
|
+
with open(target, 'w', encoding=encoding) as handle:
|
|
503
|
+
handle.write(markup)
|
|
504
|
+
return None
|
|
505
|
+
|
|
506
|
+
|
|
391
507
|
def to_html(table, target=None, title: str = 'Table', paginate: int = 25,
|
|
392
508
|
include_index: bool = False, searchable: bool = True,
|
|
393
509
|
encoding: str = 'utf-8') -> Optional[str]:
|
|
@@ -404,7 +520,14 @@ def to_html(table, target=None, title: str = 'Table', paginate: int = 25,
|
|
|
404
520
|
Values are passed to the page as JSON data and written into the DOM as
|
|
405
521
|
text, never as markup, so a cell containing HTML is displayed rather than
|
|
406
522
|
interpreted.
|
|
523
|
+
|
|
524
|
+
When the table has merged regions, a static ``rowspan``/``colspan`` table
|
|
525
|
+
is written instead of the interactive grid (sort/filter would break merges).
|
|
407
526
|
"""
|
|
527
|
+
regions = list(getattr(table, 'merged_regions', []) or [])
|
|
528
|
+
if regions:
|
|
529
|
+
return _to_html_merged(table, target, title, include_index, encoding)
|
|
530
|
+
|
|
408
531
|
headers, rows = _table_data(table, include_index)
|
|
409
532
|
header_texts = [_plain(header) for header in headers]
|
|
410
533
|
row_texts = [[_plain(cell) for cell in row] for row in rows]
|
|
@@ -578,5 +701,17 @@ def to_excel(table, path, sheet_name: str = 'Sheet1',
|
|
|
578
701
|
start_row=start_row, start_column=start_col,
|
|
579
702
|
end_row=end_row, end_column=end_col,
|
|
580
703
|
)
|
|
704
|
+
spec = _effective_region_color(table, region)
|
|
705
|
+
if spec:
|
|
706
|
+
from openpyxl.styles import Font as _Font
|
|
707
|
+
bg = background_hex(spec)
|
|
708
|
+
fg = foreground_hex(spec)
|
|
709
|
+
if bg:
|
|
710
|
+
fill = PatternFill('solid', fgColor=bg)
|
|
711
|
+
for r in range(start_row, end_row + 1):
|
|
712
|
+
for c in range(start_col, end_col + 1):
|
|
713
|
+
worksheet.cell(row=r, column=c).fill = fill
|
|
714
|
+
if fg:
|
|
715
|
+
top_left.font = _Font(color=fg, bold='bold' in str(spec).lower())
|
|
581
716
|
|
|
582
717
|
workbook.save(path)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flexablelib
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.11
|
|
4
4
|
Summary: Flexible console and HTML tables for Python
|
|
5
5
|
Home-page: https://github.com/Opus-Perpetuus/flexablelib
|
|
6
6
|
Download-URL:
|
|
@@ -355,7 +355,7 @@ flexablelib/ # GitHub repository
|
|
|
355
355
|
├── docs/ issue history and design notes
|
|
356
356
|
├── logos/ brand assets — see logos/README.md
|
|
357
357
|
├── ARCHITECTURE.md how it all fits together
|
|
358
|
-
├── style_examples.md all
|
|
358
|
+
├── style_examples.md all 44 styles rendered
|
|
359
359
|
├── package.json canonical version number; drives the release
|
|
360
360
|
└── setup.py reads the version from package.json
|
|
361
361
|
```
|
|
@@ -4,6 +4,8 @@ Public API coverage for the high-demand features.
|
|
|
4
4
|
Each test drives the shipped Table entry point and asserts concrete output.
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
7
9
|
from helpers import table_width
|
|
8
10
|
from flexablelib import Table
|
|
9
11
|
|
|
@@ -256,3 +258,84 @@ def test_missing_val_alias_matches_missing_value():
|
|
|
256
258
|
table.missing_value = '?'
|
|
257
259
|
assert table.missing_val == '?'
|
|
258
260
|
assert '?' in str(table)
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def test_html_export_preserves_merged_cells():
|
|
264
|
+
table = Table()
|
|
265
|
+
table.add_column('A', ['a0', 'a1', 'a2'])
|
|
266
|
+
table.add_column('B', ['b0', 'b1', 'b2'])
|
|
267
|
+
table.merge_cells(0, 0, last_row=1, value='VERT')
|
|
268
|
+
table.merge_cells(2, 0, last_column=1, value='HORZ')
|
|
269
|
+
html = table.to_html(title='merged')
|
|
270
|
+
assert 'rowspan="2"' in html
|
|
271
|
+
assert 'colspan="2"' in html
|
|
272
|
+
assert 'VERT' in html and 'HORZ' in html
|
|
273
|
+
bare = table._repr_html_()
|
|
274
|
+
assert 'rowspan="2"' in bare and 'colspan="2"' in bare
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def test_merged_region_color_fills_ansi():
|
|
278
|
+
table = Table(style_name='flush')
|
|
279
|
+
table.use_colors = True
|
|
280
|
+
table.add_column('A', ['alpha-0', 'alpha-1', 'alpha-2'])
|
|
281
|
+
table.add_column('B', ['bravo-0', 'bravo-1', 'bravo-2'])
|
|
282
|
+
table.merge_cells(0, 0, last_row=1, value='MERGE', color='black on green')
|
|
283
|
+
rendered = str(table)
|
|
284
|
+
assert '\x1b[' in rendered
|
|
285
|
+
assert '42m' in rendered
|
|
286
|
+
assert 'MERGE' in rendered
|
|
287
|
+
# Background must cover more than the bare word (padding spaces painted).
|
|
288
|
+
assert rendered.count('42m') >= 1
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def test_merged_region_inherits_column_color():
|
|
292
|
+
table = Table(style_name='flush')
|
|
293
|
+
table.use_colors = True
|
|
294
|
+
# Wide unmerged content so the merge span can hold the label.
|
|
295
|
+
table.add_column('A', ['alpha-0', 'alpha-1'])
|
|
296
|
+
table.add_column('B', ['b0', 'b1'])
|
|
297
|
+
table.column_colors = {'A': 'black on yellow'}
|
|
298
|
+
table.merge_cells(0, 0, last_row=1, value='INHERIT')
|
|
299
|
+
rendered = str(table)
|
|
300
|
+
assert '43m' in rendered # yellow bg
|
|
301
|
+
assert 'INHERIT' in rendered
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def test_flush_style_is_catalogued():
|
|
305
|
+
assert 'flush' in Table().possible_styles
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def test_html_and_excel_carry_merge_fill(tmp_path):
|
|
309
|
+
table = Table(style_name='flush')
|
|
310
|
+
table.use_colors = True
|
|
311
|
+
table.add_column('A', ['a0', 'a1'])
|
|
312
|
+
table.add_column('B', ['b0', 'b1'])
|
|
313
|
+
table.merge_cells(0, 0, last_column=1, value='BOX', color='black on cyan')
|
|
314
|
+
html = table.to_html(title='m')
|
|
315
|
+
assert 'background-color' in html
|
|
316
|
+
openpyxl = pytest.importorskip('openpyxl')
|
|
317
|
+
path = tmp_path / 'm.xlsx'
|
|
318
|
+
table.to_excel(path)
|
|
319
|
+
ws = openpyxl.load_workbook(path).active
|
|
320
|
+
assert ws['A2'].fill.fgColor.rgb.endswith('0891B2') or '0891B2' in str(ws['A2'].fill.fgColor.rgb)
|
|
321
|
+
|
|
322
|
+
def test_flush_horizontal_rules_carry_cell_bg():
|
|
323
|
+
"""H-rule dash runs inherit the cell-above background (no dark band)."""
|
|
324
|
+
import re
|
|
325
|
+
table = Table(style_name='flush')
|
|
326
|
+
table.use_colors = True
|
|
327
|
+
table.add_column('A', ['a0', 'a1'])
|
|
328
|
+
table.add_column('B', ['b0', 'b1'])
|
|
329
|
+
table.column_colors = {'A': 'black on cyan', 'B': 'black on yellow'}
|
|
330
|
+
rendered = str(table)
|
|
331
|
+
found_cyan_on_rule = False
|
|
332
|
+
found_yellow_on_rule = False
|
|
333
|
+
for line in rendered.splitlines():
|
|
334
|
+
plain = re.sub('\x1b' r'\[[0-9;]*m', '', line)
|
|
335
|
+
if '─' not in plain and '═' not in plain:
|
|
336
|
+
continue
|
|
337
|
+
if '46m' in line:
|
|
338
|
+
found_cyan_on_rule = True
|
|
339
|
+
if '43m' in line:
|
|
340
|
+
found_yellow_on_rule = True
|
|
341
|
+
assert found_cyan_on_rule and found_yellow_on_rule
|
|
@@ -36,7 +36,7 @@ def build_table(style_name):
|
|
|
36
36
|
def test_the_catalogue_holds_43_styles():
|
|
37
37
|
# A canary: adding a style is meant to be purely additive, and the README,
|
|
38
38
|
# ARCHITECTURE.md and style_examples.md all quote this number.
|
|
39
|
-
assert len(ALL_STYLES) ==
|
|
39
|
+
assert len(ALL_STYLES) == 44
|
|
40
40
|
|
|
41
41
|
|
|
42
42
|
def test_style_names_are_unique():
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|