from datetime import datetime from math import floor, log2, log10 from markupsafe import Markup def print_sizes10(var): i = floor(log10(var) / 3) expo = i * 3 PREFIX = [ "Byte", "kByte", "MByte", "GByte", "TByte", "PByte", ] return Markup(f"{round(var / (10**expo))} {PREFIX[i]}") def print_sizes2(var): i = floor(log2(var) / 10) expo = i * 10 PREFIX = [ "Byte", "KiByte", "MiByte", "GiByte", "TiByte", "PiByte", ] return Markup(f"{round(var / (2**expo))} {PREFIX[i]}") def parse_iso_date(inp): return datetime.fromisoformat(inp) def add_view_funcs(app): app.jinja_env.filters["byte_size2"] = print_sizes2 app.jinja_env.filters["byte_size10"] = print_sizes10 app.context_processor(lambda: dict(parse_iso_date=parse_iso_date))