from flask import (Flask, url_for, request, render_template) app = Flask(__name__) import country_db as db @app.route('/') def hello_world(): # let's skip the first dictionary, which is just header info return render_template('hello.html', dict_list=db.country_dicts[1:]) @app.route('/country-by-id/') def country_by_id(cid): print(f'looking up country with id {cid}') if not cid.isdigit(): return 'cid must be a string of digits' cid = int(cid) if cid >= len(db.country_tuples): return f'cid {cid} was not a valid index' (id, cname, capital, continent) = db.country_tuples[cid] print(f'found {cname}') return render_template('country.html', cid=id, name=cname, capital=capital, continent=continent) @app.route('/country-by-name-from-tuples/') def country_by_name_from_tuples(cname): (id, cname, capital, continent) = db.country_lookup_by_name_tuple(cname) return render_template('country.html', cid=id, name=cname, capital=capital, continent=continent) @app.route('/country-by-name-from-dicts/') def country_by_name_from_dicts(cname): country_dict = db.country_lookup_by_name_dicts(cname) return render_template('country-dic.html', c=country_dict) @app.route('/continent-countries/') def continent_countries(continent): countries = [ c for c in db.country_dicts if c['continent'] == continent ] return render_template('countries.html', continent=continent, country_list=countries) @app.route('/country-lookup-form/') def country_lookup_form(): return render_template('country-lookup-form.html') @app.route('/country-lookup/') def country_lookup(): cname = request.args.get('cname') country_dict = db.country_lookup_by_name_dicts(cname) return render_template('country-dic.html', c=country_dict) @app.route('/new-country-form/') def new_country_form(): return render_template('new-country-form.html') @app.route('/insert-country/', methods=['POST']) def insert_new_country(): cname = request.form.get('new_cname') capital = request.form.get('new_capital') continent = request.form.get('continent') db.insert_country(cname, capital, continent) return render_template('new-country-form.html') @app.route('/render_styled/') def render_styled(): # has a static CSS file return render_template('trio-styled.html', a='Harry',b='Ron',c='Hermione') if __name__ == '__main__': import os uid = os.getuid() app.debug = True app.run('0.0.0.0',uid)