44
55import pandas as pd
66from databricks import sql
7- from databricks .sdk .core import ApiClient , Config
7+ from databricks .sdk .core import ApiClient , Config , databricks_cli
88from databricks .sdk .service .iam import CurrentUserAPI
9- from posit .connect .external .databricks import viewer_credentials_provider
9+ from posit .connect .external .databricks import PositCredentialsStrategy
1010from shiny import App , Inputs , Outputs , Session , render , ui
1111
1212DATABRICKS_HOST = os .getenv ("DATABRICKS_HOST" )
@@ -24,9 +24,14 @@ def server(i: Inputs, o: Outputs, session: Session):
2424 session_token = session .http_conn .headers .get (
2525 "Posit-Connect-User-Session-Token"
2626 )
27- credentials_provider = viewer_credentials_provider (
28- user_session_token = session_token
29- )
27+ posit_strategy = PositCredentialsStrategy (
28+ local_strategy = databricks_cli ,
29+ user_session_token = session_token )
30+ cfg = Config (
31+ host = DATABRICKS_HOST_URL ,
32+ # uses Posit's custom credential_strategy if running on Connect,
33+ # otherwise falls back to the strategy defined by local_strategy
34+ credentials_strategy = posit_strategy )
3035
3136 @render .data_frame
3237 def result ():
@@ -35,8 +40,8 @@ def result():
3540 with sql .connect (
3641 server_hostname = DATABRICKS_HOST ,
3742 http_path = SQL_HTTP_PATH ,
38- auth_type = " databricks-oauth" ,
39- credentials_provider = credentials_provider ,
43+ # https://github.com/ databricks/databricks-sql-python/issues/148#issuecomment-2271561365
44+ credentials_provider = posit_strategy . sql_credentials_provider ( cfg ) ,
4045 ) as connection :
4146 with connection .cursor () as cursor :
4247 cursor .execute (query )
@@ -48,9 +53,6 @@ def result():
4853
4954 @render .text
5055 def text ():
51- cfg = Config (
52- host = DATABRICKS_HOST_URL , credentials_provider = credentials_provider
53- )
5456 databricks_user_info = CurrentUserAPI (ApiClient (cfg )).me ()
5557 return f"Hello, { databricks_user_info .display_name } !"
5658
0 commit comments