1111
1212cfg = Config ()
1313
14- # Initialize the client
1514w = WorkspaceClient ()
1615
17- # List SQL Warehouses
1816warehouses = w .warehouses .list ()
1917
20- # Create a dictionary to map warehouse names to their paths
2118warehouse_paths = {wh .name : wh .odbc_params .path for wh in warehouses }
2219
23- # List catalogs
2420catalogs = w .catalogs .list ()
2521
22+
2623@st .cache_resource
2724def get_connection (http_path ):
2825 return sql .connect (
@@ -31,58 +28,50 @@ def get_connection(http_path):
3128 credentials_provider = lambda : cfg .authenticate ,
3229 )
3330
31+
3432def read_table (table_name , conn ):
3533 with conn .cursor () as cursor :
3634 query = f"SELECT * FROM { table_name } "
3735 cursor .execute (query )
3836 return cursor .fetchall_arrow ().to_pandas ()
3937
38+
4039def get_schema_names (catalog_name ):
4140 schemas = w .schemas .list (catalog_name = catalog_name )
4241 return [schema .name for schema in schemas ]
4342
43+
4444def get_table_names (catalog_name , schema_name ):
4545 tables = w .tables .list (catalog_name = catalog_name , schema_name = schema_name )
4646 return [table .name for table in tables ]
4747
48+
4849tab_a , tab_b , tab_c = st .tabs (["**Try it**" , "**Code snippet**" , "**Requirements**" ])
4950
5051with tab_a :
5152 http_path_input = st .selectbox (
52- "Select your Databricks SQL Warehouse :" , ["" ] + list (warehouse_paths .keys ())
53+ "Select a SQL warehouse :" , ["" ] + list (warehouse_paths .keys ())
5354 )
5455
5556 catalog_name = st .selectbox (
56- "Select your Catalog :" , ["" ] + [catalog .name for catalog in catalogs ]
57+ "Select a catalog :" , ["" ] + [catalog .name for catalog in catalogs ]
5758 )
5859
5960 if http_path_input == "" or catalog_name == "" :
6061 st .warning ("Select Warehouse and Catalog" )
6162
6263 if catalog_name and catalog_name != "" :
6364 schema_names = get_schema_names (catalog_name )
64- schema_name = st .selectbox (
65- "Select your Schema:" , ["" ] + schema_names
66- )
67- if schema_name == "" :
68- st .warning ("Select Schema" )
65+ schema_name = st .selectbox ("Select a schema:" , ["" ] + schema_names )
6966
7067 if catalog_name and catalog_name != "" and schema_name and schema_name != "" :
7168 table_names = get_table_names (catalog_name , schema_name )
72- table_name = st .selectbox (
73- "Select your Table:" , ["" ] + table_names
74- )
75-
76- if table_name == "" :
77- st .warning ("Select Table" )
69+ table_name = st .selectbox ("Select a table:" , ["" ] + table_names )
7870
7971 if http_path_input and table_name and table_name != "" :
8072 http_path = warehouse_paths [http_path_input ]
8173 conn = get_connection (http_path )
82- #info_placeholder = st.empty()
83- st .info (f"Running Select on { catalog_name } .{ schema_name } .{ table_name } " )
8474 df = read_table (f"{ catalog_name } .{ schema_name } .{ table_name } " , conn )
85- #info_placeholder.empty() # Clear the info message
8675 st .dataframe (df )
8776
8877
@@ -92,23 +81,12 @@ def get_table_names(catalog_name, schema_name):
9281 import streamlit as st
9382 from databricks import sql
9483 from databricks.sdk.core import Config
95- from databricks.sdk import WorkspaceClient
96-
97- cfg = Config()
98-
99- # Initialize the client
100- w = WorkspaceClient()
10184
102- # List SQL Warehouses
103- warehouses = w.warehouses.list()
10485
105- # Create a dictionary to map warehouse names to their paths
106- warehouse_paths = {wh.name: wh.odbc_params.http_path for wh in warehouses}
86+ cfg = Config() # Set the DATABRICKS_HOST environment variable when running locally
10787
108- # List catalogs
109- catalogs = w.catalogs.list()
11088
111- @st.cache_resource
89+ @st.cache_resource # connection is cached
11290 def get_connection(http_path):
11391 return sql.connect(
11492 server_hostname=cfg.host,
@@ -122,37 +100,16 @@ def read_table(table_name, conn):
122100 cursor.execute(query)
123101 return cursor.fetchall_arrow().to_pandas()
124102
125- def get_schema_names(catalog_name):
126- schemas = w.schemas.list(catalog_name=catalog_name)
127- return [schema.name for schema in schemas]
128-
129- def get_table_names(catalog_name, schema_name):
130- tables = w.tables.list(catalog_name=catalog_name, schema_name=schema_name)
131- return [table.name for table in tables]
132-
133- http_path_input = st.selectbox(
134- "Select your Databricks SQL Warehouse", list(warehouse_paths.keys()), placeholder="select warehouse"
103+ http_path_input = st.text_input(
104+ "Enter your Databricks HTTP Path:", placeholder="/sql/1.0/warehouses/xxxxxx"
135105 )
136106
137- catalog_name = st.selectbox (
138- "Select your Catalog:", [ catalog.name for catalog in catalogs]
107+ table_name = st.text_input (
108+ "Specify a Unity Catalog table name :", placeholder=" catalog.schema.table"
139109 )
140110
141- if catalog_name:
142- schema_names = get_schema_names(catalog_name)
143- schema_name = st.selectbox(
144- "Select your Schema:", schema_names
145- )
146-
147- if catalog_name and schema_name:
148- table_names = get_table_names(catalog_name, schema_name)
149- table_name = st.selectbox(
150- "Select your Table:", table_names
151- )
152-
153111 if http_path_input and table_name:
154- http_path = warehouse_paths[http_path_input]
155- conn = get_connection(http_path)
112+ conn = get_connection(http_path_input)
156113 df = read_table(table_name, conn)
157114 st.dataframe(df)
158115 """
@@ -179,4 +136,4 @@ def get_table_names(catalog_name, schema_name):
179136 * [Databricks SDK](https://pypi.org/project/databricks-sdk/) - `databricks-sdk`
180137 * [Databricks SQL Connector](https://pypi.org/project/databricks-sql-connector/) - `databricks-sql-connector`
181138 * [Streamlit](https://pypi.org/project/streamlit/) - `streamlit`
182- """ )
139+ """ )
0 commit comments