Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Added Shiny for Python example #74

Merged
merged 12 commits into from
Mar 5, 2024
37 changes: 37 additions & 0 deletions examples/connect/shiny-python/README.md
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
```
plascaray marked this conversation as resolved.
Show resolved Hide resolved

## 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}"
```
57 changes: 57 additions & 0 deletions examples/connect/shiny-python/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- 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_PAT = os.getenv("DATABRICKS_TOKEN")
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)
4 changes: 4 additions & 0 deletions examples/connect/shiny-python/requirements.txt
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
Loading