'''
    Web sederhana pemetaan kecelakaan
    mirip first news app
'''
# app.py >> konfigurasi untuk server & route ada di sini

import csv
from flask import Flask
from flask import abort
from flask import render_template
app = Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS'] = True

def get_csv():
    csv_path = './static/data-bencana-kecelakaan-transportasi-2011-2014.csv'
    csv_file = open(csv_path, 'r', encoding='unicode_escape')
    csv_obj  = csv.DictReader(csv_file)
    csv_list = list(csv_obj)
    return csv_list

@app.route('/')
def index():
    template = 'index.html'
    object_list = get_csv()
    return render_template(template, object_list = object_list)

@app.route('/<row_id>/')
def detail(row_id):
    template = 'detail.html'
    object_list = get_csv()
    for row in object_list:
        if row['no'] == row_id:
            return render_template(template, object = row)
    abort(404)

if __name__ == '__main__':
    app.run(debug=True, use_reloader=True, host='0.0.0.0')