Advertisement
skyspi007

app.py

Nov 21st, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. import os
  2. import requests
  3. from flask import Flask, request, render_template, send_from_directory
  4. from flask import redirect, url_for
  5. import psycopg2
  6. import records
  7.  
  8. app = Flask(__name__)
  9.  
  10. def getDataFromDB(query: str) -> list:
  11.     try:
  12.         conn = psycopg2.connect(user="myusername", host="knuth.luther.edu", port=5432, dbname="world")
  13.     except:
  14.         raise ConnectionError("Bad stuff")
  15.     cur = conn.cursor()
  16.     cur.execute(query)
  17.     rows = cur.fetchall()
  18.     yield rows
  19.  
  20. @app.route('/country', methods = ['GET', 'POST'])
  21. def country():
  22.     if(request.method == "POST"):
  23.         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")}\''))
  24.     return render_template('country.html', countryList=getDataFromDB('select name from country'))
  25.  
  26. @app.route('/region', methods = ['GET', 'POST'])
  27. def region():
  28.     if(request.method == "POST"):
  29.         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")}\''))
  30.     return render_template('region.html', regionList=getDataFromDB('select region from country'))
  31.  
  32. @app.route('/continent', methods = ['GET', 'POST'])
  33. def continent():
  34.     if(request.method == "POST"):
  35.         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")}\''))
  36.     return render_template('continent.html', continentList=getDataFromDB('select continent from country'))
  37.  
  38. @app.route('/')
  39. def index():
  40.     return render_template('index.html')
  41.  
  42. if __name__ == "__main__":
  43.     app.debug = True
  44.     app.run(host="0.0.0.0")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement