Skip to content

Instantly share code, notes, and snippets.

@berkorbay
Created November 13, 2023 12:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berkorbay/979692a083612bc8741b0cd6d791274c to your computer and use it in GitHub Desktop.
Save berkorbay/979692a083612bc8741b0cd6d791274c to your computer and use it in GitHub Desktop.
Deploying Shiny Python on Cloud Run on GCP with Macbook M1 and later
## Hello world example
## https://shiny.posit.co/py/docs/overview.html
from shiny import App, ui
# Part 1: ui ----
app_ui = ui.page_fluid(
"Hello, world!",
)
# Part 2: server ----
def server(input, output, session):
...
# Combine into a shiny app.
# Note that the variable must be "app".
app = App(app_ui, server)
## Pre-ops
## This document assumes you have full access to GCP, Cloud Run and relevant services
## See here for more https://gist.github.com/berkorbay/9536e54aa18229fa2663ad78b15ae533
## Put app.py and Dockerfile in a folder first and then run the following lines on your terminal
## For Macbook M1 use buildx
## https://stackoverflow.com/a/68766137/3608936
docker buildx build --platform linux/amd64 . -t pyshiny-demo:latest --load
## Suppose your GCP project name is pyshiny-cloudrun-example
docker tag pyshiny-demo:latest gcr.io/pyshiny-cloudrun-example/pyshiny-demo:latest
## Push to GCP container instances
docker push gcr.io/pyshiny-cloudrun-example/pyshiny-demo:latest
## Suppose your Cloud Run service name is pyshinyexample
## --session-afinity is required for sticky sessions
## https://cloud.google.com/run/docs/configuring/session-affinity
gcloud run deploy pyshinyexample --image gcr.io/pyshiny-gcr-example/pyshiny-demo:latest --session-affinity
FROM python:3.10-slim
ENV APP_HOME /app
WORKDIR $APP_HOME
# Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True
ENV PORT 8080
RUN pip install --no-cache-dir --upgrade shiny
# Copy the app
COPY . .
# Run app on port 8080
EXPOSE ${PORT}
CMD exec uvicorn app:app --host 0.0.0.0 --port ${PORT}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment