Info 253: Web Architecture
Kay Ashaolu
webserver.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index_page():
return "<html><head><title>Homepage</title></head>\
<body><h1>This is the home page!</h1></body></html>"
@app.route('/about')
def about_page():
return "<html><head><title>About</title></head>\
<body><h1>This is the about us page!</h1></body></html>"
@app.route('/blog/post-1')
def blog_post():
return "<html><head><title>Blog 1</title></head>\
<body><h1>This is the first blog post</h1></body></html>"
webserver.py
from flask import Flask, render_template
app = Flask(__name__,static_url_path="/static")
@app.route('/')
def hello_world():
return """
<html>
<head>
<title>Homepage</title>
<link rel='stylesheet' href='/static/style.css' />
</head>
<body>
<h1>This is the home page!</h1>
<script type='text/javascript' src='/static/script.js'></script>
</body>
</html>
"""
static/style.css
h1 {
color: blue;
}
static/script.js
alert("script.js has been loaded!");
webserver.js
from flask import Flask, render_template
app = Flask(__name__,static_url_path="/static")
@app.route('/')
def hello_world():
return render_template("index.html")
templates/index.html
<html>
<head>
<title>Homepage</title>
<link rel='stylesheet' href='/static/style.css' />
</head>
<body>
<h1>This is the home page!</h1>
<p>With a template!</p>
<script type='text/javascript' src='/static/script.js'></script>
</body>
</html>
webserver.py (1/2)
from flask import Flask, request, render_template
app = Flask(__name__,static_url_path="/static")
quote_db = {
'sunday': "Life is about making an impact, not making an income. \
–Kevin Kruse",
'monday': "Whatever the mind of man can conceive and believe, it can achieve. \
–Napoleon Hill",
'tuesday': "Strive not to be a success, but rather to be of value. \
–Albert Einstein",
'wednesday': "You miss 100% of the shots you don’t take. \
–Wayne Gretzky",
'thursday': "Every strike brings me closer to the next home run. \
–Babe Ruth",
'friday': "We become what we think about. \
–Earl Nightingale",
'saturday': "Life is what happens to you while you’re busy making other plans. \
–John Lennon",
}
webserver.py (2/2)
@app.route('/')
def quote_of_the_day():
day_of_week = request.args.get('day_of_week')
return render_template(
"index.html",
day=day_of_week,
quote=quote_db[day_of_week])
templates/index.html (1/2)
<html>
<head>
<title>Homepage</title>
<link rel='stylesheet' href='/static/style.css' />
</head>
<body>
<h1>This is the home page!</h1>
<p>The quote for {{day}} is: {{quote}}</p>
<br />
templates/index.html(2/2)
<form action="/" method="GET">
<label for="day_select">Select a day</label>
<select name="day_of_week" id="day_select">
<option value="sunday">Sunday</option>
<option value="monday">Monday</option>
<option value="tuesday">Tuesday</option>
<option value="wednesday">Wednesday</option>
<option value="thursday">Thursday</option>
<option value="friday">Friday</option>
<option value="saturday">Saturday</option>
</select>
<input type="submit" value="Give me a quote" />
</form>
</body>
</html>