<commit_before>from . import cache
from .comparison import *
from .filesystem import load_detailed_log
from flask import Flask, render_template, request, abort
from json2html import *
import os


static = os.path.join(os.path.dirname(__file__), "web", "static")
templates = os.path.join(os.path.dirname(__file__), "web", "templates")
app = Flask("oc", static_folder=static, template_folder=templates)


@app.route("/compare")
def compare():
    if not (cache.given and cache.run):
        raise ValueError("Must populate given reference and run caches first")

    add_urls_if_needed(request.url)
    calculate_similarities()
    kwargs = {
        "hv_production": skipped_high_voltage_production_mixes(),
        "missing_given": missing_given(),
        "missing_model": missing_model(),
        "in_both": in_both(),
        "similarity": cache.similarity,
    }
    return render_template("index.html", **kwargs)

@app.route("/show")
def show():
    if not cache.run:
        raise ValueError("Must populate run cache first")

    add_urls_if_needed(request.url)
    return render_template("show.html", data=cache.run.values())

@app.route("/detail/<name>/<product>/<location>/")
def detail(name, product, location):
    try:
        model = cache.run[(name, product, location)]
        given = cache.given.get((name, product, location))
    except KeyError:
        abort(404)
    similarity = similarity_index(model, given) if given else None
    exchanges = compare_exchanges(model, given) if given else None
    return render_template('detail.html', given=given, model=model, exchanges=exchanges, similarity=similarity)

@app.route("/log/<name>/<product>/<location>/")
def log_detail(name, product, location):
    try:
        ds = cache.run[(name, product, location)]
    except KeyError:
        abort(404)
    filename = os.path.basename(ds['filepath'])
    messages = (line for line in load_detailed_log(cache.run_id)
                if line['dataset']['filename'] == filename)

    return render_template('log_detail.html', ds=ds, messages=messages)

@app.route("/model-raw/<name>/<product>/<location>/")
def model_raw(name, product, location):
    try:
        ds = cache.run[(name, product, location)]
    except KeyError:
        abort(404)

@app.route("/given-raw/<name>/<product>/<location>/")
def given_raw(name, product, location):
    try:
        ds = cache.given[(name, product, location)]
    except KeyError:
        abort(404)<commit_msg>Sort raw outputs for HTML tables<commit_after>from . import cache
from .comparison import *
from .filesystem import load_detailed_log
from flask import Flask, render_template, request, abort
from json2html import *
import os


static = os.path.join(os.path.dirname(__file__), "web", "static")
templates = os.path.join(os.path.dirname(__file__), "web", "templates")
app = Flask("oc", static_folder=static, template_folder=templates)


def order_dict(dictionary):
    # https://stackoverflow.com/questions/22721579/sorting-a-nested-ordereddict-by-key-recursively
    return {k: order_dict(v) if isinstance(v, dict) else v
            for k, v in sorted(dictionary.items())}


@app.route("/compare")
def compare():
    if not (cache.given and cache.run):
        raise ValueError("Must populate given reference and run caches first")

    add_urls_if_needed(request.url)
    calculate_similarities()
    kwargs = {
        "hv_production": skipped_high_voltage_production_mixes(),
        "missing_given": missing_given(),
        "missing_model": missing_model(),
        "in_both": in_both(),
        "similarity": cache.similarity,
    }
    return render_template("index.html", **kwargs)

@app.route("/show")
def show():
    if not cache.run:
        raise ValueError("Must populate run cache first")

    add_urls_if_needed(request.url)
    return render_template("show.html", data=cache.run.values())

@app.route("/detail/<name>/<product>/<location>/")
def detail(name, product, location):
    try:
        model = cache.run[(name, product, location)]
        given = cache.given.get((name, product, location))
    except KeyError:
        abort(404)
    similarity = similarity_index(model, given) if given else None
    exchanges = compare_exchanges(model, given) if given else None
    return render_template('detail.html', given=given, model=model, exchanges=exchanges, similarity=similarity)

@app.route("/log/<name>/<product>/<location>/")
def log_detail(name, product, location):
    try:
        ds = cache.run[(name, product, location)]
    except KeyError:
        abort(404)
    filename = os.path.basename(ds['filepath'])
    messages = (line for line in load_detailed_log(cache.run_id)
                if line['dataset']['filename'] == filename)

    return render_template('log_detail.html', ds=ds, messages=messages)

@app.route("/model-raw/<name>/<product>/<location>/")
def model_raw(name, product, location):
    try:
        ds = cache.run[(name, product, location)]
    except KeyError:
        abort(404)
    return json2html.convert(json=order_dict(ds))

@app.route("/given-raw/<name>/<product>/<location>/")
def given_raw(name, product, location):
    try:
        ds = cache.given[(name, product, location)]
    except KeyError:
        abort(404)
    return json2html.convert(json=order_dict(ds))
{'fraction_non_alphanumeric': 0.13896766874645491, 'fraction_numerical': 0.006806579693703914, 'mean_word_length': 3.91635687732342, 'pattern_counts': {'":': 10, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6477788', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1597, 'n_words': 372}