from flask import Flask, render_template, url_for
import os

app = Flask(__name__)


# Homepage
@app.route('/')
def index():
    return render_template('index.html')


# Domestic Dashboards
@app.route('/domestic/')
@app.route('/domestic/<model>')
def domestic(model=None):
    return render_template('domestic.html', model=model)


# JSON API (allows users to "download" JSON files)
@app.route('/json/')
def json():
    return str(os.listdir('static/json/'))

@app.route('/json/meta/')
@app.route('/json/meta/<file>')
def json_meta(file=None):
    if file is not None:
        with app.open_resource('static/json/meta/{}'.format(file)) as f:
            return f.read()
    else:
        return str(os.listdir('static/json/meta/'))

@app.route('/json/models/')
@app.route('/json/models/<file>')
def json_models(file=None):
    if file is not None:
        with app.open_resource('static/json/models/{}'.format(file)) as f:
            return f.read()
    else:
        return str(os.listdir('static/json/models/'))

# Trick (converting Python variable to Javascript variable)
# @app.route('/some/route/<url>')
# def convert_variable(url=None):
    # x = some data pull (e.g. SQL query), dict or list
    # if x is not None:
    #     return render_template('page.html', jsonify(x))
    #     return render_template('page.html', x)  # then {{ x | tojson }} in page.html...
    # else:
    #     return render_template('page.html')

from flask import Flask, render_template, url_for
import os

app = Flask(__name__)


# Homepage
@app.route('/')
def index():
    return render_template('index.html')


# Domestic Dashboards
@app.route('/domestic/')
@app.route('/domestic/<model>')
def domestic(model=None):
    return render_template('domestic.html', model=model)


# JSON API (allows users to "download" JSON files)
@app.route('/json/')
def json():
    return str(os.listdir('static/json/'))

@app.route('/json/meta/')
@app.route('/json/meta/<file>')
def json_meta(file=None):
    if file is not None:
        with app.open_resource('static/json/meta/{}'.format(file)) as f:
            return f.read()
    else:
        return str(os.listdir('static/json/meta/'))

@app.route('/json/models/')
@app.route('/json/models/<file>')
def json_models(file=None):
    if file is not None:
        with app.open_resource('static/json/models/{}'.format(file)) as f:
            return f.read()
    else:
        return str(os.listdir('static/json/models/'))

# Trick (converting Python variable to Javascript variable)
# @app.route('/some/route/<url>')
# def convert_variable(url=None):
    # x = some data pull (e.g. SQL query), dict or list
    # if x is not None:
    #     return render_template('page.html', jsonify(x))
    #     return render_template('page.html', x)  # then {{ x | tojson }} in page.html...
    # else:
    #     return render_template('page.html')

# Homepage # Domestic Dashboards # JSON API (allows users to "download" JSON files) # Trick (converting Python variable to Javascript variable) # @app.route('/some/route/<url>') # def convert_variable(url=None): # x = some data pull (e.g. SQL query), dict or list # if x is not None: #     return render_template('page.html', jsonify(x)) #     return render_template('page.html', x)  # then {{ x | tojson }} in page.html... # else: #     return render_template('page.html')