import importlib.util import sys from pathlib import Path from flask import Flask, render_template_string from werkzeug.middleware.dispatcher import DispatcherMiddleware from werkzeug.serving import run_simple sys.path.insert(0, str(Path(__file__).parent)) CATEGORIES = ["personal", "home", "work"] root = Flask(__name__) def _discover_apps(): apps = [] base = Path(__file__).parent for category in CATEGORIES: cat_dir = base / category if not cat_dir.is_dir(): continue for app_dir in sorted(cat_dir.iterdir()): if app_dir.is_dir() and (app_dir / "app.py").exists(): apps.append((category, app_dir.name)) return apps @root.route("/") def index(): apps = _discover_apps() grouped: dict[str, list[tuple[str, str]]] = {} for category, app_name in apps: grouped.setdefault(category, []).append((category, app_name)) sections = "" delay = 0 for category in CATEGORIES: if category not in grouped: continue items_html = "" for cat, name in grouped[category]: items_html += ( f'' f'\u203a' f'
{name.replace("_", " ").replace("-", " ").title()}
' f'
/{cat}/{name}
' f"
" ) delay += 1 sections += f"""

{category.title()}

{items_html}
""" return render_template_string(f""" Tools

Tools

Flask applications

{sections}
""") def load_sub_apps(): mounts = {} base = Path(__file__).parent for category in CATEGORIES: cat_dir = base / category if not cat_dir.is_dir(): continue for app_dir in sorted(cat_dir.iterdir()): app_file = app_dir / "app.py" if not app_dir.is_dir() or not app_file.exists(): continue module_name = f"{category}.{app_dir.name}" spec = importlib.util.spec_from_file_location(module_name, app_file) mod = importlib.util.module_from_spec(spec) sys.modules[module_name] = mod spec.loader.exec_module(mod) mounts[f"/{category}/{app_dir.name}"] = mod.app print(f" mounted /{category}/{app_dir.name}") return mounts application = DispatcherMiddleware(root, load_sub_apps()) if __name__ == "__main__": run_simple("0.0.0.0", 8080, application, use_reloader=False)