initial commit

This commit is contained in:
walkews 2017-05-19 16:02:30 -05:00
parent 95d161f57b
commit dd9a32ca88
12 changed files with 7620 additions and 0 deletions

Binary file not shown.

40
app/app.py Normal file
View file

@ -0,0 +1,40 @@
import os
from flask import Flask, render_template, g, jsonify, request
import pandas as pd
import settings
from random import choice
from flask_wtf import FlaskForm
from wtforms import StringField, SelectField
df = pd.read_csv(os.path.join(settings.data_dir, 'pop_by_country_long_form.csv'))
df['Year'] = df['Year'].str[4:].astype(int)
df2 = df.groupby(['Year', 'Nation']).sum().unstack()
df2.columns = df2.columns.droplevel()
nations = df2.columns
choices = zip(nations, nations)
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secretkey'
class MyForm(FlaskForm):
nation = SelectField('nation', choices=choices)
@app.route('/', methods=['GET', 'POST'])
def index():
global nations
form = MyForm()
return render_template('chart.html', form=form)
@app.route('/data/<string:nation>')
def data(nation):
data = '{' + f'labels:{df2.index.astype(int).tolist()}, series: {df2[nation].astype(float).tolist()}' + '}'
return jsonify({'results':df2[nation].astype(float).tolist(), 'labels':df2.index.astype(float).tolist()})
if __name__ == '__main__':
app.run(debug=True)

25
app/settings.py Normal file
View file

@ -0,0 +1,25 @@
import os
import sys
from matplotlib import rcParams
rcParams['font.family'] = 'Tunga'
try:
root_dir = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
except NameError:
root_dir = os.path.abspath(os.path.dirname(_dh[0]))
except NameError:
root_dir = os.path.abspath(os.path.dirname(os.getcwd))
reports_dir = os.path.join(root_dir, 'reports')
data_dir = os.path.join(root_dir, 'data')
raw_data_dir = os.path.join(root_dir, 'data', 'raw')
processed_data_dir = os.path.join(root_dir, 'data', 'processed')
src_dir = os.path.join(root_dir, 'src')
analysis_dir = os.path.dirname(root_dir)
static_dir = os.path.join(src_dir, 'static')
sys.path.append(analysis_dir)
sys.path.append(root_dir)
sys.path.append(src_dir)

View file

@ -0,0 +1,15 @@
{% macro render_field(field) %}
<div class="form-group">
<label for="{{ field.name }}" class="col-lg-2-control-label"> {{ field.label.text }}</label>
<div class="col-lg-10">
{{ field(class_='form.control;, **kwargs')|safe }}
<ul>
{% for error in field.errors %}
<li style='color:red;' > {{ error }}</li>
{% endfor %}
</ul>
</div>
</div>
{% endmacro %}

72
app/templates/chart.html Normal file
View file

@ -0,0 +1,72 @@
{% from "_render_field.html" import render_field %}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.css">
<script src="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<h1>Population by Nation</h1><br>
data from
<a href="https://data.world/nrippner/population-by-country1980-2010">data.world</a>
<br><br>
Created with Flask and chartist.js
<span class='nation' value='nation'></span>
<fieldset>
<legend>FORM</legend>
{{ render_field(form.nation) }}
<div class="ct-chart ct-perfect-fourth">
</div>
<script>
var mychart;
var nation = $("#nation option:selected").text()
var getData = $.get('/data/'.concat(nation));
getData.done(function(results){
var data = {
labels: results.labels,
series: [
results.results
]
};
mychart = new Chartist.Line('.ct-chart', data);
})
function updateChart(){
var nation = $("#nation option:selected").text()
var updatedData = $.get('/data/'.concat(nation));
updatedData.done(function(results){
var data = {
labels: results.labels,
series: [
results.results
]
};
mychart.update(data)
});
}
$('#update').on('click', updateChart)
$('#nation').on('change', updateChart)
</script>
</body>
</html>