Skip to content

Commit

Permalink
fix/no cast none to str (#134)
Browse files Browse the repository at this point in the history
* fix: do not cast None to str in ArrowBackend

* fix: add test for null rep
  • Loading branch information
tconbeer authored Oct 31, 2024
1 parent a7d1717 commit 14d01ee
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
## [Unreleased]

- Adds an optional parameter to DataTable to disable rendering of string data as Rich Markup.
- Fixes a bug where None could be casted to a string and displayed as "None" ([tconbeer/harlequin#658](https://github.com/tconbeer/harlequin/issues/658))

## [0.9.0] - 2024-07-23

Expand Down
5 changes: 4 additions & 1 deletion src/textual_fastdatatable/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ def from_pydict(
except (pal.ArrowInvalid, pal.ArrowTypeError):
# one or more fields has mixed types, like int and
# string. Cast all to string for safety
new_data = {k: [str(val) for val in v] for k, v in data.items()}
new_data = {
k: [str(val) if val is not None else None for val in v]
for k, v in data.items()
}
tbl = pa.Table.from_pydict(new_data)
return cls(tbl, max_rows=max_rows)

Expand Down
159 changes: 159 additions & 0 deletions tests/snapshot_tests/__snapshots__/test_snapshots.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1967,6 +1967,165 @@

'''
# ---
# name: test_datatable_null_mixed_cols
'''
<svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
<!-- Generated with Rich https://www.textualize.io -->
<style>

@font-face {
font-family: "Fira Code";
src: local("FiraCode-Regular"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");
font-style: normal;
font-weight: 400;
}
@font-face {
font-family: "Fira Code";
src: local("FiraCode-Bold"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");
font-style: bold;
font-weight: 700;
}

.terminal-3369331553-matrix {
font-family: Fira Code, monospace;
font-size: 20px;
line-height: 24.4px;
font-variant-east-asian: full-width;
}

.terminal-3369331553-title {
font-size: 18px;
font-weight: bold;
font-family: arial;
}

.terminal-3369331553-r1 { fill: #dde6ed;font-weight: bold }
.terminal-3369331553-r2 { fill: #dde6ed }
.terminal-3369331553-r3 { fill: #c5c8c6 }
.terminal-3369331553-r4 { fill: #211505 }
.terminal-3369331553-r5 { fill: #e1e1e1 }
.terminal-3369331553-r6 { fill: #939393 }
</style>

<defs>
<clipPath id="terminal-3369331553-clip-terminal">
<rect x="0" y="0" width="975.0" height="584.5999999999999" />
</clipPath>
<clipPath id="terminal-3369331553-line-0">
<rect x="0" y="1.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-1">
<rect x="0" y="25.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-2">
<rect x="0" y="50.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-3">
<rect x="0" y="74.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-4">
<rect x="0" y="99.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-5">
<rect x="0" y="123.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-6">
<rect x="0" y="147.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-7">
<rect x="0" y="172.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-8">
<rect x="0" y="196.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-9">
<rect x="0" y="221.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-10">
<rect x="0" y="245.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-11">
<rect x="0" y="269.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-12">
<rect x="0" y="294.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-13">
<rect x="0" y="318.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-14">
<rect x="0" y="343.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-15">
<rect x="0" y="367.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-16">
<rect x="0" y="391.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-17">
<rect x="0" y="416.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-18">
<rect x="0" y="440.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-19">
<rect x="0" y="465.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-20">
<rect x="0" y="489.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-21">
<rect x="0" y="513.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3369331553-line-22">
<rect x="0" y="538.3" width="976" height="24.65"/>
</clipPath>
</defs>

<rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="992" height="633.6" rx="8"/><text class="terminal-3369331553-title" fill="#c5c8c6" text-anchor="middle" x="496" y="27">TableApp</text>
<g transform="translate(26,22)">
<circle cx="0" cy="0" r="7" fill="#ff5f57"/>
<circle cx="22" cy="0" r="7" fill="#febc2e"/>
<circle cx="44" cy="0" r="7" fill="#28c840"/>
</g>

<g transform="translate(9, 41)" clip-path="url(#terminal-3369331553-clip-terminal)">
<rect fill="#004578" x="0" y="1.5" width="85.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#004578" x="85.4" y="1.5" width="268.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#004578" x="353.8" y="1.5" width="183" height="24.65" shape-rendering="crispEdges"/><rect fill="#004578" x="536.8" y="1.5" width="85.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#073b61" x="622.2" y="1.5" width="353.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#fea62b" x="0" y="25.9" width="85.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="85.4" y="25.9" width="268.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="353.8" y="25.9" width="183" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="536.8" y="25.9" width="85.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="622.2" y="25.9" width="353.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="50.3" width="85.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="85.4" y="50.3" width="97.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="183" y="50.3" width="73.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="256.2" y="50.3" width="97.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="353.8" y="50.3" width="183" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="536.8" y="50.3" width="85.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="622.2" y="50.3" width="353.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="74.7" width="85.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="85.4" y="74.7" width="268.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="353.8" y="74.7" width="183" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="536.8" y="74.7" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="549" y="74.7" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="573.4" y="74.7" width="48.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="622.2" y="74.7" width="353.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="99.1" width="85.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="85.4" y="99.1" width="268.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="353.8" y="99.1" width="183" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="536.8" y="99.1" width="85.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="622.2" y="99.1" width="353.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="123.5" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="12.2" y="123.5" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="36.6" y="123.5" width="48.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="85.4" y="123.5" width="268.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="353.8" y="123.5" width="183" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="536.8" y="123.5" width="85.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="622.2" y="123.5" width="353.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="147.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="172.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="196.7" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="221.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="245.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="269.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="294.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="318.7" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="343.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="367.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="391.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="416.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="440.7" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="465.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="489.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="513.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="538.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="562.7" width="976" height="24.65" shape-rendering="crispEdges"/>
<g class="terminal-3369331553-matrix">
<text class="terminal-3369331553-r1" x="0" y="20" textLength="85.4" clip-path="url(#terminal-3369331553-line-0)">&#160;lane&#160;&#160;</text><text class="terminal-3369331553-r1" x="85.4" y="20" textLength="268.4" clip-path="url(#terminal-3369331553-line-0)">&#160;swimmer&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-3369331553-r1" x="353.8" y="20" textLength="183" clip-path="url(#terminal-3369331553-line-0)">&#160;country&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-3369331553-r1" x="536.8" y="20" textLength="85.4" clip-path="url(#terminal-3369331553-line-0)">&#160;time&#160;&#160;</text><text class="terminal-3369331553-r3" x="976" y="20" textLength="12.2" clip-path="url(#terminal-3369331553-line-0)">
</text><text class="terminal-3369331553-r4" x="0" y="44.4" textLength="85.4" clip-path="url(#terminal-3369331553-line-1)">&#160;3&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-3369331553-r5" x="85.4" y="44.4" textLength="268.4" clip-path="url(#terminal-3369331553-line-1)">&#160;Li&#160;Zhuhao&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-3369331553-r5" x="353.8" y="44.4" textLength="183" clip-path="url(#terminal-3369331553-line-1)">&#160;China&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-3369331553-r5" x="536.8" y="44.4" textLength="85.4" clip-path="url(#terminal-3369331553-line-1)">&#160;51.26&#160;</text><text class="terminal-3369331553-r3" x="976" y="44.4" textLength="12.2" clip-path="url(#terminal-3369331553-line-1)">
</text><text class="terminal-3369331553-r5" x="0" y="68.8" textLength="85.4" clip-path="url(#terminal-3369331553-line-2)">&#160;eight&#160;</text><text class="terminal-3369331553-r6" x="183" y="68.8" textLength="73.2" clip-path="url(#terminal-3369331553-line-2)">∅&#160;null</text><text class="terminal-3369331553-r5" x="353.8" y="68.8" textLength="183" clip-path="url(#terminal-3369331553-line-2)">&#160;France&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-3369331553-r5" x="536.8" y="68.8" textLength="85.4" clip-path="url(#terminal-3369331553-line-2)">&#160;51.58&#160;</text><text class="terminal-3369331553-r3" x="976" y="68.8" textLength="12.2" clip-path="url(#terminal-3369331553-line-2)">
</text><text class="terminal-3369331553-r5" x="0" y="93.2" textLength="85.4" clip-path="url(#terminal-3369331553-line-3)">&#160;seven&#160;</text><text class="terminal-3369331553-r5" x="85.4" y="93.2" textLength="268.4" clip-path="url(#terminal-3369331553-line-3)">&#160;Tom&#160;Shields&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-3369331553-r5" x="353.8" y="93.2" textLength="183" clip-path="url(#terminal-3369331553-line-3)">&#160;United&#160;States&#160;</text><text class="terminal-3369331553-r6" x="549" y="93.2" textLength="24.4" clip-path="url(#terminal-3369331553-line-3)">∅&#160;</text><text class="terminal-3369331553-r3" x="976" y="93.2" textLength="12.2" clip-path="url(#terminal-3369331553-line-3)">
</text><text class="terminal-3369331553-r5" x="0" y="117.6" textLength="85.4" clip-path="url(#terminal-3369331553-line-4)">&#160;1&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-3369331553-r5" x="85.4" y="117.6" textLength="268.4" clip-path="url(#terminal-3369331553-line-4)">&#160;Aleksandr&#160;Sadovnikov&#160;</text><text class="terminal-3369331553-r5" x="353.8" y="117.6" textLength="183" clip-path="url(#terminal-3369331553-line-4)">&#160;Russia&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-3369331553-r5" x="536.8" y="117.6" textLength="85.4" clip-path="url(#terminal-3369331553-line-4)">&#160;51.84&#160;</text><text class="terminal-3369331553-r3" x="976" y="117.6" textLength="12.2" clip-path="url(#terminal-3369331553-line-4)">
</text><text class="terminal-3369331553-r6" x="12.2" y="142" textLength="24.4" clip-path="url(#terminal-3369331553-line-5)">∅&#160;</text><text class="terminal-3369331553-r5" x="85.4" y="142" textLength="268.4" clip-path="url(#terminal-3369331553-line-5)">&#160;Darren&#160;Burns&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-3369331553-r5" x="353.8" y="142" textLength="183" clip-path="url(#terminal-3369331553-line-5)">&#160;Scotland&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-3369331553-r5" x="536.8" y="142" textLength="85.4" clip-path="url(#terminal-3369331553-line-5)">&#160;51.84&#160;</text><text class="terminal-3369331553-r3" x="976" y="142" textLength="12.2" clip-path="url(#terminal-3369331553-line-5)">
</text><text class="terminal-3369331553-r3" x="976" y="166.4" textLength="12.2" clip-path="url(#terminal-3369331553-line-6)">
</text><text class="terminal-3369331553-r3" x="976" y="190.8" textLength="12.2" clip-path="url(#terminal-3369331553-line-7)">
</text><text class="terminal-3369331553-r3" x="976" y="215.2" textLength="12.2" clip-path="url(#terminal-3369331553-line-8)">
</text><text class="terminal-3369331553-r3" x="976" y="239.6" textLength="12.2" clip-path="url(#terminal-3369331553-line-9)">
</text><text class="terminal-3369331553-r3" x="976" y="264" textLength="12.2" clip-path="url(#terminal-3369331553-line-10)">
</text><text class="terminal-3369331553-r3" x="976" y="288.4" textLength="12.2" clip-path="url(#terminal-3369331553-line-11)">
</text><text class="terminal-3369331553-r3" x="976" y="312.8" textLength="12.2" clip-path="url(#terminal-3369331553-line-12)">
</text><text class="terminal-3369331553-r3" x="976" y="337.2" textLength="12.2" clip-path="url(#terminal-3369331553-line-13)">
</text><text class="terminal-3369331553-r3" x="976" y="361.6" textLength="12.2" clip-path="url(#terminal-3369331553-line-14)">
</text><text class="terminal-3369331553-r3" x="976" y="386" textLength="12.2" clip-path="url(#terminal-3369331553-line-15)">
</text><text class="terminal-3369331553-r3" x="976" y="410.4" textLength="12.2" clip-path="url(#terminal-3369331553-line-16)">
</text><text class="terminal-3369331553-r3" x="976" y="434.8" textLength="12.2" clip-path="url(#terminal-3369331553-line-17)">
</text><text class="terminal-3369331553-r3" x="976" y="459.2" textLength="12.2" clip-path="url(#terminal-3369331553-line-18)">
</text><text class="terminal-3369331553-r3" x="976" y="483.6" textLength="12.2" clip-path="url(#terminal-3369331553-line-19)">
</text><text class="terminal-3369331553-r3" x="976" y="508" textLength="12.2" clip-path="url(#terminal-3369331553-line-20)">
</text><text class="terminal-3369331553-r3" x="976" y="532.4" textLength="12.2" clip-path="url(#terminal-3369331553-line-21)">
</text><text class="terminal-3369331553-r3" x="976" y="556.8" textLength="12.2" clip-path="url(#terminal-3369331553-line-22)">
</text>
</g>
</g>
</svg>

'''
# ---
# name: test_datatable_range_cursor_render
'''
<svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
Expand Down
22 changes: 22 additions & 0 deletions tests/snapshot_tests/snapshot_apps/data_table_null_mixed_cols.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from textual.app import App, ComposeResult
from textual_fastdatatable import ArrowBackend, DataTable

ROWS = [
("lane", "swimmer", "country", "time"),
(3, "Li Zhuhao", "China", 51.26),
("eight", None, "France", 51.58),
("seven", "Tom Shields", "United States", None),
(1, "Aleksandr Sadovnikov", "Russia", 51.84),
(None, "Darren Burns", "Scotland", 51.84),
]


class TableApp(App):
def compose(self) -> ComposeResult:
backend = ArrowBackend.from_records(ROWS, has_header=True)
yield DataTable(backend=backend, null_rep="[dim]∅ null[/]")


app = TableApp()
if __name__ == "__main__":
app.run()
4 changes: 4 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def test_datatable_no_render_markup(snap_compare: Callable) -> None:
assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_no_render_markup.py")


def test_datatable_null_mixed_cols(snap_compare: Callable) -> None:
assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_null_mixed_cols.py")


def test_datatable_range_cursor_render(snap_compare: Callable) -> None:
press = ["right", "down", "shift+right", "shift+down", "shift+down"]
assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_range_cursor.py", press=press)
Expand Down

0 comments on commit 14d01ee

Please sign in to comment.