INFO 153B/INFO 253B: Backend 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
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!");
from flask import Flask
import json
app = Flask(__name__)
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",
}
@app.route('/quote/<day_of_week>')
def quote_of_the_day(day_of_week):
response = {"day": day_of_week, "quote": quote_db[day_of_week.lower()]}
return json.dumps(response)