From edb19bde80d0ccd5a8502667b335ddd84c89d187 Mon Sep 17 00:00:00 2001 From: eclipse Date: Tue, 27 May 2025 18:30:43 +0200 Subject: [PATCH] added a jinja helper function for displaying file sizes --- the_works/__init__.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/the_works/__init__.py b/the_works/__init__.py index 4c714d2..2acfaf7 100644 --- a/the_works/__init__.py +++ b/the_works/__init__.py @@ -41,4 +41,18 @@ def create_app(): app.config['SQLALCHEMY_RECORD_QUERIES'] = os.getenv("SQLALCHEMY_RECORD_QUERIES") print(f"Current Environment: " + app.config['ENVIRONMENT']) + # register helper function + app.jinja_env.globals.update(sizeof_fmt=sizeof_fmt) + return app + + +# helper function to print formatted file size from https://stackoverflow.com/a/1094933 +def sizeof_fmt(num, suffix="B"): + if type(num) == "String": + num = int(num) + for unit in ("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"): + if abs(num) < 1024.0: + return f"{num:3.1f} {unit}{suffix}" + num /= 1024.0 + return f"{num:.1f} Yi{suffix}"