1212 resolve_connection_picker ,
1313)
1414
15+ __all__ = ["TableBrowser" , "connection_picker" , "table_browser" ]
16+
1517
1618class TableBrowser :
1719 """Pick a fully qualified `connection.schema.table` and inspect columns.
@@ -43,58 +45,26 @@ def __init__(
4345 )
4446
4547 self ._table_pick_ctx : str | None = None
46- self ._rebuilt_table_pick_this_run = False
4748 self ._init_table_pick ()
4849
49- def _init_table_pick (self ) -> None :
50- if self ._conn_pick is not None :
51- self .table_pick = empty_dropdown (
52- label = "Table" ,
53- message = "(select connection above)" ,
54- )
55- self ._empty_catalog = True
56- self ._all_names = []
57- self ._table_pick_ctx = ""
58- return
59-
60- names = self ._names_for_active_connection ()
61- self ._all_names = names
62- if not names :
63- self .table_pick = empty_dropdown (
64- label = "Table" ,
65- message = "(no tables in catalog)" ,
66- )
67- self ._empty_catalog = True
68- else :
69- self ._empty_catalog = False
70- self .table_pick = mo .ui .dropdown (
71- options = {n : n for n in names },
72- label = "Table" ,
73- full_width = True ,
74- searchable = True ,
75- )
76- self ._table_pick_ctx = self ._active_connection_id ()
77-
7850 def _active_connection_id (self ) -> str | None :
7951 if self ._override_connection_id is not None :
8052 return self ._override_connection_id or None
8153 if self ._conn_pick is not None :
82- v = self ._conn_pick .value # type: ignore[attr-defined]
83- return v if v else None
84- if self ._implicit_connection_id is None :
85- return None
54+ return self ._conn_pick .value or None # type: ignore[attr-defined]
8655 return self ._implicit_connection_id or None
8756
8857 def _names_for_active_connection (self ) -> list [str ]:
8958 cid = self ._active_connection_id ()
90- if cid is None or cid == "" :
59+ if not cid :
9160 return []
9261 return self ._client .list_qualified_table_names (
9362 limit = self ._table_limit ,
9463 connection_id = cid ,
9564 )
9665
97- def _rebuild_table_pick (self , names : list [str ]) -> None :
66+ def _set_table_pick (self , names : list [str ]) -> None :
67+ """Create or replace the table dropdown for the given names list."""
9868 self ._all_names = names
9969 if not names :
10070 self ._empty_catalog = True
@@ -111,7 +81,32 @@ def _rebuild_table_pick(self, names: list[str]) -> None:
11181 searchable = True ,
11282 )
11383 self ._table_pick_ctx = self ._active_connection_id ()
114- self ._rebuilt_table_pick_this_run = True
84+
85+ def _init_table_pick (self ) -> None :
86+ if self ._conn_pick is not None :
87+ self ._all_names = []
88+ self ._empty_catalog = True
89+ self .table_pick = empty_dropdown (
90+ label = "Table" ,
91+ message = "(select connection above)" ,
92+ )
93+ self ._table_pick_ctx = ""
94+ return
95+ self ._set_table_pick (self ._names_for_active_connection ())
96+
97+ def _sync_table_catalog (self ) -> bool :
98+ """Refresh the table dropdown when the active connection changes.
99+
100+ Returns True if the dropdown was rebuilt (so the caller knows not to
101+ read ``.value`` on the new widget in the same Marimo run).
102+ """
103+ if self ._conn_pick is not None :
104+ _ = self ._conn_pick .value # type: ignore[attr-defined]
105+ cid = self ._active_connection_id ()
106+ if not cid or cid == self ._table_pick_ctx :
107+ return False
108+ self ._set_table_pick (self ._names_for_active_connection ())
109+ return True
115110
116111 @property
117112 def selected_connection_id (self ) -> str | None :
@@ -122,30 +117,17 @@ def selected_table(self) -> str | None:
122117 v = self .table_pick .value
123118 return v if v else None
124119
125- def _sync_table_catalog (self ) -> None :
126- """Refresh the table dropdown when the active connection changes."""
127- if self ._conn_pick is not None :
128- _ = self ._conn_pick .value # type: ignore[attr-defined]
129- cid = self ._active_connection_id ()
130- if not cid :
131- return
132- if cid == self ._table_pick_ctx :
133- return
134- self ._rebuild_table_pick (self ._names_for_active_connection ())
135-
136120 @property
137121 def ui (self ):
138- self ._rebuilt_table_pick_this_run = False
139- self ._sync_table_catalog ()
140-
141- if not self ._rebuilt_table_pick_this_run :
122+ rebuilt = self ._sync_table_catalog ()
123+ if not rebuilt :
142124 _ = self .table_pick .value
143125
144- sel = None if self . _rebuilt_table_pick_this_run else self .selected_table
126+ sel = None if rebuilt else self .selected_table
145127 cid = self ._active_connection_id ()
146128 conn_header = (
147- mo .md (f"**Connection** `{ self . _active_connection_id () } `" )
148- if self . _active_connection_id ()
129+ mo .md (f"**Connection** `{ cid } `" )
130+ if cid
149131 else None
150132 )
151133 if not sel :
0 commit comments