Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import requests
- from flask import Flask, request, render_template, send_from_directory
- from flask import redirect, url_for
- import psycopg2
- import records
- app = Flask(__name__)
- def getDataFromDB(query: str) -> list:
- try:
- conn = psycopg2.connect(user="myusername", host="knuth.luther.edu", port=5432, dbname="world")
- except:
- raise ConnectionError("Bad stuff")
- cur = conn.cursor()
- cur.execute(query)
- rows = cur.fetchall()
- yield rows
- @app.route('/country', methods = ['GET', 'POST'])
- def country():
- if(request.method == "POST"):
- return render_template('country.html', countryList=getDataFromDB('select name from country'), countryData=getDataFromDB(f'select name, continent, region, capital, surfacearea, population, governmentform, headofstate from country where name = \'{request.form.get("countrySelection")}\''))
- return render_template('country.html', countryList=getDataFromDB('select name from country'))
- @app.route('/region', methods = ['GET', 'POST'])
- def region():
- if(request.method == "POST"):
- return render_template('region.html', regionList=getDataFromDB('select region from country'), regionData=getDataFromDB(f'select name, continent, region, capital, surfacearea, population, governmentform, headofstate from country where region = \'{request.form.get("regionSelection")}\''))
- return render_template('region.html', regionList=getDataFromDB('select region from country'))
- @app.route('/continent', methods = ['GET', 'POST'])
- def continent():
- if(request.method == "POST"):
- return render_template('continent.html', continentList=getDataFromDB('select continent from country'), continentData=getDataFromDB(f'select name, continent, region, capital, surfacearea, population, governmentform, headofstate from country where continent = \'{request.form.get("continentSelection")}\''))
- return render_template('continent.html', continentList=getDataFromDB('select continent from country'))
- @app.route('/')
- def index():
- return render_template('index.html')
- if __name__ == "__main__":
- app.debug = True
- app.run(host="0.0.0.0")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement