How do you deploy a Streamlit app on App Engine (GCP)?
Asked Answered
H

1

5

I am aiming to deploy a simple web-app written with Sreamlit, e.g.

app.py

import streamlit as st
st.title('Hello World!')

I can run this on my local machine by running streamlit run app.py in my command line.

However, I'm not sure how to modify the app.yaml file in GCP's App Engine to deploy this.

Any advice?

Hanky answered 26/11, 2019 at 13:30 Comment(0)
A
11

You can use App Engine flexible environment for that as you can specify a custom runtime. The steps to follow will be:

  1. Create the Dockerfile:

    FROM python:3.7
    EXPOSE 8080
    WORKDIR /app
    COPY requirements.txt ./requirements.txt
    RUN pip3 install -r requirements.txt
    COPY . .
    CMD streamlit run app.py --server.port 8080
    

    I have updated the Dockerfile as App Engine flex requires that the server listens on port 8080.

  2. Create the requirements.txt file with the needed dependencies:

    streamlit
    
  3. Create the app.yaml file:

    runtime: custom
    env: flex
    

    Both the app.yaml and the Dockerfile must be in the same folder. The above app.yaml settings are very basic, more information can be found in the documentation.

Then to deploy you need to use the following gcloud command:gcloud app deploy

Aronarondel answered 26/11, 2019 at 14:11 Comment(6)
Hi there. Quick follow-up; would you do the same with Cloud Run? And is Cloud Run a better version of App Engine in most use cases?Hanky
With the Dockerfile you can deploy to Cloud Run as well, you can see all the needed steps here. Regarding Cloud Run vs App Engine and which is better, both products are Serverless and have their unique features. Cloud Run is based on Knative which is open-source and offers a pay-per-use billing model. It also supports Serverless VPC connectors so you can have the same connectivity to Storage products as when using App Engine Flex.Aronarondel
Also compared to GAE flex, Cloud Run will scale to 0 if there's no load, so that needs to be taken into consideration because you will have warmup requests if all of the serving instances have been shut down after being idle for a certain period of time.Aronarondel
Great answer, Thanks! I have another small inquiry: is there any option to use "standard" env? since using the flex env is much more cumbersomeAscanius
@Ascanius I think you should be able to use it now, as before the limiting factor was the amount of libraries that you could install. With python 3+ you can install whatever library you need, as the docs state. Then you could include gunicorn among the dependencies needed and run it on port 8080 I have not tried it myself but I think it sould work.Aronarondel
does not work for me on GAE flex, I'm getting an error from gcloud app deploy: ERROR: (gcloud.app.deploy) Error Response: [4] An internal error occurred while processing task /app-engine-flex/flex_await_healthy/flex_await_healthy>2022-11-29T07:55:05.850Z21335.wa.2: Your deployment has failed to become healthy in the allotted time and therefore was rolled back. If you believe this was an error, try adjusting the 'app_start_timeout_sec' setting in the 'readiness_check' section.Junkman

© 2022 - 2024 — McMap. All rights reserved.