Skip to content

Commit

Permalink
Add databricks viewer-oauth sample streamlit app
Browse files Browse the repository at this point in the history
  • Loading branch information
dbkegley committed Jun 25, 2024
1 parent e330b8d commit ba9e6b0
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
64 changes: 64 additions & 0 deletions local/python-oauth-databricks-app/app.py
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}"
SQL_HTTP_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 SQL_HTTP_PATH == None:
raise ValueError("DATABRICKS_HOST and SQL_HTTP_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=SQL_HTTP_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)
24 changes: 24 additions & 0 deletions local/python-oauth-databricks-app/manifest.json
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": "5106e1ace88c8cbaa5c1e1608a22013c"
}
}
}
5 changes: 5 additions & 0 deletions local/python-oauth-databricks-app/requirements.txt
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

0 comments on commit ba9e6b0

Please sign in to comment.