66
77from hotdata_runtime import HotdataClient
88
9-
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-
22- def connection_picker (
23- client : HotdataClient ,
24- * ,
25- label : str = "Connection" ,
26- full_width : bool = True ,
27- ):
28- listing = client .connections ().list_connections ()
29- conns = listing .connections
30- if not conns :
31- return mo .ui .dropdown (
32- options = {"(no connections)" : "" },
33- label = label ,
34- full_width = full_width ,
35- )
36- options = _connection_options (conns )
37- return mo .ui .dropdown (
38- options = options ,
39- label = label ,
40- full_width = full_width ,
41- )
9+ from hotdata_marimo ._options import (
10+ connection_picker ,
11+ empty_dropdown ,
12+ resolve_connection_picker ,
13+ )
4214
4315
4416class TableBrowser :
@@ -66,44 +38,40 @@ def __init__(
6638 self ._implicit_connection_id : str | None = None
6739
6840 if self ._override_connection_id is None :
69- listing = client .connections ().list_connections ()
70- conns = listing .connections
71- if len (conns ) > 1 :
72- self ._conn_pick = connection_picker (client )
73- elif len (conns ) == 1 :
74- self ._implicit_connection_id = conns [0 ].id
75- else :
76- self ._implicit_connection_id = ""
41+ self ._conn_pick , self ._implicit_connection_id = resolve_connection_picker (
42+ client
43+ )
7744
7845 self ._table_pick_ctx : str | None = None
46+ self ._init_table_pick ()
7947
48+ def _init_table_pick (self ) -> None :
8049 if self ._conn_pick is not None :
81- self .table_pick = mo .ui .dropdown (
82- options = {"(select connection above)" : "" },
50+ self .table_pick = empty_dropdown (
8351 label = "Table" ,
84- full_width = True ,
52+ message = "(select connection above)" ,
53+ )
54+ self ._empty_catalog = True
55+ self ._all_names = []
56+ return
57+
58+ names = self ._names_for_active_connection ()
59+ self ._all_names = names
60+ if not names :
61+ self .table_pick = empty_dropdown (
62+ label = "Table" ,
63+ message = "(no tables in catalog)" ,
8564 )
8665 self ._empty_catalog = True
87- self ._all_names : list [str ] = []
8866 else :
89- names = self ._names_for_active_connection ()
90- self ._all_names = names
91- if not names :
92- self .table_pick = mo .ui .dropdown (
93- options = {"(no tables in catalog)" : "" },
94- label = "Table" ,
95- full_width = True ,
96- )
97- self ._empty_catalog = True
98- else :
99- self ._empty_catalog = False
100- self .table_pick = mo .ui .dropdown (
101- options = {n : n for n in names },
102- label = "Table" ,
103- full_width = True ,
104- searchable = True ,
105- )
106- self ._table_pick_ctx = self ._active_connection_id ()
67+ self ._empty_catalog = False
68+ self .table_pick = mo .ui .dropdown (
69+ options = {n : n for n in names },
70+ label = "Table" ,
71+ full_width = True ,
72+ searchable = True ,
73+ )
74+ self ._table_pick_ctx = self ._active_connection_id ()
10775
10876 def _active_connection_id (self ) -> str | None :
10977 if self ._override_connection_id is not None :
@@ -128,10 +96,9 @@ def _rebuild_table_pick(self, names: list[str]) -> None:
12896 self ._all_names = names
12997 if not names :
13098 self ._empty_catalog = True
131- self .table_pick = mo .ui .dropdown (
132- options = {"(no tables in catalog)" : "" },
99+ self .table_pick = empty_dropdown (
133100 label = "Table" ,
134- full_width = True ,
101+ message = "(no tables in catalog)" ,
135102 )
136103 else :
137104 self ._empty_catalog = False
0 commit comments