<commit_before>from textwrap import dedent
import random

from flask import render_template_string, g, render_template, session
from jinja2 import Markup
from pygments.formatters.html import HtmlFormatter
from pygments.lexers import get_lexer_by_name
import pygments

from pydrill import app
from pydrill import gen


@app.template_filter()
def render_question(question):
    return Markup(render_template(
        'question.html', question=question, vars=get_template_vars(question)))


@app.template_filter()
def render_explained_question(question, given_answer):
    return Markup(render_template(
        'explained_question.html', question=question, given_answer=given_answer,
        vars=get_template_vars(question)
    ))


def get_template_vars(question):
    template = app.jinja_env.from_string(question.text)
    random.seed(g.seed)
    return vars(template.module)


@app.template_filter()
def render(template, vars={}):
    random.seed(g.seed)
    return Markup(render_template_string(template, **vars))


@app.template_filter()
def in_random_order(iterable):
    items = list(iterable)
    random.shuffle(items)
    return items


def highlight_with_css_class(s, language, css_class):
    formatter = HtmlFormatter(cssclass=css_class)
    # TODO: can we do without strip?
    return pygments.highlight(dedent(unicode(s)), get_lexer_by_name(language),
                              formatter).strip()


@app.template_filter()
def highlight(s, language='python'):
    return highlight_with_css_class(s, language, 'highlight')


@app.template_filter()
def highlight_inline(s, language='python'):
    return highlight_with_css_class(s, language, 'highlight-inline')


@app.template_filter()
def get_answer_message(answer_is_correct):
    if answer_is_correct:
        return 'right!'
    return 'wrong!'


@app.template_filter()
def percentile_text(percentile):
    if percentile >= 0.5:
        return "You're in the top {:.0%}".format(max(1 - percentile, 0.01))
    return "You're in the bottom {:.0%}".format(max(percentile, 0.01))


@app.template_filter('repr')
def repr_filter(obj):
    return repr(obj)


def get_prev_answer():
    return session.pop('prev_answer', None)


app.jinja_env.globals.update(gen=gen, get_prev_answer=get_prev_answer)<commit_msg>Use @app.template_global instead of globals.update()<commit_after>from textwrap import dedent
import random

from flask import render_template_string, g, render_template, session
from jinja2 import Markup
from pygments.formatters.html import HtmlFormatter
from pygments.lexers import get_lexer_by_name
import pygments

from pydrill import app
from pydrill import gen


@app.template_filter()
def render_question(question):
    return Markup(render_template(
        'question.html', question=question, vars=get_template_vars(question)))


@app.template_filter()
def render_explained_question(question, given_answer):
    return Markup(render_template(
        'explained_question.html', question=question, given_answer=given_answer,
        vars=get_template_vars(question)
    ))


def get_template_vars(question):
    template = app.jinja_env.from_string(question.text)
    random.seed(g.seed)
    return vars(template.module)


@app.template_filter()
def render(template, vars={}):
    random.seed(g.seed)
    return Markup(render_template_string(template, **vars))


@app.template_filter()
def in_random_order(iterable):
    items = list(iterable)
    random.shuffle(items)
    return items


def highlight_with_css_class(s, language, css_class):
    formatter = HtmlFormatter(cssclass=css_class)
    # TODO: can we do without strip?
    return pygments.highlight(dedent(unicode(s)), get_lexer_by_name(language),
                              formatter).strip()


@app.template_filter()
def highlight(s, language='python'):
    return highlight_with_css_class(s, language, 'highlight')


@app.template_filter()
def highlight_inline(s, language='python'):
    return highlight_with_css_class(s, language, 'highlight-inline')


@app.template_filter()
def get_answer_message(answer_is_correct):
    if answer_is_correct:
        return 'right!'
    return 'wrong!'


@app.template_filter()
def percentile_text(percentile):
    if percentile >= 0.5:
        return "You're in the top {:.0%}".format(max(1 - percentile, 0.01))
    return "You're in the bottom {:.0%}".format(max(percentile, 0.01))


@app.template_filter('repr')
def repr_filter(obj):
    return repr(obj)


@app.template_global()
def get_prev_answer():
    return session.pop('prev_answer', None)


app.jinja_env.globals.update(gen=gen)
{'fraction_non_alphanumeric': 0.10994535519125682, 'fraction_numerical': 0.005245901639344262, 'mean_word_length': 4.958333333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '878586', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1420, 'n_words': 303}