flexablelib 2.1.1__tar.gz → 2.2.0__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.
Files changed (64) hide show
  1. {flexablelib-2.1.1 → flexablelib-2.2.0}/CHANGELOG.md +32 -0
  2. {flexablelib-2.1.1/flexablelib.egg-info → flexablelib-2.2.0}/PKG-INFO +23 -3
  3. {flexablelib-2.1.1 → flexablelib-2.2.0}/README.md +22 -2
  4. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/table.py +131 -3
  5. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/writers.py +209 -74
  6. {flexablelib-2.1.1 → flexablelib-2.2.0/flexablelib.egg-info}/PKG-INFO +23 -3
  7. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib.egg-info/SOURCES.txt +1 -0
  8. {flexablelib-2.1.1 → flexablelib-2.2.0}/package.json +1 -1
  9. flexablelib-2.2.0/tests/test_v220_exports.py +230 -0
  10. {flexablelib-2.1.1 → flexablelib-2.2.0}/LICENSE +0 -0
  11. {flexablelib-2.1.1 → flexablelib-2.2.0}/MANIFEST.in +0 -0
  12. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/__init__.py +0 -0
  13. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/_speedups.c +0 -0
  14. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/_width_tables.h +0 -0
  15. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/cells.py +0 -0
  16. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/colors.py +0 -0
  17. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/columns.py +0 -0
  18. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/fast.py +0 -0
  19. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/merges.py +0 -0
  20. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/options.py +0 -0
  21. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/py.typed +0 -0
  22. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/readers.py +0 -0
  23. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/style_compositions.py +0 -0
  24. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/table_strings.py +0 -0
  25. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/text_width.py +0 -0
  26. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib/utils.py +0 -0
  27. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib.egg-info/dependency_links.txt +0 -0
  28. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib.egg-info/requires.txt +0 -0
  29. {flexablelib-2.1.1 → flexablelib-2.2.0}/flexablelib.egg-info/top_level.txt +0 -0
  30. {flexablelib-2.1.1 → flexablelib-2.2.0}/pytest.ini +0 -0
  31. {flexablelib-2.1.1 → flexablelib-2.2.0}/setup.cfg +0 -0
  32. {flexablelib-2.1.1 → flexablelib-2.2.0}/setup.py +0 -0
  33. {flexablelib-2.1.1 → flexablelib-2.2.0}/style_examples.md +0 -0
  34. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/conftest.py +0 -0
  35. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/helpers.py +0 -0
  36. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_cells.py +0 -0
  37. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_columns.py +0 -0
  38. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_excel.py +0 -0
  39. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_features.py +0 -0
  40. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_issues.py +0 -0
  41. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_merge_edge_matrix.py +0 -0
  42. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_merges.py +0 -0
  43. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_readme_examples.py +0 -0
  44. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_style_line_widths.py +0 -0
  45. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_styles.py +0 -0
  46. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_text_width.py +0 -0
  47. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_two_pass_measurement.py +0 -0
  48. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_upstream_issues.py +0 -0
  49. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_v2021_fixes.py +0 -0
  50. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_v210_rows_copy.py +0 -0
  51. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_v2110_fixes.py +0 -0
  52. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_v211_fixes.py +0 -0
  53. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_v213_features.py +0 -0
  54. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_v214_features.py +0 -0
  55. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_v215_features.py +0 -0
  56. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_v216_features.py +0 -0
  57. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_v217_features.py +0 -0
  58. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_v218_features.py +0 -0
  59. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_v219_coverage.py +0 -0
  60. {flexablelib-2.1.1 → flexablelib-2.2.0}/tests/test_v219_fixes.py +0 -0
  61. {flexablelib-2.1.1 → flexablelib-2.2.0}/tools/benchmark.py +0 -0
  62. {flexablelib-2.1.1 → flexablelib-2.2.0}/tools/generate_width_tables.py +0 -0
  63. {flexablelib-2.1.1 → flexablelib-2.2.0}/tools/readme_gallery.py +0 -0
  64. {flexablelib-2.1.1 → flexablelib-2.2.0}/tools/text_to_png.py +0 -0
@@ -2,6 +2,38 @@
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.2.0] - 2026-11-04
6
+
7
+ ### Changed (breaking)
8
+ - Exports now write the table as ``compose()`` shows it. ``to_csv``,
9
+ ``to_markdown``, ``to_html``, ``to_rst``, ``to_asciidoc``, ``to_latex``,
10
+ ``to_excel``, ``to_records`` and ``_repr_html_`` follow ``sort_by``
11
+ (``sort_reverse``, callables) and ``row_filter``, leave out
12
+ ``hide_columns`` (and empty rows/columns when ``show_empty_rows`` /
13
+ ``show_empty_columns`` are off), and number the index by display position.
14
+ Before, they wrote every stored row in insertion order.
15
+ - The text exports (CSV, Markdown, HTML, RST, AsciiDoc, LaTeX) hold the
16
+ formatted strings ``compose()`` prints -- ``float_format``,
17
+ ``int_format``, ``leading_zeros``, ``custom_format``, ``strip_cells`` --
18
+ without padding or ANSI codes. Before, they wrote ``str()`` of the raw
19
+ value. Gaps stay empty strings. Reading such an export back gives the
20
+ printed text (``'1,200'`` stays a string).
21
+ - ``to_records()`` keeps the stored Python values (numbers stay numbers, gaps
22
+ ``None``) but in display order, filtered, without hidden columns.
23
+ - ``to_excel()`` keeps numbers numeric; other cells hold the displayed text.
24
+ - Merges keep their display position, as in ``compose()``; a merge split by
25
+ ``show_empty_rows=False`` is dropped and one over hidden columns shrinks.
26
+ - ``Table.rows`` is unchanged: the stored data in insertion order.
27
+
28
+ ### Added
29
+ - ``to_records(formatted=True)`` returns the displayed strings.
30
+ - ``to_excel()`` turns simple ``int_format`` / ``float_format`` /
31
+ ``custom_format`` specs and ``leading_zeros`` into Excel number formats
32
+ (``',d'`` -> ``#,##0``, ``'.2f'`` -> ``0.00``, ``'.1%'`` -> ``0.0%``,
33
+ ``'$ {:,.2f}'`` -> ``"$ "#,##0.00``).
34
+ - ``to_html()`` sorts numeric columns on the stored number, so formatted
35
+ values such as ``1,200`` sort correctly in the browser.
36
+
5
37
  ## [2.1.1] - 2026-11-04
6
38
 
7
39
  ### Fixed
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flexablelib
3
- Version: 2.1.1
3
+ Version: 2.2.0
4
4
  Summary: Fast, dependency-free terminal tables for Python: 56 border styles, ANSI colors, cell merges, decimal alignment, width fitting, and CSV/Markdown/HTML/Excel/LaTeX I/O.
5
5
  Home-page: https://github.com/Opus-Perpetuus/flexablelib
6
6
  Author: Benjamin Ramirez
@@ -425,6 +425,25 @@ table.to_html('report.html', paginate=25)
425
425
  table.to_excel('report.xlsx')
426
426
  ```
427
427
 
428
+ Since 2.2.0 every export writes the table as `print(table)` shows it: rows in
429
+ `sort_by` order (with `sort_reverse` and callables), `row_filter` applied,
430
+ `hide_columns` left out, and the text formats (`to_csv`, `to_markdown`,
431
+ `to_html`, `to_rst`, `to_asciidoc`, `to_latex`) hold the formatted strings —
432
+ `float_format`, `int_format`, `leading_zeros`, `custom_format`, `strip_cells` —
433
+ without padding or colour. Empty cells are empty strings.
434
+
435
+ ```python
436
+ table.sort_by, table.sort_reverse = 'Uptime', True
437
+ table.row_filter = lambda row: row[1] < 99.9
438
+ table.float_format = '.1f'
439
+ table.to_markdown() # same rows, order and numbers as print(table)
440
+ ```
441
+
442
+ `to_records()` keeps the stored Python values (numbers stay numbers, gaps are
443
+ `None`) in display order; `to_records(formatted=True)` gives the printed
444
+ strings. `to_excel()` writes numbers as numbers and turns simple format specs
445
+ (`',d'`, `'.2f'`, `'.1%'`, `'{:,.2f}'`…) into the cell's Excel number format.
446
+
428
447
  `to_html` writes one self-contained file — the stylesheet and the sorting,
429
448
  filtering and pagination script are inline — so it works offline and from a
430
449
  `file://` URL.
@@ -439,8 +458,9 @@ table.parse_str_numbers = True
439
458
 
440
459
  Values with leading zeros are left alone, so `'007'` stays a string.
441
460
 
442
- `table.rows` is a copy of the data, with `None` in empty cells (the same values
443
- as `to_records()`), so editing it does not change the table. Use `add_row` and
461
+ `table.rows` is a copy of the stored data, in insertion order and with `None`
462
+ in empty cells (sorting, filtering and formats do not touch it), so editing it
463
+ does not change the table. Use `add_row` and
444
464
  `set_cell` instead:
445
465
 
446
466
  ```python
@@ -360,6 +360,25 @@ table.to_html('report.html', paginate=25)
360
360
  table.to_excel('report.xlsx')
361
361
  ```
362
362
 
363
+ Since 2.2.0 every export writes the table as `print(table)` shows it: rows in
364
+ `sort_by` order (with `sort_reverse` and callables), `row_filter` applied,
365
+ `hide_columns` left out, and the text formats (`to_csv`, `to_markdown`,
366
+ `to_html`, `to_rst`, `to_asciidoc`, `to_latex`) hold the formatted strings —
367
+ `float_format`, `int_format`, `leading_zeros`, `custom_format`, `strip_cells` —
368
+ without padding or colour. Empty cells are empty strings.
369
+
370
+ ```python
371
+ table.sort_by, table.sort_reverse = 'Uptime', True
372
+ table.row_filter = lambda row: row[1] < 99.9
373
+ table.float_format = '.1f'
374
+ table.to_markdown() # same rows, order and numbers as print(table)
375
+ ```
376
+
377
+ `to_records()` keeps the stored Python values (numbers stay numbers, gaps are
378
+ `None`) in display order; `to_records(formatted=True)` gives the printed
379
+ strings. `to_excel()` writes numbers as numbers and turns simple format specs
380
+ (`',d'`, `'.2f'`, `'.1%'`, `'{:,.2f}'`…) into the cell's Excel number format.
381
+
363
382
  `to_html` writes one self-contained file — the stylesheet and the sorting,
364
383
  filtering and pagination script are inline — so it works offline and from a
365
384
  `file://` URL.
@@ -374,8 +393,9 @@ table.parse_str_numbers = True
374
393
 
375
394
  Values with leading zeros are left alone, so `'007'` stays a string.
376
395
 
377
- `table.rows` is a copy of the data, with `None` in empty cells (the same values
378
- as `to_records()`), so editing it does not change the table. Use `add_row` and
396
+ `table.rows` is a copy of the stored data, in insertion order and with `None`
397
+ in empty cells (sorting, filtering and formats do not touch it), so editing it
398
+ does not change the table. Use `add_row` and
379
399
  `set_cell` instead:
380
400
 
381
401
  ```python
@@ -6150,6 +6150,127 @@ class Table(object):
6150
6150
  """Alias of :meth:`transpose` (tabled naming)."""
6151
6151
  return self.transpose(**options)
6152
6152
 
6153
+ def _export_view(self, include_index=False):
6154
+ """
6155
+ What the writers export: the rows ``compose()`` shows, as data.
6156
+
6157
+ Rows come in display order -- ``row_filter`` and ``sort_by`` (with
6158
+ ``sort_reverse`` and callables) applied, empty rows left out when
6159
+ ``show_empty_rows`` is off. Columns in ``hide_columns`` are left out,
6160
+ and so are empty columns when ``show_empty_columns`` is off.
6161
+
6162
+ Returns a namespace with:
6163
+
6164
+ - ``headers``: header names (``index_title`` first with the index).
6165
+ - ``raw``: cell values as stored, ``None`` in the gaps.
6166
+ - ``text``: the strings ``compose()`` prints for each cell --
6167
+ ``int_format``, ``float_format``, ``leading_zeros``,
6168
+ ``custom_format`` and ``strip_cells`` applied, colour stripped, no
6169
+ padding -- ``''`` in the gaps.
6170
+ - ``merges``: ``merged_regions`` in export coordinates (data columns,
6171
+ index excluded); merges broken up by the filter, or covering only
6172
+ hidden columns, are dropped.
6173
+ - ``index``: whether the first column is the index.
6174
+
6175
+ ``Table.rows`` stays the stored copy; this is only for writers.
6176
+ """
6177
+ from types import SimpleNamespace
6178
+
6179
+ headers = list(self.__headers)
6180
+ n_rows = self.__real_row_count
6181
+ stored = [self.__stored_row(row_i, headers) for row_i in range(n_rows)]
6182
+
6183
+ def is_gap(cell):
6184
+ return cell is None or isinstance(cell, ValuePlacer)
6185
+
6186
+ # Raw values, gaps as None -- both the padding marker and the cells
6187
+ # filled with ``missing_value`` above.
6188
+ raw_rows = []
6189
+ for row_i in range(n_rows):
6190
+ row = []
6191
+ for header in headers:
6192
+ col = self.__columns.get(header, [])
6193
+ cell = col[row_i] if row_i < len(col) else None
6194
+ row.append(None if is_gap(cell) else cell)
6195
+ raw_rows.append(row)
6196
+
6197
+ order = self.__display_row_order(stored)
6198
+ hidden_rows = (
6199
+ set() if self.__show_empty_rows else set(self.__empty_row_indexes)
6200
+ )
6201
+ # Display position (what merges are keyed on) -> exported row.
6202
+ position_to_row = {}
6203
+ kept = []
6204
+ for position, row_i in enumerate(order):
6205
+ if row_i in hidden_rows:
6206
+ continue
6207
+ position_to_row[position] = len(kept)
6208
+ kept.append(row_i)
6209
+
6210
+ hidden_cols = set(self.__user_hidden_column_indexes(False))
6211
+ if not self.__show_empty_columns:
6212
+ for column_i in range(len(headers)):
6213
+ if all(
6214
+ is_empty_cell(raw_rows[row_i][column_i], self.__value_placer)
6215
+ for row_i in range(n_rows)
6216
+ ):
6217
+ hidden_cols.add(column_i)
6218
+ visible = [i for i in range(len(headers)) if i not in hidden_cols]
6219
+ column_to_export = {c: j for j, c in enumerate(visible)}
6220
+
6221
+ out_headers = [str(headers[c]) for c in visible]
6222
+ out_raw = []
6223
+ out_text = []
6224
+ for row_i in kept:
6225
+ raw_row = raw_rows[row_i]
6226
+ out_raw.append([raw_row[c] for c in visible])
6227
+ texts = []
6228
+ for c in visible:
6229
+ value = raw_row[c]
6230
+ if value is None:
6231
+ texts.append('')
6232
+ continue
6233
+ shown = self.__format_display_value(value, headers[c], c)
6234
+ texts.append('' if shown is None else strip_ansi(str(shown)))
6235
+ out_text.append(texts)
6236
+
6237
+ merges = []
6238
+ for region in self.__merged_regions:
6239
+ rows_span = range(region.first_row, region.last_row + 1)
6240
+ mapped_rows = [position_to_row.get(p) for p in rows_span]
6241
+ if any(r is None for r in mapped_rows):
6242
+ continue
6243
+ if mapped_rows[-1] - mapped_rows[0] != len(mapped_rows) - 1:
6244
+ continue
6245
+ cols = [
6246
+ column_to_export[c]
6247
+ for c in range(region.first_column, region.last_column + 1)
6248
+ if c in column_to_export
6249
+ ]
6250
+ if not cols:
6251
+ continue
6252
+ merges.append(region._replace(
6253
+ first_row=mapped_rows[0], last_row=mapped_rows[-1],
6254
+ first_column=cols[0], last_column=cols[-1],
6255
+ ))
6256
+
6257
+ if include_index:
6258
+ start, step = self.index_start, self.index_step
6259
+ labels = []
6260
+ for export_i in range(len(kept)):
6261
+ label = start + export_i * step
6262
+ labels.append(to_roman(label) if self.roman_index else label)
6263
+ out_headers = [str(self.index_title)] + out_headers
6264
+ out_raw = [[label] + row for label, row in zip(labels, out_raw)]
6265
+ out_text = [
6266
+ [str(label)] + row for label, row in zip(labels, out_text)
6267
+ ]
6268
+
6269
+ return SimpleNamespace(
6270
+ headers=out_headers, raw=out_raw, text=out_text,
6271
+ merges=merges, index=bool(include_index),
6272
+ )
6273
+
6153
6274
  def paginate(self, page_length=20, freeze_header=True, page_label=False):
6154
6275
  """
6155
6276
  Split the table into composed string pages of ``page_length`` body rows.
@@ -6271,10 +6392,17 @@ class Table(object):
6271
6392
  from .readers import from_markdown
6272
6393
  return from_markdown(cls, source, parse_numbers=parse_numbers, **options)
6273
6394
 
6274
- def to_records(self, include_index=False):
6275
- """The table as a list of dicts, one per row."""
6395
+ def to_records(self, include_index=False, formatted=False):
6396
+ """
6397
+ The table as a list of dicts, one per row, in display order.
6398
+
6399
+ ``sort_by`` and ``row_filter`` are applied and ``hide_columns`` left
6400
+ out, like every export since 2.2.0. Values stay the stored Python
6401
+ objects (numbers remain numbers); ``formatted=True`` returns the
6402
+ strings ``compose()`` shows instead. Gaps are ``None``.
6403
+ """
6276
6404
  from .writers import to_records
6277
- return to_records(self, include_index)
6405
+ return to_records(self, include_index, formatted)
6278
6406
 
6279
6407
  def to_csv(self, target=None, include_index=False, delimiter=',',
6280
6408
  encoding='utf-8', dialect=None, quoting=None, quotechar=None,
@@ -22,6 +22,7 @@ import csv as _csv
22
22
  import html as _html
23
23
  import io as _io
24
24
  import json
25
+ import re
25
26
  from typing import Any, List, Optional, Sequence
26
27
 
27
28
  from .fast import strip_ansi
@@ -58,35 +59,23 @@ def _plain(value: Any) -> str:
58
59
  return strip_ansi(str(value)) if value is not None else ''
59
60
 
60
61
 
61
- def _table_data(table, include_index: bool):
62
+ def _view(table, include_index: bool):
63
+ """
64
+ The rows, columns and cell texts ``compose()`` would show.
65
+
66
+ Since 2.2.0 every writer exports the table as it is displayed:
67
+ ``row_filter`` and ``sort_by`` (``sort_reverse``, callables) applied,
68
+ ``hide_columns`` left out, and the configured formats (``int_format``,
69
+ ``float_format``, ``leading_zeros``, ``custom_format``, ``strip_cells``)
70
+ applied to the text. See :meth:`Table._export_view`.
62
71
  """
63
- (headers, rows) honouring the index setting.
64
-
65
- Gaps hold the table's ``missing`` sentinel -- short rows padded by the
66
- table, NaN and NULL from the readers, ``table.missing`` placed by hand.
67
- It is an internal marker with no printable form, and every export used
68
- to write its ``repr`` (``<flexablelib.utils.ValuePlacer object at ...>``)
69
- into the CSV field, the Markdown cell or the record. It leaves here as
70
- ``None``, which every writer already turns into an empty cell.
71
- """
72
- headers = [str(header) for header in table.headers]
73
- # ``table.rows`` is already a copy with ``None`` in the gaps.
74
- rows = table.rows
75
- if include_index:
76
- # The stored index column holds a counter object that only the
77
- # renderer knows how to read, so exports wrote its ``repr`` in every
78
- # row -- and left the column out altogether when ``show_index`` was
79
- # off, although the Excel writer already shifted merges for it.
80
- # Number the rows here the way the rendered index does.
81
- start, step = table.index_start, table.index_step
82
- roman = table.roman_index
83
- labels = []
84
- for row_i in range(len(rows)):
85
- label = start + row_i * step
86
- labels.append(to_roman(label) if roman else label)
87
- headers = [str(table.index_title)] + headers
88
- rows = [[label] + row for label, row in zip(labels, rows)]
89
- return headers, rows
72
+ return table._export_view(include_index)
73
+
74
+
75
+ def _table_data(table, include_index: bool):
76
+ """(headers, raw rows) in display order, ``None`` in the gaps."""
77
+ view = _view(table, include_index)
78
+ return view.headers, view.raw
90
79
 
91
80
 
92
81
  def _effective_region_color(table, region):
@@ -110,16 +99,20 @@ def _align_style(align: str) -> str:
110
99
  return {'l': 'left', 'r': 'right', 'c': 'center'}.get(align, 'center')
111
100
 
112
101
 
113
- def _html_body_with_merges(table, include_index: bool = False) -> str:
102
+ def _html_body_with_merges(table, include_index: bool = False,
103
+ view=None) -> str:
114
104
  """
115
105
  Build ``<tr>…</tr>`` rows that honour ``merged_regions`` via rowspan/colspan.
116
106
 
117
107
  Covered cells are omitted so the browser paints one merged cell. Merge
118
108
  coordinates address data columns; an optional index column shifts them by 1.
109
+ Cells hold the formatted text ``compose()`` shows, in display order.
119
110
  """
120
- headers, rows = _table_data(table, include_index)
111
+ if view is None:
112
+ view = _view(table, include_index)
113
+ headers, rows = view.headers, view.text
121
114
  col_offset = 1 if include_index else 0
122
- regions = list(getattr(table, 'merged_regions', []) or [])
115
+ regions = view.merges
123
116
 
124
117
  # Map every covered (row, col-in-output) cell -> owning region (or None for top-left).
125
118
  covered = {}
@@ -168,10 +161,26 @@ def _html_body_with_merges(table, include_index: bool = False) -> str:
168
161
 
169
162
 
170
163
 
171
- def to_records(table, include_index: bool = False) -> List[dict]:
172
- """The table as a list of dicts, one per row."""
173
- headers, rows = _table_data(table, include_index)
174
- return [dict(zip(headers, row)) for row in rows]
164
+ def to_records(table, include_index: bool = False,
165
+ formatted: bool = False) -> List[dict]:
166
+ """
167
+ The table as a list of dicts, one per row.
168
+
169
+ Rows come in the order ``compose()`` shows them (``sort_by`` and
170
+ ``row_filter`` applied) and ``hide_columns`` are left out. Values stay
171
+ the stored Python objects, so numbers remain numbers; ``formatted=True``
172
+ gives the displayed strings instead. Gaps are ``None`` either way.
173
+ """
174
+ view = _view(table, include_index)
175
+ if not formatted:
176
+ return [dict(zip(view.headers, row)) for row in view.raw]
177
+ return [
178
+ dict(zip(view.headers, [
179
+ None if raw is None else text
180
+ for raw, text in zip(raw_row, text_row)
181
+ ]))
182
+ for raw_row, text_row in zip(view.raw, view.text)
183
+ ]
175
184
 
176
185
 
177
186
  def to_simple_html(table, include_index: bool = False) -> str:
@@ -182,7 +191,8 @@ def to_simple_html(table, include_index: bool = False) -> str:
182
191
  and friends can embed the table in a cell output. Merged regions become
183
192
  ``rowspan`` / ``colspan`` attributes.
184
193
  """
185
- headers, _rows = _table_data(table, include_index)
194
+ view = _view(table, include_index)
195
+ headers = view.headers
186
196
  parts = ['<table>']
187
197
  title = getattr(table, 'title', None)
188
198
  if title:
@@ -197,7 +207,7 @@ def to_simple_html(table, include_index: bool = False) -> str:
197
207
  )
198
208
  parts.append('</tr></thead>')
199
209
  parts.append('<tbody>')
200
- parts.append(_html_body_with_merges(table, include_index))
210
+ parts.append(_html_body_with_merges(table, include_index, view))
201
211
  parts.append('</tbody></table>')
202
212
  return ''.join(parts)
203
213
 
@@ -217,7 +227,8 @@ def to_csv(table, target=None, include_index: bool = False,
217
227
  ``doublequote``, ``lineterminator``, ``skipinitialspace``, ``strict``,
218
228
  ``dialect``) are forwarded to ``csv.writer``.
219
229
  """
220
- headers, rows = _table_data(table, include_index)
230
+ view = _view(table, include_index)
231
+ headers, rows = view.headers, view.text
221
232
  from .readers import _csv_reader_kwargs
222
233
  writer_kwargs = _csv_reader_kwargs(
223
234
  delimiter=delimiter, dialect=dialect, quoting=quoting,
@@ -310,7 +321,8 @@ def to_markdown(table, include_index: bool = False,
310
321
  """
311
322
  from .fast import visible_width, pad_to_width
312
323
 
313
- headers, rows = _table_data(table, include_index)
324
+ view = _view(table, include_index)
325
+ headers, rows = view.headers, view.raw
314
326
  if not headers:
315
327
  return ''
316
328
 
@@ -318,7 +330,7 @@ def to_markdown(table, include_index: bool = False,
318
330
  return _plain(value).replace('|', '\\|').replace('\n', ' ')
319
331
 
320
332
  header_texts = [cell(header) for header in headers]
321
- row_texts = [[cell(value) for value in row] for row in rows]
333
+ row_texts = [[cell(value) for value in row] for row in view.text]
322
334
 
323
335
  if align:
324
336
  widths = [
@@ -419,6 +431,8 @@ td.num { text-align: right; }
419
431
  _HTML_SCRIPT = """
420
432
  (function () {
421
433
  var rows = DATA.rows, cols = DATA.headers, numeric = DATA.numeric;
434
+ // Numeric columns sort on the stored number, not the formatted text.
435
+ rows.forEach(function (row, i) { row.keys = (DATA.keys || [])[i] || []; });
422
436
  var perPage = DATA.perPage, page = 0, sortCol = -1, sortDir = 0;
423
437
  var view = rows.slice();
424
438
 
@@ -433,7 +447,9 @@ _HTML_SCRIPT = """
433
447
  function compare(a, b) {
434
448
  var x = a[sortCol], y = b[sortCol];
435
449
  if (numeric[sortCol]) {
436
- var nx = parseFloat(x), ny = parseFloat(y);
450
+ var kx = a.keys[sortCol], ky = b.keys[sortCol];
451
+ var nx = kx === null || kx === undefined ? NaN : kx;
452
+ var ny = ky === null || ky === undefined ? NaN : ky;
437
453
  // Blank and non-numeric cells sort last, whichever way the column goes.
438
454
  var bx = isNaN(nx), by = isNaN(ny);
439
455
  if (bx && by) return 0;
@@ -519,11 +535,12 @@ _HTML_SCRIPT = """
519
535
  def _to_html_merged(table, target, title: str, include_index: bool,
520
536
  encoding: str) -> Optional[str]:
521
537
  """Static self-contained HTML page that preserves merged cells."""
522
- headers, _rows = _table_data(table, include_index)
538
+ view = _view(table, include_index)
539
+ headers = view.headers
523
540
  head_cells = ''.join(
524
541
  f'<th>{_html.escape(_plain(h))}</th>' for h in headers
525
542
  )
526
- body = _html_body_with_merges(table, include_index)
543
+ body = _html_body_with_merges(table, include_index, view)
527
544
  markup = f"""<!DOCTYPE html>
528
545
  <html lang="en">
529
546
  <head>
@@ -576,32 +593,50 @@ def to_html(table, target=None, title: str = 'Table', paginate: int = 25,
576
593
  When the table has merged regions, a static ``rowspan``/``colspan`` table
577
594
  is written instead of the interactive grid (sort/filter would break merges).
578
595
  """
579
- regions = list(getattr(table, 'merged_regions', []) or [])
580
- if regions:
596
+ view = _view(table, include_index)
597
+ if view.merges:
581
598
  return _to_html_merged(table, target, title, include_index, encoding)
582
599
 
583
- headers, rows = _table_data(table, include_index)
584
- header_texts = [_plain(header) for header in headers]
585
- row_texts = [[_plain(cell) for cell in row] for row in rows]
600
+ header_texts = [_plain(header) for header in view.headers]
601
+ row_texts = [list(row) for row in view.text]
586
602
 
587
603
  # A column is treated as numeric for sorting and alignment when every
588
- # non-empty cell in it parses as a number.
604
+ # non-empty cell in it is a number -- judged on the stored value, so a
605
+ # formatted '1,234.50' or '12.5 %' still sorts as a number.
606
+ def sort_number(raw, text):
607
+ if isinstance(raw, bool):
608
+ return None
609
+ if isinstance(raw, (int, float)):
610
+ return None if raw != raw else raw
611
+ try:
612
+ return float(text)
613
+ except ValueError:
614
+ return None
615
+
589
616
  numeric_flags = []
590
617
  for index in range(len(header_texts)):
591
- values = [row[index] for row in row_texts
592
- if index < len(row) and row[index].strip()]
593
- numeric = bool(values)
594
- for value in values:
595
- try:
596
- float(value)
597
- except ValueError:
598
- numeric = False
599
- break
600
- numeric_flags.append(numeric)
618
+ cells = [
619
+ (raw_row[index], text_row[index])
620
+ for raw_row, text_row in zip(view.raw, row_texts)
621
+ if index < len(text_row) and text_row[index].strip()
622
+ ]
623
+ numeric_flags.append(
624
+ bool(cells)
625
+ and all(sort_number(raw, text) is not None for raw, text in cells)
626
+ )
627
+
628
+ keys = [
629
+ [
630
+ sort_number(raw_row[i], text_row[i]) if numeric_flags[i] else None
631
+ for i in range(len(text_row))
632
+ ]
633
+ for raw_row, text_row in zip(view.raw, row_texts)
634
+ ]
601
635
 
602
636
  payload = {
603
637
  'headers': header_texts,
604
638
  'rows': row_texts,
639
+ 'keys': keys,
605
640
  'numeric': numeric_flags,
606
641
  'perPage': max(0, int(paginate)),
607
642
  }
@@ -670,6 +705,84 @@ var DATA = {_script_safe_json(payload)};
670
705
  return None
671
706
 
672
707
 
708
+ _EXCEL_SPEC = re.compile(
709
+ r'^(?P<comma>,)?(?:\.(?P<precision>\d+))?(?P<kind>[dfFeE%n]?)$'
710
+ )
711
+
712
+
713
+ def _excel_quote(text: str) -> str:
714
+ return '"{0}"'.format(text.replace('"', '')) if text else ''
715
+
716
+
717
+ def _excel_from_spec(spec: str, is_int: bool) -> Optional[str]:
718
+ """
719
+ An Excel number format for a Python format spec, or None.
720
+
721
+ Covers the common shapes -- ``',d'``, ``'.2f'``, ``',.2f'``, ``'.1%'``,
722
+ ``'.3e'``, ``'{:,.2f}'``, ``'$ {:,.0f} MXN'`` -- so the spreadsheet shows
723
+ what the terminal shows while the cell stays a number. Anything else
724
+ (callables, padding, fill characters) leaves Excel's General format.
725
+ """
726
+ prefix = suffix = ''
727
+ if '{' in spec:
728
+ match = re.match(r'^([^{}]*)\{(?:0)?:?([^{}]*)\}([^{}]*)$', spec)
729
+ if not match:
730
+ return None
731
+ prefix, spec, suffix = match.groups()
732
+ match = _EXCEL_SPEC.match(spec)
733
+ if not match:
734
+ return None
735
+ comma = bool(match.group('comma'))
736
+ precision = match.group('precision')
737
+ kind = match.group('kind')
738
+ whole = '#,##0' if comma else '0'
739
+ if kind in ('', 'n'):
740
+ if precision is not None:
741
+ return None
742
+ body = whole if is_int else None
743
+ if body is None and not comma:
744
+ return None
745
+ body = body or '#,##0.##########'
746
+ elif kind == 'd':
747
+ if not is_int:
748
+ return None
749
+ body = whole
750
+ elif kind in ('f', 'F', '%'):
751
+ digits = int(precision) if precision is not None else 6
752
+ body = whole + ('.' + '0' * digits if digits else '')
753
+ if kind == '%':
754
+ body += '%'
755
+ else: # e / E
756
+ digits = int(precision) if precision is not None else 6
757
+ body = '0' + ('.' + '0' * digits if digits else '') + 'E+00'
758
+ return _excel_quote(prefix) + body + _excel_quote(suffix)
759
+
760
+
761
+ def _excel_number_format(table, value, header, column_i) -> Optional[str]:
762
+ """
763
+ The Excel number format matching how ``compose()`` prints ``value``.
764
+
765
+ ``int_format`` / ``float_format`` / ``leading_zeros`` first, then a
766
+ per-column string spec in ``custom_format`` when the type has none.
767
+ """
768
+ is_int = isinstance(value, int)
769
+ spec = table.int_format if is_int else table.float_format
770
+ if spec is None and is_int and getattr(table, 'leading_zeros', None):
771
+ return '0' * int(table.leading_zeros)
772
+ if spec is None:
773
+ custom = getattr(table, 'custom_format', None)
774
+ if isinstance(custom, dict):
775
+ spec = custom.get(header)
776
+ if spec is None:
777
+ spec = custom.get(column_i)
778
+ if not isinstance(spec, str):
779
+ return None
780
+ try:
781
+ return _excel_from_spec(spec, is_int)
782
+ except (TypeError, ValueError):
783
+ return None
784
+
785
+
673
786
  def to_excel(table, path, sheet_name: str = 'Sheet1',
674
787
  include_index: bool = False, autofit: bool = True,
675
788
  freeze_header: bool = True) -> None:
@@ -677,7 +790,11 @@ def to_excel(table, path, sheet_name: str = 'Sheet1',
677
790
  Write an .xlsx workbook.
678
791
 
679
792
  Numbers are written as numbers rather than text, so the spreadsheet can
680
- compute with them, and are right-aligned to match. The header row is bold
793
+ compute with them, and are right-aligned to match. Rows follow the
794
+ displayed order (``sort_by``, ``row_filter``), ``hide_columns`` are left
795
+ out, simple ``int_format`` / ``float_format`` / ``custom_format`` specs
796
+ become the cells' Excel number format, and every other cell holds the
797
+ text ``compose()`` shows. The header row is bold
681
798
  and frozen, and column widths are fitted to the content.
682
799
  """
683
800
  try:
@@ -690,7 +807,10 @@ def to_excel(table, path, sheet_name: str = 'Sheet1',
690
807
  'Install it with: pip install flexablelib[excel]'
691
808
  )
692
809
 
693
- headers, rows = _table_data(table, include_index)
810
+ view = _view(table, include_index)
811
+ headers, rows, texts = view.headers, view.raw, view.text
812
+ data_offset = 1 if include_index else 0
813
+ data_headers = list(table.headers)
694
814
 
695
815
  workbook = Workbook()
696
816
  worksheet = workbook.active
@@ -704,25 +824,37 @@ def to_excel(table, path, sheet_name: str = 'Sheet1',
704
824
  cell.fill = header_fill
705
825
  cell.alignment = Alignment(horizontal='left', vertical='center')
706
826
 
707
- for row_index, row in enumerate(rows, start=2):
827
+ for row_index, (row, text_row) in enumerate(zip(rows, texts), start=2):
708
828
  for column_index, value in enumerate(row, start=1):
709
- # Keep real numbers numeric so formulas and charts work; anything
710
- # else goes in as its plain text.
829
+ # Keep real numbers numeric so formulas and charts work, with the
830
+ # table's number format carried over when Excel can express it;
831
+ # anything else goes in as the text compose() shows.
832
+ number_format = None
711
833
  if isinstance(value, bool) or not isinstance(value, (int, float)):
712
- written = _plain(value)
834
+ written = text_row[column_index - 1]
713
835
  else:
714
836
  written = value
837
+ header_i = column_index - 1 - data_offset
838
+ if header_i >= 0:
839
+ number_format = _excel_number_format(
840
+ table, value,
841
+ data_headers[header_i] if header_i < len(data_headers)
842
+ else None,
843
+ header_i,
844
+ )
715
845
  cell = worksheet.cell(row=row_index, column=column_index,
716
846
  value=written)
717
847
  if isinstance(written, (int, float)) and not isinstance(written, bool):
718
848
  cell.alignment = Alignment(horizontal='right')
849
+ if number_format:
850
+ cell.number_format = number_format
719
851
 
720
852
  if autofit:
721
853
  for column_index, header in enumerate(headers, start=1):
722
854
  longest = len(_plain(header))
723
- for row in rows:
855
+ for row in texts:
724
856
  if column_index - 1 < len(row):
725
- longest = max(longest, len(_plain(row[column_index - 1])))
857
+ longest = max(longest, len(row[column_index - 1]))
726
858
  # A little padding, and a ceiling so one long cell cannot push a
727
859
  # column off the screen.
728
860
  worksheet.column_dimensions[get_column_letter(column_index)].width = (
@@ -735,7 +867,7 @@ def to_excel(table, path, sheet_name: str = 'Sheet1',
735
867
  # Mirror flexablelib merged regions as Excel merged cells. Table coordinates
736
868
  # ignore the optional index column and the header row lives on Excel row 1.
737
869
  col_offset = 2 if include_index else 1
738
- for region in getattr(table, 'merged_regions', []):
870
+ for region in view.merges:
739
871
  start_row = region.first_row + 2
740
872
  end_row = region.last_row + 2
741
873
  start_col = region.first_column + col_offset
@@ -778,7 +910,8 @@ def to_rst(table, include_index: bool = False) -> str:
778
910
  """
779
911
  from .fast import visible_width, pad_to_width
780
912
 
781
- headers, rows = _table_data(table, include_index)
913
+ view = _view(table, include_index)
914
+ headers = view.headers
782
915
  if not headers:
783
916
  return ''
784
917
 
@@ -786,7 +919,7 @@ def to_rst(table, include_index: bool = False) -> str:
786
919
  return _plain(value).replace('\n', ' ')
787
920
 
788
921
  header_texts = [cell(h) for h in headers]
789
- row_texts = [[cell(v) for v in row] for row in rows]
922
+ row_texts = [[cell(v) for v in row] for row in view.text]
790
923
  widths = [
791
924
  max(
792
925
  [visible_width(header_texts[i])]
@@ -820,7 +953,8 @@ def to_asciidoc(table, include_index: bool = False) -> str:
820
953
  Column widths are left to the AsciiDoc processor; cells with newlines are
821
954
  flattened to spaces so a single logical row stays one source line.
822
955
  """
823
- headers, rows = _table_data(table, include_index)
956
+ view = _view(table, include_index)
957
+ headers = view.headers
824
958
  if not headers:
825
959
  return ''
826
960
 
@@ -828,7 +962,7 @@ def to_asciidoc(table, include_index: bool = False) -> str:
828
962
  return _plain(value).replace('|', '\\|').replace('\n', ' ')
829
963
 
830
964
  lines = ['|===', '| ' + ' | '.join(cell(h) for h in headers)]
831
- for row in rows:
965
+ for row in view.text:
832
966
  lines.append('| ' + ' | '.join(cell(v) for v in row))
833
967
  lines.append('|===')
834
968
  return '\n'.join(lines)
@@ -842,7 +976,8 @@ def to_latex(table, include_index: bool = False) -> str:
842
976
  (``l`` / ``c`` / ``r``); otherwise every column is left-aligned. Special
843
977
  TeX characters in cells are escaped.
844
978
  """
845
- headers, rows = _table_data(table, include_index)
979
+ view = _view(table, include_index)
980
+ headers, rows = view.headers, view.raw
846
981
  if not headers:
847
982
  return ''
848
983
 
@@ -901,7 +1036,7 @@ def to_latex(table, include_index: bool = False) -> str:
901
1036
  ' & '.join(escape_tex(h) for h in headers) + r' \\',
902
1037
  r'\hline',
903
1038
  ]
904
- for row in rows:
1039
+ for row in view.text:
905
1040
  lines.append(' & '.join(escape_tex(v) for v in row) + r' \\')
906
1041
  lines.append(r'\hline')
907
1042
  lines.append(r'\end{tabular}')
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flexablelib
3
- Version: 2.1.1
3
+ Version: 2.2.0
4
4
  Summary: Fast, dependency-free terminal tables for Python: 56 border styles, ANSI colors, cell merges, decimal alignment, width fitting, and CSV/Markdown/HTML/Excel/LaTeX I/O.
5
5
  Home-page: https://github.com/Opus-Perpetuus/flexablelib
6
6
  Author: Benjamin Ramirez
@@ -425,6 +425,25 @@ table.to_html('report.html', paginate=25)
425
425
  table.to_excel('report.xlsx')
426
426
  ```
427
427
 
428
+ Since 2.2.0 every export writes the table as `print(table)` shows it: rows in
429
+ `sort_by` order (with `sort_reverse` and callables), `row_filter` applied,
430
+ `hide_columns` left out, and the text formats (`to_csv`, `to_markdown`,
431
+ `to_html`, `to_rst`, `to_asciidoc`, `to_latex`) hold the formatted strings —
432
+ `float_format`, `int_format`, `leading_zeros`, `custom_format`, `strip_cells` —
433
+ without padding or colour. Empty cells are empty strings.
434
+
435
+ ```python
436
+ table.sort_by, table.sort_reverse = 'Uptime', True
437
+ table.row_filter = lambda row: row[1] < 99.9
438
+ table.float_format = '.1f'
439
+ table.to_markdown() # same rows, order and numbers as print(table)
440
+ ```
441
+
442
+ `to_records()` keeps the stored Python values (numbers stay numbers, gaps are
443
+ `None`) in display order; `to_records(formatted=True)` gives the printed
444
+ strings. `to_excel()` writes numbers as numbers and turns simple format specs
445
+ (`',d'`, `'.2f'`, `'.1%'`, `'{:,.2f}'`…) into the cell's Excel number format.
446
+
428
447
  `to_html` writes one self-contained file — the stylesheet and the sorting,
429
448
  filtering and pagination script are inline — so it works offline and from a
430
449
  `file://` URL.
@@ -439,8 +458,9 @@ table.parse_str_numbers = True
439
458
 
440
459
  Values with leading zeros are left alone, so `'007'` stays a string.
441
460
 
442
- `table.rows` is a copy of the data, with `None` in empty cells (the same values
443
- as `to_records()`), so editing it does not change the table. Use `add_row` and
461
+ `table.rows` is a copy of the stored data, in insertion order and with `None`
462
+ in empty cells (sorting, filtering and formats do not touch it), so editing it
463
+ does not change the table. Use `add_row` and
444
464
  `set_cell` instead:
445
465
 
446
466
  ```python
@@ -56,6 +56,7 @@ tests/test_v217_features.py
56
56
  tests/test_v218_features.py
57
57
  tests/test_v219_coverage.py
58
58
  tests/test_v219_fixes.py
59
+ tests/test_v220_exports.py
59
60
  tools/benchmark.py
60
61
  tools/generate_width_tables.py
61
62
  tools/readme_gallery.py
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flexablelib",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "private": true,
5
5
  "description": "Flexible console and HTML tables for Python: styles, merges, colors, widths, decimal alignment.",
6
6
  "repository": {
@@ -0,0 +1,230 @@
1
+ """2.2.0: exports follow sort_by, row_filter, hide_columns and the formats."""
2
+ import csv
3
+ import io
4
+ import json
5
+ import re
6
+
7
+ import pytest
8
+
9
+ from flexablelib import Table
10
+
11
+
12
+ def _table():
13
+ t = Table(
14
+ rows=[['api', 99.982, 1200], ['auth', 98.75, 35000],
15
+ ['db', None, 7], ['cache', 97.1, 15]],
16
+ headers=['svc', 'up', 'reqs'],
17
+ )
18
+ t.sort_by = 'up'
19
+ t.sort_reverse = True
20
+ t.row_filter = lambda row: row[2] > 10
21
+ t.float_format = '.1f'
22
+ t.int_format = ',d'
23
+ return t
24
+
25
+
26
+ EXPECTED = [['api', '100.0', '1,200'], ['auth', '98.8', '35,000'],
27
+ ['cache', '97.1', '15']]
28
+
29
+
30
+ def _md_rows(text):
31
+ lines = text.splitlines()[2:]
32
+ return [[c.strip() for c in line.strip('|').split('|')] for line in lines]
33
+
34
+
35
+ def test_csv_follows_display():
36
+ rows = list(csv.reader(io.StringIO(_table().to_csv())))
37
+ assert rows[0] == ['svc', 'up', 'reqs']
38
+ assert rows[1:] == EXPECTED
39
+
40
+
41
+ def test_markdown_follows_display():
42
+ assert _md_rows(_table().to_markdown()) == EXPECTED
43
+
44
+
45
+ def test_markdown_numeric_alignment_uses_stored_numbers():
46
+ rule = _table().to_markdown().splitlines()[1]
47
+ assert rule.split('|')[3].strip().endswith(':') # '1,200' still numeric
48
+
49
+
50
+ def test_rst_asciidoc_latex_follow_display():
51
+ t = _table()
52
+ rst = t.to_rst()
53
+ assert rst.index('api') < rst.index('auth') < rst.index('cache')
54
+ assert '35,000' in rst and 'db' not in rst and '99.982' not in rst
55
+ adoc = t.to_asciidoc().splitlines()
56
+ assert adoc[2:5] == ['| api | 100.0 | 1,200', '| auth | 98.8 | 35,000',
57
+ '| cache | 97.1 | 15']
58
+ latex = t.to_latex()
59
+ assert 'api & 100.0 & 1,200' in latex and 'db' not in latex
60
+ assert r'\begin{tabular}{lrr}' in latex
61
+
62
+
63
+ def test_html_follows_display_and_sorts_on_numbers():
64
+ html = _table().to_html()
65
+ data = json.loads(re.search(r'var DATA = (.*);', html).group(1))
66
+ assert data['rows'] == EXPECTED
67
+ assert data['numeric'] == [False, True, True]
68
+ assert data['keys'][1] == [None, 98.75, 35000]
69
+
70
+
71
+ def test_simple_html_follows_display():
72
+ html = _table()._repr_html_()
73
+ assert html.index('api') < html.index('auth') < html.index('cache')
74
+ assert '35,000' in html and '>db<' not in html
75
+
76
+
77
+ def test_records_raw_in_display_order():
78
+ assert _table().to_records() == [
79
+ {'svc': 'api', 'up': 99.982, 'reqs': 1200},
80
+ {'svc': 'auth', 'up': 98.75, 'reqs': 35000},
81
+ {'svc': 'cache', 'up': 97.1, 'reqs': 15},
82
+ ]
83
+
84
+
85
+ def test_records_formatted():
86
+ t = _table()
87
+ t.row_filter = None
88
+ records = t.to_records(formatted=True)
89
+ assert [r['svc'] for r in records] == ['api', 'auth', 'cache', 'db']
90
+ assert records[0] == {'svc': 'api', 'up': '100.0', 'reqs': '1,200'}
91
+ assert records[-1]['up'] is None # gaps stay None
92
+
93
+
94
+ def test_callable_sort_and_index_numbering():
95
+ t = Table(rows=[['b', 2], ['a', 1], ['c', 3]], headers=['k', 'v'])
96
+ t.sort_by = lambda row: -row[1]
97
+ t.index_start = 1
98
+ out = list(csv.reader(io.StringIO(t.to_csv(include_index=True))))
99
+ assert [r[1] for r in out[1:]] == ['c', 'b', 'a']
100
+ assert [r[0] for r in out[1:]] == ['1', '2', '3']
101
+
102
+
103
+ def test_roman_index_after_filter():
104
+ t = Table(rows=[['a', 1], ['b', 2], ['c', 3]], headers=['k', 'v'])
105
+ t.row_filter = lambda row: row[1] != 2
106
+ t.index_start = 1
107
+ t.roman_index = True
108
+ assert [r['i'] if 'i' in r else list(r.values())[0]
109
+ for r in t.to_records(include_index=True)] == ['I', 'II']
110
+
111
+
112
+ def test_hide_columns_left_out():
113
+ t = _table()
114
+ t.hide_columns = ['reqs']
115
+ assert _md_rows(t.to_markdown()) == [r[:2] for r in EXPECTED]
116
+ assert list(t.to_records()[0]) == ['svc', 'up']
117
+
118
+
119
+ def test_custom_format_and_strip_cells():
120
+ t = Table(rows=[[' x ', 0.5]], headers=['s', 'p'])
121
+ t.strip_cells = True
122
+ t.custom_format = {'p': '{:.0%}'}
123
+ assert list(csv.reader(io.StringIO(t.to_csv())))[1] == ['x', '50%']
124
+
125
+
126
+ def test_gaps_are_empty_not_missing_value():
127
+ t = Table(rows=[['a', None], ['b', 2]], headers=['k', 'v'])
128
+ t.missing_value = '-'
129
+ assert list(csv.reader(io.StringIO(t.to_csv())))[1] == ['a', '']
130
+ assert t.to_records()[0]['v'] is None
131
+
132
+
133
+ def test_empty_rows_dropped_when_hidden():
134
+ t = Table(rows=[['a', 1], [None, None], ['b', 2]], headers=['k', 'v'])
135
+ assert len(t.to_records()) == 3
136
+ t.show_empty_rows = False
137
+ assert [r['k'] for r in t.to_records()] == ['a', 'b']
138
+
139
+
140
+ def test_rows_property_stays_raw_and_stored():
141
+ t = _table()
142
+ assert [r[0] for r in t.rows] == ['api', 'auth', 'db', 'cache']
143
+ assert t.rows[0][1] == 99.982
144
+
145
+
146
+ def test_footer_rows_stay_last_without_sort():
147
+ t = Table(rows=[['a', 2], ['b', 1], ['Total', 3]], headers=['k', 'v'])
148
+ t.footer_rows = 1
149
+ assert [r['k'] for r in t.to_records()] == ['a', 'b', 'Total']
150
+
151
+
152
+ def test_merges_follow_hidden_columns_and_filter():
153
+ t = Table(rows=[['a', 'x', 1], ['b', 'y', 2], ['c', 'z', 3]],
154
+ headers=['k', 'h', 'v'])
155
+ t.merge_cells(0, 0, 0, 1, value='AB')
156
+ t.hide_columns = ['h']
157
+ html = t._repr_html_()
158
+ assert 'AB' in html and 'colspan' not in html
159
+ t.hide_columns = None
160
+ t.row_filter = lambda row: row[0] != 'a'
161
+ # Merges sit on display positions, as in compose(): after the filter
162
+ # the first shown row ('b') carries it.
163
+ html = t._repr_html_()
164
+ assert 'colspan="2"' in html and '>AB<' in html and '>y<' not in html
165
+ assert 'AB' in t.compose()
166
+
167
+
168
+ def test_merge_keeps_display_position():
169
+ t = Table(rows=[['a', 1], ['b', 2], ['c', 3]], headers=['k', 'v'])
170
+ t.merge_cells(0, 0, 1, 0, value='top')
171
+ t.sort_by = 'v'
172
+ t.sort_reverse = True
173
+ html = t._repr_html_()
174
+ assert 'rowspan="2"' in html and 'top' in html
175
+
176
+
177
+ def test_excel_numbers_and_number_formats(tmp_path):
178
+ openpyxl = pytest.importorskip('openpyxl')
179
+ t = _table()
180
+ path = tmp_path / 'out.xlsx'
181
+ t.to_excel(str(path))
182
+ ws = openpyxl.load_workbook(str(path)).active
183
+ values = [[c.value for c in row] for row in ws.iter_rows()]
184
+ assert values == [['svc', 'up', 'reqs'], ['api', 99.982, 1200],
185
+ ['auth', 98.75, 35000], ['cache', 97.1, 15]]
186
+ assert ws['B2'].number_format == '0.0'
187
+ assert ws['C3'].number_format == '#,##0'
188
+
189
+
190
+ @pytest.mark.parametrize('spec,is_int,expected', [
191
+ (',d', True, '#,##0'),
192
+ ('.2f', False, '0.00'),
193
+ (',.2f', False, '#,##0.00'),
194
+ ('.1%', False, '0.0%'),
195
+ ('.3e', False, '0.000E+00'),
196
+ ('{:,.0f}', False, '#,##0'),
197
+ ('$ {:,.2f} MXN', False, '"$ "#,##0.00" MXN"'),
198
+ ('>10', False, None),
199
+ ('d', False, None),
200
+ ])
201
+ def test_excel_spec_translation(spec, is_int, expected):
202
+ from flexablelib.writers import _excel_from_spec
203
+ assert _excel_from_spec(spec, is_int) == expected
204
+
205
+
206
+ def test_excel_leading_zeros_and_text_cells(tmp_path):
207
+ openpyxl = pytest.importorskip('openpyxl')
208
+ t = Table(rows=[[7, True, 'x']], headers=['n', 'b', 's'])
209
+ t.leading_zeros = 3
210
+ t.custom_format = {'s': lambda v: v.upper()}
211
+ path = tmp_path / 'z.xlsx'
212
+ t.to_excel(str(path))
213
+ ws = openpyxl.load_workbook(str(path)).active
214
+ assert ws['A2'].value == 7 and ws['A2'].number_format == '000'
215
+ assert ws['B2'].value == 'True' and ws['C2'].value == 'X'
216
+
217
+
218
+ def test_round_trips_without_formats():
219
+ t = Table(rows=[['a', 1, 2.5], ['b', 2, 0.5]], headers=['k', 'n', 'f'])
220
+ assert Table.from_csv(io.StringIO(t.to_csv())).rows == t.rows
221
+ assert Table.from_markdown(t.to_markdown()).rows == t.rows
222
+ assert Table.from_html(t._repr_html_()).rows == t.rows
223
+ assert Table.from_records(t.to_records()).rows == t.rows
224
+
225
+
226
+ def test_round_trip_reads_what_was_shown():
227
+ t = _table()
228
+ back = Table.from_markdown(t.to_markdown())
229
+ assert [r[0] for r in back.rows] == ['api', 'auth', 'cache']
230
+ assert back.rows[0][1] == 100.0
File without changes
File without changes
File without changes
File without changes
File without changes