-
Notifications
You must be signed in to change notification settings - Fork 4
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
+102
−0
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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 . \ | ||||||
--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 . \ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
--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,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] |
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 | ||
fastapi==0.110.0 | ||
git+https://github.com/posit-dev/posit-sdk-py.git |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch @jonkeane