-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
76 lines (57 loc) · 2.18 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from flask import Flask, render_template, request, redirect, Response
import random, json
import pandas as pd
app = Flask(__name__)
dataframe = pd.read_csv('places_descriptions.csv')
dictionary_from_dataframe = dataframe.to_dict()
dictionary = {}
# Initializes dictionary to contain places and associated descriptions
i = 0
places = dictionary_from_dataframe.get('Place')
while i < len(places):
place = places[i]
description = dictionary_from_dataframe.get('Description')[i]
dictionary[place] = description
i += 1
# Renders the webpage upon loading
@app.route('/')
def renderPage():
return render_template('front.html')
# Handles incoming POST request and dispatches information to functions
@app.route('/post_receiver', methods = ['POST'])
def handlePOSTRequest():
requestByteLiteral = request.get_data()
requestStringJson = requestByteLiteral.decode('utf-8')
jsonObject = json.loads(requestStringJson)
data = jsonObject['information']
functionName = jsonObject['operation']
response = 'There was an error during function dispatch.'
if functionName == 'recommendGivenKeyword':
response = recommendGivenKeyword(data)
elif functionName == 'getPairRandomIndex':
response = getPairRandomIndex(data)
elif functionName == 'getDescriptionGivenPlace':
response = getDescriptionGivenPlace(data)
return response
# Returns array of countries with descriptions containing the given keyword
def recommendGivenKeyword(keyword):
countries = []
for country in dictionary:
if (keyword in dictionary[country]):
countries.append(country)
recommendedArrayString = json.dumps(countries)
return recommendedArrayString
# Given a random index, returns the place:description pair from the dictionary
def getPairRandomIndex(index):
pair = list(dictionary.items())[index]
place = pair[0]
description = pair[1]
pairArrayString = json.dumps([place, description])
return pairArrayString
# For a given place, returns the description
def getDescriptionGivenPlace(place):
description = dictionary.get(place)
return description
# Runs the app
if __name__ == '__main__':
app.run()