Skip to content

Commit b0482c3

Browse files
committed
Prevent option collisions in Marimo pickers.
Disambiguate duplicate labels for recent results and connection dropdowns so entries are never dropped, and add tests for both mappings.
1 parent 4929c60 commit b0482c3

3 files changed

Lines changed: 62 additions & 4 deletions

File tree

hotdata_marimo/display.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@
99
from hotdata_runtime.result import QueryResult
1010

1111

12+
def _option_map_with_unique_labels(
13+
pairs: list[tuple[str, str]],
14+
) -> dict[str, str]:
15+
counts: dict[str, int] = {}
16+
options: dict[str, str] = {}
17+
for label, value in pairs:
18+
count = counts.get(label, 0)
19+
counts[label] = count + 1
20+
key = label if count == 0 else f"{label} ({count + 1})"
21+
options[key] = value
22+
return options
23+
24+
1225
def query_result(
1326
result: QueryResult,
1427
*,
@@ -61,9 +74,11 @@ def __init__(self, client: HotdataClient, *, limit: int = 50) -> None:
6174
self._client = client
6275
listing = client.results().list_results(limit=limit, offset=0)
6376
self._results = listing.results
64-
options = {
65-
f"{r.created_at} · {r.status} · {r.id}": r.id for r in self._results
66-
}
77+
option_pairs = [
78+
(f"{r.created_at} · {r.status} · {r.id}", r.id)
79+
for r in self._results
80+
]
81+
options = _option_map_with_unique_labels(option_pairs)
6782
self.pick = mo.ui.dropdown(
6883
options=options or {"(no results)": ""},
6984
label="Recent results",

hotdata_marimo/table_browser.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
from hotdata_runtime.client import HotdataClient
88

99

10+
def _connection_options(conns: list[Any]) -> dict[str, str]:
11+
counts: dict[str, int] = {}
12+
options: dict[str, str] = {}
13+
for c in conns:
14+
label = c.name
15+
count = counts.get(label, 0)
16+
counts[label] = count + 1
17+
key = label if count == 0 else f"{label} ({c.id})"
18+
options[key] = c.id
19+
return options
20+
21+
1022
def connection_picker(
1123
client: HotdataClient,
1224
*,
@@ -21,7 +33,7 @@ def connection_picker(
2133
label=label,
2234
full_width=full_width,
2335
)
24-
options = {c.name: c.id for c in conns}
36+
options = _connection_options(conns)
2537
return mo.ui.dropdown(
2638
options=options,
2739
label=label,

tests/test_options.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from __future__ import annotations
2+
3+
from types import SimpleNamespace
4+
5+
from hotdata_marimo.display import _option_map_with_unique_labels
6+
from hotdata_marimo.table_browser import _connection_options
7+
8+
9+
def test_option_map_with_unique_labels_keeps_all_values():
10+
options = _option_map_with_unique_labels(
11+
[("dup", "a"), ("dup", "b"), ("dup", "c")]
12+
)
13+
assert options == {
14+
"dup": "a",
15+
"dup (2)": "b",
16+
"dup (3)": "c",
17+
}
18+
19+
20+
def test_connection_options_disambiguates_duplicate_names():
21+
conns = [
22+
SimpleNamespace(name="Warehouse", id="conn_1"),
23+
SimpleNamespace(name="Warehouse", id="conn_2"),
24+
SimpleNamespace(name="Analytics", id="conn_3"),
25+
]
26+
options = _connection_options(conns)
27+
assert options == {
28+
"Warehouse": "conn_1",
29+
"Warehouse (conn_2)": "conn_2",
30+
"Analytics": "conn_3",
31+
}

0 commit comments

Comments
 (0)