This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
67 lines (56 loc) · 2.17 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
import os
from os import environ
import shutil
from pyspark.sql import SparkSession
from pyspark.sql.functions import mean
from plotly.offline import plot
import argparse
from flask import Flask, render_template
app = Flask(__name__)
class WineMapGenerator:
def __init__(self):
server = environ.get("SERVER")
user = environ.get("USER")
password = environ.get("PASSWORD")
dbname = environ.get("DBNAME")
self.make(server,
user, dbname, password)
def make(self, server, user, dbname, password):
spark_session = SparkSession.builder.appName('winemap').getOrCreate()
url = "jdbc:postgresql://{0}/{1}?user={2}&password={3}".format(
server, dbname, user, password)
df = spark_session.read.format("jdbc").options(
url=url,
dbtable="wine_reviews",
driver="org.postgresql.Driver").load()
table = (df.select('country', 'points')
.groupBy('country').agg(mean('points'))
.orderBy('avg(points)', ascending=False))
country_cols = table.select('country').collect()
countries = [country[0] for country in country_cols]
point_cols = table.select('avg(points)').collect()
points = [point[0] for point in point_cols]
data = dict(type='choropleth',
locationmode='country names',
locations=countries,
colorscale='Jet',
z=points,
colorbar={'title': 'Average Rating'})
layout = dict(geo={'scope': 'world'})
choromap = dict(data=[data], layout=layout)
plot(choromap, filename='map.html')
def make_template():
# make the templates dir
new_path = '/opt/app-root/src/templates'
if not os.path.exists(new_path):
os.makedirs(new_path)
# move the file to the templates dir
shutil.move('/opt/app-root/src/map.html', new_path)
return render_template("map.html", title='Maps')
@app.route('/')
def index():
WineMapGenerator()
return make_template()
if __name__ == '__main__':
port = int(os.environ.get("PORT", 8080))
app.run(host='0.0.0.0', port=port)