Skip to content

Commit 94e1e5e

Browse files
feat: add helloworld samples for FastAPI and Streamlit
1 parent 495a64b commit 94e1e5e

10 files changed

Lines changed: 230 additions & 0 deletions

File tree

run/helloworld-fastapi/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Cloud Run Hello World FastAPI Sample
2+
3+
This sample shows how to deploy a Hello World FastAPI application to Cloud Run.
4+
5+
[![Run in Google Cloud][run_img]][run_link]
6+
7+
[run_img]: https://storage.googleapis.com/cloudrun/button.svg
8+
[run_link]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&cloudshell_working_dir=run/helloworld-fastapi
9+
10+
## Build
11+
12+
* Set an environment variable with your GCP Project ID:
13+
14+
```
15+
export GOOGLE_CLOUD_PROJECT=<PROJECT_ID>
16+
```
17+
18+
* Use a [Buildpack](https://github.com/GoogleCloudPlatform/buildpacks) to build the container:
19+
20+
```sh
21+
gcloud builds submit --pack image=gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-fastapi
22+
```
23+
24+
## Run Locally
25+
26+
```sh
27+
docker run --rm gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-fastapi
28+
```
29+
30+
## Test
31+
32+
```
33+
pytest
34+
```
35+
36+
_Note: you may need to install `pytest` using `pip install pytest`._
37+
38+
## Deploy
39+
40+
```sh
41+
# Set an environment variable with your GCP Project ID
42+
export GOOGLE_CLOUD_PROJECT=<PROJECT_ID>
43+
44+
# Deploy to Cloud Run
45+
gcloud run deploy helloworld-fastapi --source .
46+
```
47+
48+
49+
For more details on how to work with this sample read the [Python Cloud Run Samples README](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/run)

run/helloworld-fastapi/main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START cloudrun_helloworld_fastapi]
16+
from fastapi import FastAPI
17+
18+
app = FastAPI()
19+
20+
21+
@app.get("/")
22+
def hello(name: str = "World"):
23+
"""Return a friendly HTTP greeting."""
24+
return {
25+
"message": f"Hello {name}!"
26+
}
27+
# [END cloudrun_helloworld_fastapi]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
from fastapi.testclient import TestClient
17+
18+
import main
19+
20+
21+
@pytest.fixture
22+
def client():
23+
return TestClient(main.app)
24+
25+
26+
def test_handler_no_param(client):
27+
r = client.get("/")
28+
29+
assert r.json() == {"message": "Hello World!"}
30+
assert r.status_code == 200
31+
32+
33+
def test_handler_with_param(client):
34+
r = client.get("/", params={"name": "Foo"})
35+
36+
assert r.json() == {"message": "Hello Foo!"}
37+
assert r.status_code == 200
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==8.2.0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fastapi[standard]==0.116.1
2+
uvicorn==0.35.0

run/helloworld-streamlit/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Cloud Run Hello World Streamlit Sample
2+
3+
This sample shows how to deploy a Hello World Streamlit application to Cloud Run.
4+
5+
[![Run in Google Cloud][run_img]][run_link]
6+
7+
[run_img]: https://storage.googleapis.com/cloudrun/button.svg
8+
[run_link]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&cloudshell_working_dir=run/helloworld-streamlit
9+
10+
## Build
11+
12+
* Set an environment variable with your GCP Project ID:
13+
14+
```
15+
export GOOGLE_CLOUD_PROJECT=<PROJECT_ID>
16+
```
17+
18+
* Use a [Buildpack](https://github.com/GoogleCloudPlatform/buildpacks) to build the container:
19+
20+
```sh
21+
gcloud builds submit --pack image=gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-streamlit
22+
```
23+
24+
## Run Locally
25+
26+
```sh
27+
docker run --rm gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-streamlit
28+
```
29+
30+
## Test
31+
32+
```
33+
pytest
34+
```
35+
36+
_Note: you may need to install `pytest` using `pip install pytest`._
37+
38+
## Deploy
39+
40+
```sh
41+
# Set an environment variable with your GCP Project ID
42+
export GOOGLE_CLOUD_PROJECT=<PROJECT_ID>
43+
44+
# Deploy to Cloud Run
45+
gcloud run deploy helloworld-streamlit --source .
46+
```
47+
48+
49+
For more details on how to work with this sample read the [Python Cloud Run Samples README](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/run)

run/helloworld-streamlit/main.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START cloudrun_helloworld_streamlit]
16+
import streamlit as st
17+
18+
st.title(f"Hello World! 👋🌎")
19+
st.markdown(
20+
"""
21+
This is a demo Streamlit app.
22+
23+
Enter your name in the text box below and press a button to see some fun features in Streamlit.
24+
"""
25+
)
26+
27+
name = st.text_input("Enter your name:")
28+
29+
# Use columns to create buttons side by side
30+
col1, col2 = st.columns(2)
31+
32+
with col1:
33+
if st.button("Send balloons! 🎈"):
34+
st.balloons()
35+
st.write(f"Time to celebrate {name}! 🥳")
36+
st.write("You deployed a Streamlit app! 👏")
37+
38+
with col2:
39+
if st.button("Send snow! ❄️"):
40+
st.snow()
41+
st.write(f"Let it snow {name}! 🌨️")
42+
st.write("You deployed a Streamlit app! 👏")
43+
# [END cloudrun_helloworld_streamlit]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from streamlit.testing.v1 import AppTest
2+
3+
def test_balloons():
4+
"""A user presses the balloons button"""
5+
at = AppTest.from_file("main.py").run()
6+
at.text_input[0].set_value("Foo").run()
7+
at.button[0].click().run()
8+
assert at.markdown.values[0] == "This is a demo Streamlit app.\n\nEnter your name in the text box below and press a button to see some fun features in Streamlit."
9+
assert at.markdown.values[1] == "Time to celebrate Foo! 🥳"
10+
assert at.markdown.values[2] == "You deployed a Streamlit app! 👏"
11+
12+
def test_snow():
13+
"""A user presses the snow button"""
14+
at = AppTest.from_file("main.py").run()
15+
at.text_input[0].set_value("Foo").run()
16+
at.button[1].click().run()
17+
assert at.markdown.values[0] == "This is a demo Streamlit app.\n\nEnter your name in the text box below and press a button to see some fun features in Streamlit."
18+
assert at.markdown.values[1] == "Let it snow Foo! 🌨️"
19+
assert at.markdown.values[2] == "You deployed a Streamlit app! 👏"
20+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==8.2.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
streamlit==1.47.1

0 commit comments

Comments
 (0)