Misc cleanup

This commit is contained in:
Matthew Dillon 2015-09-02 18:21:57 -07:00
parent b394924aa8
commit b4ab8cbd1e
11 changed files with 85 additions and 80 deletions

View file

@ -1,5 +1,6 @@
from flask import Flask from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.sqlalchemy import SQLAlchemy
from config import config from config import config
@ -17,4 +18,3 @@ def create_app(config_name):
app.register_blueprint(main_blueprint) app.register_blueprint(main_blueprint)
return app return app

View file

@ -1,7 +1,7 @@
from flask import Blueprint from flask import Blueprint
main = Blueprint('main', __name__) main = Blueprint('main', __name__, template_folder='templates')
from . import views from . import views

View file

@ -2,9 +2,10 @@ from flask_wtf import Form
from wtforms import IntegerField from wtforms import IntegerField
from wtforms.validators import NumberRange, Required from wtforms.validators import NumberRange, Required
from wtforms.ext.sqlalchemy.fields import QuerySelectField from wtforms.ext.sqlalchemy.fields import QuerySelectField
from app.models import Community, Dataset, Temperature
from sqlalchemy import func from sqlalchemy import func
from .models import Community, Dataset, Temperature
class AKIYearField(IntegerField): class AKIYearField(IntegerField):
def pre_validate(self, form): def pre_validate(self, form):

View file

@ -1,4 +1,4 @@
from . import db from app import db
from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.ext.hybrid import hybrid_property

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title>{{config['TITLE']}}</title> <title>{{ config['TITLE'] }}</title>
<link href="/static/css/bootstrap-readable.min.css" rel="stylesheet"> <link href="/static/css/bootstrap-readable.min.css" rel="stylesheet">
<link href="/static/css/akindices.css" rel="stylesheet"> <link href="/static/css/akindices.css" rel="stylesheet">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
@ -14,16 +14,16 @@
</script> </script>
<div class="container" style="max-width:90%"> <div class="container" style="max-width:90%">
<div class="page-header"> <div class="page-header">
<h1>{{title}}<br><small>alaska climate data</small></h1> <h1>{{ config['TITLE'] }}<br><small>alaska climate data</small></h1>
</div> </div>
{% block content %}{% endblock %} {% block content %}{% endblock %}
<hr> <hr>
<div id="footer"> <div id="footer">
<p class="text-muted credit"> <p class="text-muted credit">
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/80x15.png" /></a> This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License</a>. <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/80x15.png" /></a> This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License</a>.
<br> <br>
<small>Created by <a href="mailto:mrdillon@alaska.edu">Matthew Dillon</a>, 2015.</small> <small>Created by <a href="mailto:mrdillon@alaska.edu">Matthew Dillon</a>, {{ config['COPYRIGHT_YEAR'] }}.</small>
</p> </p>
</div> </div>
</div> </div>

View file

@ -1,4 +1,4 @@
{% extends "base.html" %} {% extends "main/base.html" %}
{% block content %} {% block content %}
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5/leaflet.css" /> <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5/leaflet.css" />
@ -31,27 +31,25 @@
</thead> </thead>
<tbody> <tbody>
{% for temp in temps %} {% for temp in temps %}
<tr> <tr>
<td>{{ temp[0]|int }}</td> <td>{{ temp[0]|int }}</td>
<td>{{ temp[1]|round(2) }}</td> <td>{{ temp[1]|round(2) }}</td>
<td>{{ temp[2]|round(2) }}</td> <td>{{ temp[2]|round(2) }}</td>
<td>{{ temp[3]|round(2) }}</td> <td>{{ temp[3]|round(2) }}</td>
<td>{{ temp[4]|round(2) }}</td> <td>{{ temp[4]|round(2) }}</td>
<td>{{ temp[5]|round(2) }}</td> <td>{{ temp[5]|round(2) }}</td>
<td>{{ temp[6]|round(2) }}</td> <td>{{ temp[6]|round(2) }}</td>
<td>{{ temp[7]|round(2) }}</td> <td>{{ temp[7]|round(2) }}</td>
<td>{{ temp[8]|round(2) }}</td> <td>{{ temp[8]|round(2) }}</td>
<td>{{ temp[9]|round(2) }}</td> <td>{{ temp[9]|round(2) }}</td>
<td>{{ temp[10]|round(2) }}</td> <td>{{ temp[10]|round(2) }}</td>
<td>{{ temp[11]|round(2) }}</td> <td>{{ temp[11]|round(2) }}</td>
<td>{{ temp[12]|round(2) }}</td> <td>{{ temp[12]|round(2) }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
<script> <script>
var map = L.map('map').setView([{{ lat }}, {{ lon }}], 5); var map = L.map('map').setView([{{ lat }}, {{ lon }}], 5);
L.tileLayer('http://otile1.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg', { L.tileLayer('http://otile1.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg', {

View file

@ -1,5 +1,5 @@
{% extends "base.html" %} {% extends "main/base.html" %}
{% from "_formhelpers.html" import render_field %} {% from "main/_formhelpers.html" import render_field %}
{% block content %} {% block content %}
<p class="lead" align="justify"> <p class="lead" align="justify">
@ -46,7 +46,7 @@
<div class="col-md-12"> <div class="col-md-12">
<div class="form-group col-md-4"> <div class="form-group col-md-4">
<input type="submit" name="submit" class="btn btn-primary form-control" <input type="submit" name="submit" class="btn btn-primary form-control"
value="Get Temperatures" > value="Get Temperatures" />
</div> </div>
<div class="form-group col-md-4"> <div class="form-group col-md-4">
<input type="button" name="reset" class="btn btn-danger form-control" <input type="button" name="reset" class="btn btn-danger form-control"
@ -112,11 +112,6 @@
</td> </td>
</tr> </tr>
{% endif %} {% endif %}
{#
<tr>
<td colspan="9">&nbsp;</td>
</tr>
#}
<tr class="active"> <tr class="active">
<td colspan="9" align="center"> <td colspan="9" align="center">
Saved Searches Saved Searches

View file

@ -1,22 +1,22 @@
import numpy import numpy
from app.models import Temperature, Dataset from .models import Temperature, Dataset
def getTemps(datasets, community_id, minyear, maxyear): def getTemps(datasets, community_id, minyear, maxyear):
temps = Temperature.query.join(Dataset). \ temps = Temperature.query.join(Dataset). \
filter(Dataset.id == Temperature.dataset_id, filter(Dataset.id == Temperature.dataset_id,
Dataset.id == datasets, Dataset.id == datasets,
Temperature.community_id == community_id, Temperature.community_id == community_id,
Temperature.year >= minyear, Temperature.year >= minyear,
Temperature.year <= maxyear) Temperature.year <= maxyear)
length = int(maxyear) - int(minyear) length = int(maxyear) - int(minyear)
temps_arr = numpy.zeros((length+1, 12)) temps_arr = numpy.zeros((length+1, 12))
i = 0 i = 0
for t in temps.all(): for t in temps.all():
temps_arr[i,:] = [t.january, t.february, t.march, temps_arr[i, :] = [t.january, t.february, t.march,
t.april, t.may, t.june, t.april, t.may, t.june,
t.july, t.august, t.september, t.july, t.august, t.september,
t.october, t.november, t.december] t.october, t.november, t.december]
@ -58,8 +58,8 @@ def avg_air_indices(indices):
def des_air_indices(indices): def des_air_indices(indices):
if indices.shape[0] > 2: if indices.shape[0] > 2:
ati = numpy.sort(indices[:,0]) ati = numpy.sort(indices[:, 0])
afi = numpy.sort(indices[:,1]) afi = numpy.sort(indices[:, 1])
dti = (ati[-1] + ati[-2] + ati[-3]) / 3.0 dti = (ati[-1] + ati[-2] + ati[-3]) / 3.0
dfi = (afi[0] + afi[1] + afi[2]) / 3.0 dfi = (afi[0] + afi[1] + afi[2]) / 3.0
return (int(dti), int(dfi)) return (int(dti), int(dfi))

View file

@ -1,11 +1,12 @@
from numpy import arange, hstack from numpy import arange, hstack
from flask import session, render_template, request, redirect, current_app from flask import session, render_template, request, redirect, url_for
from . import main from . import main
from .forms import AKIForm from .forms import AKIForm
from .utils import getTemps, avg_air_temp, ann_air_indices, avg_air_indices, des_air_indices from .utils import getTemps, avg_air_temp, ann_air_indices, \
from app.models import Community, Dataset, Temperature avg_air_indices, des_air_indices
from .models import Community, Dataset
@main.route('/', methods=['GET']) @main.route('/', methods=['GET'])
@ -21,24 +22,27 @@ def index():
if all(key in session for key in ('minyear', 'maxyear', 'datasets')): if all(key in session for key in ('minyear', 'maxyear', 'datasets')):
community = Community.query.get_or_404(community_id) community = Community.query.get_or_404(community_id)
# TODO: clean this up session['community_data'] = {
session['community_data'] = dict() 'id': community_id,
session['community_data']['id'] = community_id 'name': community.name,
session['community_data']['name'] = community.name 'latitude': round(community.latitude, 5),
session['community_data']['latitude'] = round(community.latitude, 5) 'longitude': round(community.longitude, 5),
session['community_data']['longitude'] = round(community.longitude, 5) }
session['ds_name'] = Dataset.query. \ session['ds_name'] = Dataset.query. \
with_entities(Dataset.modelname, Dataset.scenario). \ with_entities(Dataset.modelname, Dataset.scenario). \
filter_by(id=session['datasets']).all() filter_by(id=session['datasets']).all()
temps_arr = getTemps(session['datasets'], community_id, session['minyear'], session['maxyear']) temps_arr = getTemps(session['datasets'],
community_id,
session['minyear'],
session['maxyear'])
session['avg_temp'] = avg_air_temp(temps_arr) session['avg_temp'] = avg_air_temp(temps_arr)
indices = ann_air_indices(temps_arr) indices = ann_air_indices(temps_arr)
session['avg_indices'] = avg_air_indices(indices) session['avg_indices'] = avg_air_indices(indices)
session['des_indices'] = des_air_indices(indices) session['des_indices'] = des_air_indices(indices)
return render_template("index.html", form=form) return render_template("main/index.html", form=form)
@main.route('/', methods=['POST']) @main.route('/', methods=['POST'])
@ -52,20 +56,20 @@ def index_submit():
session['maxyear'] = session['minyear'] session['maxyear'] = session['minyear']
session['datasets'] = request.form['model'] session['datasets'] = request.form['model']
return redirect('/') return redirect(url_for('main.index'))
else: else:
return render_template("index.html", form=form) return render_template("main/index.html", form=form)
@main.route('/datatypes') @main.route('/datatypes')
def datatypes(): def datatypes():
return render_template("datatypes.html") return render_template("main/datatypes.html")
@main.route('/reset') @main.route('/reset')
def reset(): def reset():
session.clear() session.clear()
return redirect('/') return redirect(url_for('main.index'))
@main.route('/details') @main.route('/details')
@ -75,40 +79,43 @@ def details():
minyear = request.args.get('minyear', '') minyear = request.args.get('minyear', '')
maxyear = request.args.get('maxyear', '') maxyear = request.args.get('maxyear', '')
temps = getTemps(datasets, community_id, minyear, maxyear) temps = getTemps(datasets, community_id, minyear, maxyear)
years = arange(int(minyear), int(maxyear)+1).reshape(int(maxyear)-int(minyear) + 1, 1) years = arange(int(minyear),
int(maxyear)+1).reshape(int(maxyear)-int(minyear) + 1, 1)
temps = hstack((years, temps)) temps = hstack((years, temps))
return render_template("details.html", return render_template("main/details.html",
lat=request.args.get('lat', ''), lat=request.args.get('lat', ''),
lon=request.args.get('lon', ''), lon=request.args.get('lon', ''),
community_name=request.args.get('name', ''), community_name=request.args.get('name', ''),
temps=temps) temps=temps)
@main.route('/save') @main.route('/save')
def save(): def save():
if 'save' in session: if 'save' in session:
i = len(session['save']) i = str(len(session['save']))
save = session['save'] save = session['save']
else: else:
save = dict() save = dict()
i = 0 i = '0'
save[i] = {
'datasets': session['datasets'],
'ds_name': session['ds_name'],
'community_data': session['community_data'],
'minyear': session['minyear'],
'maxyear': session['maxyear'],
'avg_temp': session['avg_temp'],
'avg_indices': session['avg_indices'],
'des_indices': session['des_indices'],
}
save[i] = dict()
save[i]['datasets'] = session['datasets']
save[i]['ds_name'] = session['ds_name']
save[i]['community_data'] = session['community_data']
save[i]['minyear'] = session['minyear']
save[i]['maxyear'] = session['maxyear']
save[i]['avg_temp'] = session['avg_temp']
save[i]['avg_indices'] = session['avg_indices']
save[i]['des_indices'] = session['des_indices']
session.clear() session.clear()
session['save'] = save session['save'] = save
return redirect('/') return redirect(url_for('main.index'))
@main.route('/delete') @main.route('/delete')
def delete(): def delete():
record = request.args.get('record', '') record = request.args.get('record', '')
session['save'].pop(record) session['save'].pop(record)
return redirect('/') return redirect(url_for('main.index'))

View file

@ -1,11 +1,14 @@
import os import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config: class Config:
SECRET_KEY = os.environ.get('SECRET_KEY') SECRET_KEY = os.environ.get('SECRET_KEY')
APP_NAME = 'AKIndices' APP_NAME = 'AKIndices'
APP_VERSION = '0.2.0' APP_VERSION = '0.2.0'
TITLE = 'AKIndices'
COPYRIGHT_YEAR = 2015
CSRF_ENABLED = True
DEBUG = False
@staticmethod @staticmethod
def init_app(app): def init_app(app):
@ -14,9 +17,10 @@ class Config:
class DevelopmentConfig(Config): class DevelopmentConfig(Config):
DEBUG = True DEBUG = True
TITLE = 'AKIndices (test)'
SECRET_KEY = os.environ.get('SECRET_KEY') or 'top secret' SECRET_KEY = os.environ.get('SECRET_KEY') or 'top secret'
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \ SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
'postgres://postgres@localhost/akindices' 'postgres://postgres@localhost/akindices'
config = { config = {