-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
147 lines (108 loc) · 3.42 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
139
140
141
142
143
144
145
146
147
''' IMPORTS '''
import pandas as pd
import numpy as np
import math
from flask import Flask, request, render_template, redirect, url_for, jsonify
import os
import gspread
'''
Get photo links for drive
'''
def edit_photo_links(df):
links = df['Photo']
for i in range(len(links)):
dump, id = links[i].split('id=')
new_link = 'https://drive.google.com/uc?export=view&id=' + id
links[i] = new_link
df['Photo'] = links
return df
'''
Init flask app
'''
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
sa = gspread.service_account(filename="humans-of-align.json")
sheet = sa.open("Render")
'''
READ FORM RESPONSES
'''
worksheet = sheet.worksheet("Sheet1")
records = worksheet.get_all_records()
'''
Landing page
'''
@app.route('/', methods=['GET', 'POST'])
def index():
# Read google sheet
sheet = sa.open("Render")
worksheet = sheet.worksheet("Sheet1")
records = worksheet.get_all_records()
# Read dataframe
df = pd.DataFrame.from_records(records)
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
df = df.sort_values(by='Timestamp', ascending=False)
# print(df)
# Get links to photos
df = edit_photo_links(df)
# Pick recent 5 for carousal
recent_five = df.head(5)
recent_five_dict = recent_five.to_dict('list')
# print(recent_five_dict)
return render_template("index.html", recent_five=recent_five_dict)
'''
List of students page
'''
@app.route('/stories', methods=['GET', 'POST'])
def stories():
# Read form responses
sheet = sa.open("Render")
worksheet = sheet.worksheet("Sheet1")
records = worksheet.get_all_records()
# Read dataframe
df = pd.DataFrame.from_records(records)
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
df = df.sort_values(by='Timestamp', ascending=False)
# print(df)
# Get links to photos
df = edit_photo_links(df)
students_details_dict = df.to_dict('list')
rows = math.ceil(len(df['First Name'])/3)
total = len(df['First Name'])
start = 0
index_list = []
for i in range(rows):
temp_list = []
for j in range(3):
if (start < total):
temp_list.append(start)
start += 1
index_list.append(temp_list)
index_list_normal = []
for i in range(total):
index_list_normal.append(i)
return render_template("students.html", students_details=students_details_dict, index_list=index_list, index_list_normal=index_list_normal)
'''
Individual stories page
'''
@app.route('/stories/<name>', methods=['GET', 'POST'])
def students_name(name):
# Read form responses
sheet = sa.open("Render")
worksheet = sheet.worksheet("Sheet1")
records = worksheet.get_all_records()
# Read dataframe
df = pd.DataFrame.from_records(records)
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
df = df.sort_values(by='Timestamp', ascending=False)
# print(df)
# Get links to photos
df = edit_photo_links(df)
# Get student details
first_name, last_name = name.split('-')
student_details = df.loc[(df['First Name'] == first_name) & (
df['Last Name'] == last_name)]
student_details_dict = student_details.to_dict('list')
return render_template("individual_story.html", student_details=student_details_dict)
if __name__ == "__main__":
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.run(host='0.0.0.0', port=5000, debug=True)