-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add databricks viewer-oauth sample streamlit app
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -*- coding: utf-8 -*- | ||
import os | ||
|
||
import pandas as pd | ||
|
||
from posit.connect.external.databricks import viewer_credentials_provider | ||
from shiny.ui.dataframe import output_data_frame | ||
|
||
from databricks import sql | ||
from databricks.sdk.service.iam import CurrentUserAPI | ||
from databricks.sdk.core import ApiClient, Config | ||
|
||
from shiny import App, Inputs, Outputs, Session, render, ui | ||
|
||
DATABRICKS_HOST = os.getenv("DATABRICKS_HOST") | ||
DATABRICKS_HOST_URL = f"https://{DATABRICKS_HOST}" | ||
DATABRICKS_SQL_PATH = os.getenv("DATABRICKS_SQL_PATH") | ||
|
||
app_ui = ui.page_fluid(ui.output_text("text"), ui.output_data_frame("result")) | ||
|
||
|
||
def server(input: Inputs, output: Outputs, session: Session): | ||
""" | ||
Shiny for Python example application that shows user information and | ||
the first few rows from a table hosted in Databricks. | ||
""" | ||
session_token = session.http_conn.headers.get( | ||
"Posit-Connect-User-Session-Token" | ||
) | ||
credentials_provider = viewer_credentials_provider( | ||
user_session_token=session_token | ||
) | ||
|
||
if DATABRICKS_HOST == None or DATABRICKS_SQL_PATH == None: | ||
raise ValueError("DATABRICKS_HOST and DATABRICKS_SQL_PATH environment variables must be set") | ||
|
||
@render.data_frame | ||
def result(): | ||
query = "SELECT * FROM samples.nyctaxi.trips LIMIT 10;" | ||
|
||
with sql.connect( | ||
server_hostname=DATABRICKS_HOST, | ||
http_path=DATABRICKS_SQL_PATH, | ||
auth_type="databricks-oauth", | ||
credentials_provider=credentials_provider, | ||
) as connection: | ||
with connection.cursor() as cursor: | ||
cursor.execute(query) | ||
rows = cursor.fetchall() | ||
df = pd.DataFrame( | ||
rows, columns=[col[0] for col in cursor.description] | ||
) | ||
return df | ||
|
||
@render.text | ||
def text(): | ||
cfg = Config( | ||
host=DATABRICKS_HOST_URL, credentials_provider=credentials_provider | ||
) | ||
databricks_user_info = CurrentUserAPI(ApiClient(cfg)).me() | ||
return f"Hello, {databricks_user_info.display_name}!" | ||
|
||
|
||
app = App(app_ui, server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"version": 1, | ||
"locale": "en_US.UTF-8", | ||
"metadata": { | ||
"appmode": "python-shiny", | ||
"entrypoint": "app" | ||
}, | ||
"python": { | ||
"version": "3.11.3", | ||
"package_manager": { | ||
"name": "pip", | ||
"version": "22.3.1", | ||
"package_file": "requirements.txt" | ||
} | ||
}, | ||
"files": { | ||
"requirements.txt": { | ||
"checksum": "358e0c8dab918144b22f1e48e5632eef" | ||
}, | ||
"app.py": { | ||
"checksum": "806a715aff9e0145e73f61db6d3f9d9c" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
databricks-sql-connector==3.0.1 | ||
databricks-sdk==0.20.0 | ||
numpy<2.0.0 | ||
posit-sdk==0.2.2 | ||
shiny==0.7.1 |