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: update streamlit example and readmes #78

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions examples/connect/shiny-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## Start the app locally

```bash
export DATABRICKS_HOST="<databricks-sql-warehouse-server-hostname>"
export DATABRICKS_PATH="<databricks-sql-warehouse-http-path>"
shiny run app.py
```

Expand Down
42 changes: 31 additions & 11 deletions examples/connect/streamlit/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
# Streamlit Example

## Start the app locally

```bash
# start streamlit locally
DATABRICKS_TOKEN=<DB_PAT> \
streamlit run ./sample-content.py
export DATABRICKS_HOST="<databricks-sql-warehouse-server-hostname>"
export DATABRICKS_PATH="<databricks-sql-warehouse-http-path>"
streamlit run app.py
```

# deploy the app the first time
publisher deploy -a localhost:3939 -n databricks ./
## Deploy to Posit Connect

# re-deploy the databricks app
publisher redeploy databricks
Validate that `rsconnect-python` is installed:

```bash
rsconnect version
```

TODO: Test this content with databricks-connect
<https://docs.databricks.com/en/dev-tools/databricks-connect/python/index.html>
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 streamlit . \
--server "${CONNECT_SERVER}" \
--api-key "${CONNECT_API_KEY}" \
--environment DATABRICKS_HOST \
--environment DATABRICKS_PATH
```
# install the sdk from this branch
pip install git+https://github.com/posit-dev/posit-sdk-py.git@kegs/databricks-oauth-2

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.

```bash
rsconnect deploy streamlit . \
--server "${CONNECT_SERVER}" \
--api-key "${CONNECT_API_KEY}"
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,27 @@
import streamlit as st
from streamlit.web.server.websocket_headers import _get_websocket_headers

DB_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")

DB_HOST = os.getenv("DB_HOST")
DB_HOST_URL = f"https://{DB_HOST}"
SQL_HTTP_PATH = os.getenv("SQL_HTTP_PATH")
session_token = _get_websocket_headers().get("Posit-Connect-User-Session-Token")

USER_SESSION_TOKEN = None
credentials_provider = viewer_credentials_provider(user_session_token=session_token)

# Read the viewer's user session token from the streamlit ws header.
headers = _get_websocket_headers()
if headers:
USER_SESSION_TOKEN = headers.get("Posit-Connect-User-Session-Token")

credentials_provider = viewer_credentials_provider(
user_session_token=USER_SESSION_TOKEN
cfg = Config(
host=DATABRICKS_HOST_URL, credentials_provider=credentials_provider
)
cfg = Config(host=DB_HOST_URL, credentials_provider=credentials_provider)
# cfg = Config(host=DB_HOST_URL, token=DB_PAT)
Comment on lines -31 to -32
Copy link
Collaborator

Choose a reason for hiding this comment

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

❤️ 🧹 #️⃣

Thank for cleaning this up


databricks_user = CurrentUserAPI(ApiClient(cfg)).me()
st.write(f"Hello, {databricks_user.display_name}!")

with sql.connect(
server_hostname=DB_HOST,
server_hostname=DATABRICKS_HOST,
http_path=SQL_HTTP_PATH,
# access_token=DB_PAT) as connection:
auth_type="databricks-oauth",
credentials_provider=credentials_provider,
) as connection:
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM samples.nyctaxi.trips")
cursor.execute("SELECT * FROM samples.nyctaxi.trips LIMIT 10;")
result = cursor.fetchall()
st.table(pd.DataFrame(result))
56 changes: 2 additions & 54 deletions examples/connect/streamlit/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,56 +1,4 @@
altair==5.2.0
attrs==23.2.0
blinker==1.7.0
cachetools==5.3.2
certifi==2024.2.2
charset-normalizer==3.3.2
click==8.1.7
databricks-sql-connector==3.0.1
databricks-sdk==0.20.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is more a question than an issue or suggestion: what happened with all of these dependencies? Were they old copy 🍝 or something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did a pip freeze initially on this example but I like the approach that @plascaray used in his examples better where we only capture the dependencies that we really need, and then let pip handle transitive deps. No need for us to record them here IMO so I was cleaning this up so that it's consistent with the other examples

Copy link
Collaborator

Choose a reason for hiding this comment

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

💯 Absolutely

Thanks!

databricks-sql-connector==3.1.0
et-xmlfile==1.1.0
gitdb==4.0.11
GitPython==3.1.42
google-auth==2.28.0
idna==3.6
importlib-metadata==7.0.1
Jinja2==3.1.3
jsonschema==4.21.1
jsonschema-specifications==2023.12.1
lz4==4.3.3
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
numpy==1.26.4
oauthlib==3.2.2
openpyxl==3.1.2
packaging==23.2
pandas==2.1.4
pillow==10.2.0
posit-sdk @ git+https://github.com/posit-dev/posit-sdk-py.git@cec7f78a1bdee9e5fd7580011bfa0bcef58cd64b
protobuf==4.25.3
pyarrow==14.0.2
pyasn1==0.5.1
pyasn1-modules==0.3.0
pydeck==0.8.1b0
Pygments==2.17.2
python-dateutil==2.8.2
pytz==2024.1
referencing==0.33.0
requests==2.31.0
rich==13.7.0
rpds-py==0.18.0
rsa==4.9
six==1.16.0
smmap==5.0.1
streamlit==1.31.1
tenacity==8.2.3
thrift==0.16.0
toml==0.10.2
toolz==0.12.1
tornado==6.4
typing_extensions==4.9.0
tzdata==2024.1
tzlocal==5.2
urllib3==2.2.1
validators==0.22.0
zipp==3.17.0
git+https://github.com/posit-dev/posit-sdk-py.git
Loading