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 FastAPI example. #77

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/connect/fastapi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# FastAPI Example

## Start the app locally

```bash
pip install "uvicorn[standard]"
uvicorn app:app --reload
curl -X 'GET' 'http://127.0.0.1:8000/fares'
```

## 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 . \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rsconnect deploy shiny . \
rsconnect deploy fastapi . \

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch @jonkeane

--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 . \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rsconnect deploy shiny . \
rsconnect deploy fastapi . \

--server "${CONNECT_SERVER}" \
--api-key "${CONNECT_API_KEY}"
```
52 changes: 52 additions & 0 deletions examples/connect/fastapi/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
# mypy: ignore-errors
import os

from posit.connect.external.databricks import viewer_credentials_provider

from databricks import sql

from typing import Annotated

from fastapi import FastAPI, Header, Depends
from fastapi.responses import JSONResponse

DATABRICKS_HOST=os.getenv("DATABRICKS_HOST")
DATABRICKS_HOST_URL = f"https://{DATABRICKS_HOST}"
SQL_HTTP_PATH = os.getenv("DATABRICKS_PATH")

rows = None
app = FastAPI()

async def get_token(
posit_connect_user_session_token: Annotated[str | None, Header()] = None
) -> dict | None:
"""
Get the token from the Posit-Connect-User-Session-Token header.
"""
if posit_connect_user_session_token is None:
return None
return posit_connect_user_session_token

@app.get("/fares")
async def get_fares(session_token=Depends(get_token)) -> JSONResponse:
"""
FastAPI example API that returns the first few rows from
a table hosted in Databricks.
"""
global rows

credentials_provider = viewer_credentials_provider(user_session_token=session_token)

if rows is None:
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()

return [row.asDict() for row in rows]
4 changes: 4 additions & 0 deletions examples/connect/fastapi/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
fastapi==0.110.0
git+https://github.com/posit-dev/posit-sdk-py.git
Loading