Shiny for Python + Google Cloud Run
If you are just looking for a github repo you can find it here
The Plan
The plan here is to create a simple shiny app, but we are using python and we want to host it on Google Cloud Platform (GCP). First we are going to build a simple shiny for python example site. Second we are going to need to serve this site some how. Third we will need to containerize the operation. Finally we need to setup continous deployment with Google Cloud Run and Google Cloud Build.
Prerequisites
- Docker Desktop installed
- A github repo
- A GCP Instance
- The Cloud Run API enabled
Step 1
This is not going to be a fancy app we are hoping to create with shiny. We just want to prove we can sucessfully build something locally and deploy it with Google Cloud Run. This means we are going to copy and paste the code from
here . Place this code in a file called "app.py". This app should work out of the box according to the website code.
Step 2
The next step is going to be finding a way to serve this site from your local machine. Shiny for python actually works with
uvicorn. This means that we can use the same commands here that we would there.
uvicorn app:app --host 0.0.0.0 --port 8080 should start your local server and allow you to view your app on localhost:8080
Step 3
Now we must containerize this entire operation. You can head over to my
github to download the needed docker file. The comments there should tell you how everything works and what each command does. Once you are sure that the container behaves as you expect it locally you can move on to the next step.
Step 4
Finally you can head over to your GCP instance and go to cloud run and create a new service. On the first selection choose
Continuously deploy new revisions from a source repository This will allow you to select the SET UP WITH CLOUD BUILD option. Once you click that you should be able to choose your repo provider (Github or Bitbucket). Once you authenticate you can select your repo. You then need to select the production branch of your repo. The default from Github is
^main$. If you have a different production branch replace the text. Next select Dockerfile as the build type. Leave everything else as it is except for the Authentication section where you need to
Allow unauthenticated invocations . After that hit create.