Skip to content
This repository was archived by the owner on Mar 16, 2024. It is now read-only.

Commit f16f413

Browse files
committed
change: optimize image build time in getting-started guide
1 parent ebbc84b commit f16f413

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

docs/flask/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ FROM python:3-slim
22
WORKDIR /app
33
ENV FLASK_APP=app.py
44
ENV FLASK_RUN_HOST=0.0.0.0
5-
COPY . .
5+
COPY requirements.txt .
66
RUN pip install -r requirements.txt
7+
COPY . .
78
EXPOSE 5000
89
CMD ["flask", "run"]

docs/flask/app.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import logging as log
21
import os
32

43
import psycopg2
54
import redis
65
from flask import Flask, render_template_string
76

87
# HTML Jinja2 Template which will be shown in the browser
9-
page_template = '''
8+
page_template = """
109
<div style="margin: auto; text-align: center;">
1110
<h1>{{ welcome_text }}</h1><br>
1211
<h2>This is a change :)</h2>
@@ -17,32 +16,37 @@
1716
{%- endfor %}
1817
</ul>
1918
</div>
20-
'''
19+
"""
2120

2221
# Defining the Flask Web App
2322
app = Flask(__name__)
24-
cache = redis.StrictRedis(host='cache', port=6379)
23+
cache = redis.StrictRedis(host="cache", port=6379)
2524

2625

2726
# The website root will show the page_template rendered with
2827
# - visitor count fetched from Redis Cache
2928
# - list of food fetched from Postgres DB
3029
# - welcome text passed in as environment variable
31-
@app.route('/')
30+
@app.route("/")
3231
def root():
3332
visitors = cache_get_visitor_count()
3433
food = db_get_squirrel_food()
3534

36-
return render_template_string(page_template, visitors=visitors, foods=food, welcome_text=os.getenv("WELCOME", "Hey Acorn user!"))
35+
return render_template_string(
36+
page_template,
37+
visitors=visitors,
38+
foods=food,
39+
welcome_text=os.getenv("WELCOME", "Hey Acorn user!"),
40+
)
3741

3842

3943
# Fetch the squirrel food from the Postgres database
4044
def db_get_squirrel_food():
4145
conn = psycopg2.connect(
4246
host="db",
4347
database="acorn",
44-
user=os.environ['PG_USER'],
45-
password=os.environ['PG_PASS'],
48+
user=os.environ["PG_USER"],
49+
password=os.environ["PG_PASS"],
4650
)
4751

4852
cur = conn.cursor()
@@ -53,4 +57,4 @@ def db_get_squirrel_food():
5357

5458
# Increment the visitor count in the Redis cache and return the new value
5559
def cache_get_visitor_count():
56-
return cache.incr('visitors')
60+
return cache.incr("visitors")

0 commit comments

Comments
 (0)