a6062fbacc
Co-authored-by: Cursor <cursoragent@cursor.com>
44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from flask import Flask
|
|
|
|
from app.config import Config
|
|
from app.db import init_db, seed_if_empty
|
|
from app.routes import register_routes
|
|
|
|
|
|
def _fmt_price(value: Any, digits: int = 3) -> str:
|
|
"""价格展示:固定小数位,避免浮点尾巴。"""
|
|
if value is None or value == "":
|
|
return "—"
|
|
try:
|
|
return f"{float(value):.{int(digits)}f}"
|
|
except (TypeError, ValueError):
|
|
return str(value)
|
|
|
|
|
|
def _fmt_money(value: Any, digits: int = 2) -> str:
|
|
if value is None or value == "":
|
|
return "—"
|
|
try:
|
|
return f"{float(value):.{int(digits)}f}"
|
|
except (TypeError, ValueError):
|
|
return str(value)
|
|
|
|
|
|
def create_app() -> Flask:
|
|
app = Flask(__name__, template_folder="templates", static_folder="static")
|
|
app.config.from_object(Config)
|
|
app.secret_key = Config.SECRET_KEY
|
|
|
|
app.jinja_env.filters["fmt_price"] = _fmt_price
|
|
app.jinja_env.filters["fmt_money"] = _fmt_money
|
|
|
|
init_db()
|
|
seed_if_empty()
|
|
register_routes(app)
|
|
return app
|