the_works/the_works/__init__.py
2025-07-19 15:06:32 +02:00

62 lines
1.9 KiB
Python

from flask import Flask
# this import is not strictly necessary but it forces pipreqs-to include dotenv when generating `requirements.txt`
import dotenv
from the_works.database import init_db
from the_works.views import home, text, werk, verlag, sprache, textform, werksform, genre, pseudonym, reihe, herausgeber, veroeffentlichung, titelbild
#from flask_debugtoolbar import DebugToolbarExtension
def create_app(config=None):
app = Flask(__name__)
# read all config values from environment that are prefixed with "FLASK_"
app.config.from_prefixed_env()
# some #DEBUG configuration
# toolbar = DebugToolbarExtension(app) #DEBUG
# app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False #DEBUG
# use config from function parameter if present
if config:
app.config.update(config)
# some #DEBUG output
print(f"Current Environment: {app.config['ENV'] if 'ENV' in app.config.keys() else 'ENV is not set'}") #DEBUG
print(app.config) #DEBUG
# initialize database
init_db(app)
# register blueprints
app.register_blueprint(genre.bp)
app.register_blueprint(herausgeber.bp)
app.register_blueprint(home.bp)
app.register_blueprint(pseudonym.bp)
app.register_blueprint(reihe.bp)
app.register_blueprint(sprache.bp)
app.register_blueprint(text.bp)
app.register_blueprint(textform.bp)
app.register_blueprint(verlag.bp)
app.register_blueprint(werk.bp)
app.register_blueprint(werksform.bp)
app.register_blueprint(veroeffentlichung.bp)
app.register_blueprint(titelbild.bp)
# register helper function
app.jinja_env.globals.update(sizeof_fmt=sizeof_fmt)
return app
# helper function to print formatted file size; [source](https://stackoverflow.com/a/1094933)
def sizeof_fmt(num: int | str, suffix: str = "B") -> str:
if isinstance(num, str):
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}"