-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
138 lines (109 loc) · 3.43 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Import necessary libraries
from flask import Flask, jsonify, render_template
import pymongo
import os
# Create instance of Flask app
app = Flask(__name__)
# Setup connection to MongoDB database remotely on MongoDB Atlas or locally
conn = os.environ.get('DATABASE_URL', '') or 'mongodb://localhost:27017'
# Initialize PyMongo to work with MongoDBs.
client = pymongo.MongoClient(conn)
# connect to mongo db and collections
db = client.animal_visual_db
vba_fauna = db.vba_fauna
scraped_fauna = db.scraped_fauna
# Create route that renders index.html template
@app.route("/")
def index():
return render_template("index.html")
# Create route that renders dashboard.html template
@app.route("/dashboard")
def dashboard():
return render_template("dashboard.html")
# Create route that renders datapage.html template
@app.route("/data")
def datapage():
return render_template("datapage.html")
# Add api route to get VBA fauna json
@app.route("/api/v1.0/vbafauna")
def vbafauna():
# Convert all documents in the vba_fauna collection to a list
records = list(vba_fauna.find())
# Convert mongo id object to string
for record in records:
record["_id"] = str(record["_id"])
# Return the json representation of the records
return jsonify(records)
# Add api route to get the scraped fauna json
@app.route("/api/v1.0/scrapedfauna")
def scrapedfauna():
# Convert all documents in the scraped_fauna collection to a list
species = list(scraped_fauna.find())
# Convert mongo id object to string
for specie in species:
specie["_id"] = str(specie["_id"])
# Return the json representation of the species
return jsonify(species)
# Add api route to remove unnecessary fields for displaying table
@app.route("/api/v1.0/table")
def table():
data_table = list(vba_fauna.aggregate([{ "$unset": ["_id"] }]))
return jsonify(data_table)
# Add api route to get the vba fauna data aggregated by animal names
@app.route("/api/v1.0/aggregation")
def aggregation():
# Aggregate total sightings by each animal (represented in common names,
# science names, taxon ids and taxon types) over 5 years
metadata = list(
vba_fauna.aggregate(
[
{
"$group" : {
"_id" :"$comm_name",
"scientific_name": { "$first": "$sci_name" },
"taxon_id": { "$first": "$taxon_id" },
"taxon_type": { "$first": "$taxon_type" },
"total_sightings": { "$sum": "$totalcount" },
}
}
])
)
# Aggregate records by animal name
records_by_animal = list(
vba_fauna.aggregate(
[
{
"$group" : {
"_id" : "$comm_name",
"record_id": { "$push": "$record_id" },
"survey_id": { "$push": "$survey_id" },
"number_sightings": { "$push": "$totalcount" },
"long": { "$push": "$long" },
"lat": { "$push": "$lat" },
"start_date": { "$push": "$start_date" }
}
}
])
)
# Aggregrate total sightings by animal by month
sightings_by_month = list(
vba_fauna.aggregate([
{
"$group": {
"_id": {
"animal_name": "$comm_name",
"year_month": "$year_month"
},
"total_sightings": { "$sum": "$totalcount" }
}
}
]))
# Add an aggregation dictionary
aggregation_dict = {
"metadata": metadata,
"records": records_by_animal,
"sightings_by_month": sightings_by_month
}
return jsonify(aggregation_dict)
if __name__ == "__main__":
app.run(debug=True)