-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
67 lines (54 loc) · 2.27 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import Flask
from flask import render_template
import socket
import random
import os
import argparse
app = Flask(__name__)
color_codes = {
"red": "#e74c3c",
"green": "#16a085",
"blue": "#2980b9",
"blue2": "#30336b",
"pink": "#be2edd",
"darkblue": "#130f40"
}
SUPPORTED_COLORS = ",".join(color_codes.keys())
# Get color from Environment variable
COLOR_FROM_ENV = os.environ.get('APP_COLOR')
# Generate a random color
COLOR = random.choice(["red", "green", "blue", "blue2", "darkblue", "pink"])
@app.route("/")
def main():
# return 'Hello'
return render_template('hello.html', name=socket.gethostname(), color=color_codes[COLOR])
if __name__ == "__main__":
print(" This is a sample web application that displays a colored background. \n"
" A color can be specified in two ways. \n"
"\n"
" 1. As a command line argument with --color as the argument. Accepts one of " + SUPPORTED_COLORS + " \n"
" 2. As an Environment variable APP_COLOR. Accepts one of " + SUPPORTED_COLORS + " \n"
" 3. If none of the above then a random color is picked from the above list. \n"
" Note: Command line argument precedes over environment variable.\n"
"\n"
"")
# Check for Command Line Parameters for color
parser = argparse.ArgumentParser()
parser.add_argument('--color', required=False)
args = parser.parse_args()
if args.color:
print("Color from command line argument =" + args.color)
COLOR = args.color
if COLOR_FROM_ENV:
print("A color was set through environment variable -" + COLOR_FROM_ENV + ". However, color from command line argument takes precendence.")
elif COLOR_FROM_ENV:
print("No Command line argument. Color from environment variable =" + COLOR_FROM_ENV)
COLOR = COLOR_FROM_ENV
else:
print("No command line argument or environment variable. Picking a Random Color =" + COLOR)
# Check if input color is a supported one
if COLOR not in color_codes:
print("Color not supported. Received '" + COLOR + "' expected one of " + SUPPORTED_COLORS)
exit(1)
# Run Flask Application
app.run(host="0.0.0.0", port=8080)