aboutsummaryrefslogtreecommitdiffstats
path: root/tranches_insight/tranches_insight.py
blob: 13928c7c281f1cbb2d0c18a7ab479bc7975e219c (plain)
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
from flask import Flask, render_template, jsonify, request
import os, csv, yaml
app = Flask(__name__)

basedir = "/home/share/CorpCDOs"
@app.route("/_data")
def get_corr():
    index = request.args.get("i")
    series = request.args.get("s")
    tenor = request.args.get("t")
    data = []
    with open(os.path.join(basedir, "Tranche_data", "Runs",
                           "{0}{1}.{2}.csv".format(index, series, tenor))) as fh:
        csvreader = csv.DictReader(fh)
        for line in csvreader:
            date = line["date"].replace("-","/")
            corrkeys = [k for k in line.keys() if k.endswith("Corr")]
            corrkeys = sorted(corrkeys, key=lambda x: int(x.split(" ")[0]))
            data.append([date] + [line[k] for k in corrkeys])
    return jsonify(labels=["Date"] + corrkeys, data=data)

@app.route("/")
def main():
    with open(os.path.join(basedir, "code", "etc", "runs.yml")) as fh:
        runs = yaml.load(fh)
    index_list = [[name[:2], name[2:], tenor] for name, tenor in zip(runs['name'], runs['tenor'])]
    return render_template("index.html", index_list=index_list)

if __name__ == "__main__":
    app.run(debug=True)