-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
147 lines (131 loc) · 4.67 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
from flask import Flask,render_template,g,request,redirect,url_for
import sqlite3
import json
import calendar
from datetime import datetime,timedelta
app = Flask(__name__)
class DWSCalendar(calendar.Calendar):
def dwsdayscalendar(self, year, month):
"""
Return a matrix representing a month's calendar.
Each row represents a week; days outside this month are zero.
"""
db = get_db()
cur = db.cursor()
cur.execute("""
SELECT cast(strftime('%d', date) as integer) as day, type FROM trip WHERE date >= date(?) AND date < date(?,'+1 month')
""",
(datetime(year,month,1),
datetime(year,month,1),
))
db_days = dict(cur.fetchall())
t = [' ', 'AM', 'PM', 'All Day']
days = list(self.itermonthdays(year, month))
return [ [(day, t[db_days.get(day, 0)]) for day in days[i:i+7]] for i in range(0, len(days), 7) ]
myDWSCalendar = DWSCalendar()
DATABASE = '../tides.db'
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
def query_db(query, args=()):
cur = get_db().execute(query, args)
rv = cur.fetchall()
cur.close()
return rv if rv else None
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html', name=name,)
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/wtf')
def wtf():
return render_template('wtf.html')
@app.route('/')
def dwscalendar():
today = datetime.now()
year, month, date = int(request.args.get('year',today.year)), int(request.args.get('month',today.month)), int(request.args.get('date',today.day))
delta = int(request.args.get('delta', 0))
if delta == -1:
year=year if month>1 else year-1
month=month-1 if month>1 else 12
d = datetime(year, month, 1)
return redirect(url_for('dwscalendar', year=d.year, month=d.month, date=d.day))
elif delta == 1:
year=year if month<12 else year+1
month=month+1 if month<12 else 1
d = datetime(year, month, 1)
return redirect(url_for('dwscalendar', year=d.year, month=d.month, date=d.day))
cal = myDWSCalendar.dwsdayscalendar(year, month)
return render_template('calendar.html',
cal=cal,
year=year,
month=month,
month_name=calendar.month_name[month])
@app.route('/plot')
def plot():
year, month, date = int(request.args.get('year',2018)), int(request.args.get('month',10)), int(request.args.get('date',1))
delta = int(request.args.get('delta', 0))
if delta != 0:
d = datetime(year, month, date) + timedelta(delta)
return redirect(url_for('plot', year=d.year, month=d.month, date=d.day))
d = datetime(year, month, date)
query = """
SELECT * FROM tide WHERE date > date(?) AND date < date(?)
"""
data = query_db(query, [d, (d+timedelta(1))])
xs, ys = [], []
for x, y in data:
xs.append(x)
ys.append(y)
graphs = dict(
data=[
dict(
fill='tozeroy',
x=xs,
y=ys
)
],
layout=dict(
margin=dict(l=47,r=10,b=43,t=20),
xaxis=dict(title='Time',
tickformat='%H:%M',
ticks='outside',
fixedrange=True,
gridcolor='#644536',
gridwidth=1,
),
yaxis=dict(title='Height (m)',
ticks='outside',
fixedrange=True,
gridcolor='#644536'
),
paper_bgcolor= 'rgba(235,236,239,0)',
plot_bgcolor= 'rgba(235,236,239,0)',),
config={
'displayModeBar': False,
'showLink': False,
'responsive': True
}
)
# Convert the figures to JSON
# PlotlyJSONEncoder appropriately converts pandas, datetime, etc
# objects to their JSON equivalents
#graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder)
graphJSON = json.dumps(graphs)
return render_template('plot.html',
year=year,
month=month,
date=d.strftime('%Y-%m-%d'),
graphJSON=graphJSON)
return resp
if __name__ == "__main__":
app.run(debug=True)