change structure
This commit is contained in:
parent
2d61260583
commit
4f4fe1eff2
11 changed files with 126 additions and 726 deletions
95
server.py
95
server.py
|
|
@ -6,24 +6,52 @@ from flask import Flask, render_template_string
|
|||
from werkzeug.middleware.dispatcher import DispatcherMiddleware
|
||||
from werkzeug.serving import run_simple
|
||||
|
||||
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 = [
|
||||
d.name
|
||||
for d in Path(__file__).parent.iterdir()
|
||||
if d.is_dir() and (d / "app.py").exists()
|
||||
]
|
||||
items = "".join(
|
||||
f'<a href="/{a}" class="app-item" style="animation-delay:{i * 50}ms">'
|
||||
f'<span class="app-arrow">\u203a</span>'
|
||||
f'<div class="app-name">{a.replace("_", " ").replace("-", " ").title()}</div>'
|
||||
f'<div class="app-path">/{a}</div>'
|
||||
f"</a>"
|
||||
for i, a in enumerate(sorted(apps))
|
||||
)
|
||||
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'<a href="/{cat}/{name}" class="app-item" style="animation-delay:{delay * 50}ms">'
|
||||
f'<span class="app-arrow">\u203a</span>'
|
||||
f'<div class="app-name">{name.replace("_", " ").replace("-", " ").title()}</div>'
|
||||
f'<div class="app-path">/{cat}/{name}</div>'
|
||||
f"</a>"
|
||||
)
|
||||
delay += 1
|
||||
sections += f"""
|
||||
<div class="category">
|
||||
<h2 class="category-title">{category.title()}</h2>
|
||||
<div class="app-list">{items_html}</div>
|
||||
</div>"""
|
||||
|
||||
return render_template_string(f"""
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
|
@ -86,6 +114,20 @@ def index():
|
|||
color: var(--text-muted);
|
||||
}}
|
||||
|
||||
.category {{
|
||||
margin-bottom: 28px;
|
||||
}}
|
||||
|
||||
.category-title {{
|
||||
font-size: 0.82rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-muted);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.08em;
|
||||
margin-bottom: 10px;
|
||||
padding-left: 4px;
|
||||
}}
|
||||
|
||||
.app-list {{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
|
@ -154,7 +196,7 @@ def index():
|
|||
<h1>Tools</h1>
|
||||
<p class="subtitle">Flask applications</p>
|
||||
</header>
|
||||
<div class="app-list">{items}</div>
|
||||
{sections}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -164,18 +206,21 @@ def index():
|
|||
def load_sub_apps():
|
||||
mounts = {}
|
||||
base = Path(__file__).parent
|
||||
for app_dir in sorted(base.iterdir()):
|
||||
app_file = app_dir / "app.py"
|
||||
if not app_dir.is_dir() or not app_file.exists():
|
||||
for category in CATEGORIES:
|
||||
cat_dir = base / category
|
||||
if not cat_dir.is_dir():
|
||||
continue
|
||||
spec = importlib.util.spec_from_file_location(app_dir.name, app_file)
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
sys.modules[app_dir.name] = (
|
||||
mod # must be registered before exec so Flask(__name__) resolves root_path correctly
|
||||
)
|
||||
spec.loader.exec_module(mod)
|
||||
mounts[f"/{app_dir.name}"] = mod.app
|
||||
print(f" mounted /{app_dir.name}")
|
||||
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
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue