-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Added Shiny for Python example (#74)
Shiny for Python app using the SDK to access Databricks
- Loading branch information
Showing
3 changed files
with
97 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,37 @@ | ||
# Shiny for Python Example | ||
|
||
## Start the app locally | ||
|
||
```bash | ||
shiny run app.py | ||
``` | ||
|
||
## Deploy to Posit Connect | ||
|
||
Validate that `rsconnect-python` is installed: | ||
|
||
```bash | ||
rsconnect version | ||
``` | ||
|
||
Or install it as documented in the [installation](https://docs.posit.co/rsconnect-python/#installation) section of the documentation. | ||
|
||
To publish, make sure `CONNECT_SERVER`, `CONNECT_API_KEY`, `DATABRICKS_HOST`, `DATABRICKS_PATH` have valid values. Then, on a terminal session, enter the following command: | ||
|
||
```bash | ||
rsconnect deploy shiny . \ | ||
--server "${CONNECT_SERVER}" \ | ||
--api-key "${CONNECT_API_KEY}" \ | ||
--environment DATABRICKS_HOST \ | ||
--environment DATABRICKS_PATH | ||
``` | ||
|
||
Note that the Databricks environment variables do not need to be resolved by the shell, so they do not include the `$` prefix. | ||
|
||
The Databricks environment variables only need to be set once, unless a change needs to be made. If the values have not changed, you don’t need to provide them again when you publish updates to the document. | ||
|
||
``` | ||
rsconnect deploy shiny . \ | ||
--server "${CONNECT_SERVER}" \ | ||
--api-key "${CONNECT_API_KEY}" | ||
``` |
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,56 @@ | ||
# -*- coding: utf-8 -*- | ||
# mypy: ignore-errors | ||
import os | ||
|
||
import pandas as pd | ||
|
||
from posit.connect.external.databricks import viewer_credentials_provider | ||
|
||
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_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) | ||
|
||
@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) |
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,4 @@ | ||
databricks-sql-connector==3.0.1 | ||
databricks-sdk==0.20.0 | ||
shiny==0.7.1 | ||
git+https://github.com/posit-dev/posit-sdk-py.git |