from flask import (Flask, render_template, make_response, url_for, request, redirect, flash, session, send_from_directory, jsonify) from werkzeug.utils import secure_filename app = Flask(__name__) import secrets import cs304dbi as dbi app.secret_key = 'your secret here' # replace that with a random key app.secret_key = secrets.token_hex() # This gets us better error messages for certain request errors app.config['TRAP_BAD_REQUEST_ERRORS'] = True import country_db as db @app.route('/') def index(): # let's skip the first dictionary, which is just header info return render_template('main.html', page_title = 'main page') @app.route('/country-by-id/') def country_by_id(cid): print(f'looking up country with id {cid}') if not cid.isdigit(): flash('cid must be a string of digits') return redirect( url_for('index') ) cid = int(cid) if cid >= len(db.country_tuples): flash(f'cid {cid} was not a valid index') return redirect( url_for('index') ) (id, cname, capital, continent) = db.country_tuples[cid] # to Python console, for debugging print(f'found {cname}') return render_template('country.html', page_title = 'Country Detail', 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', page_title = 'Country Detail', 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', page_title = 'Country Detail', 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', page_title = f'Countries in {continent}', continent=continent, country_list=countries) @app.route('/country-lookup-form/') def country_lookup_form(): return render_template('country-lookup-form.html', page_title = 'Country Lookup Form') @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', page_title = 'Country Detail', c=country_dict) @app.route('/new-country-form/') def new_country_form(): return render_template('new-country-form.html', page_title = 'New Country Form') @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') # You will probably not need the routes below, but they are here # just in case. Please delete them if you are not using them @app.route('/greet/', methods=["GET", "POST"]) def greet(): if request.method == 'GET': return render_template('greet.html', title='Customized Greeting') else: try: username = request.form['username'] # throws error if there's trouble flash('form submission successful') return render_template('greet.html', title='Welcome '+username, name=username) except Exception as err: flash('form submission error'+str(err)) return redirect( url_for('index') ) @app.route('/formecho/', methods=['GET','POST']) def formecho(): if request.method == 'GET': return render_template('form_data.html', method=request.method, form_data=request.args) elif request.method == 'POST': return render_template('form_data.html', method=request.method, form_data=request.form) else: # maybe PUT? return render_template('form_data.html', method=request.method, form_data={}) @app.route('/testform/') def testform(): # these forms go to the formecho route return render_template('testform.html') if __name__ == '__main__': import sys, os if len(sys.argv) > 1: # arg, if any, is the desired port number port = int(sys.argv[1]) assert(port>1024) else: port = os.getuid() # set this local variable to 'wmdb' or your personal or team db db_to_use = 'put_database_name_here_db' print(f'will connect to {db_to_use}') dbi.conf(db_to_use) app.debug = True app.run('0.0.0.0',port)