1- import logging as log
21import os
32
43import psycopg2
54import redis
65from 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>
1716 {%- endfor %}
1817 </ul>
1918 </div>
20- '''
19+ """
2120
2221# Defining the Flask Web App
2322app = 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 ("/" )
3231def 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
4044def 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
5559def cache_get_visitor_count ():
56- return cache .incr (' visitors' )
60+ return cache .incr (" visitors" )
0 commit comments