FastAPI ?????? ???? ?? ? ?????? ??
FastAPI? Python ?? ??? ??? ??? ??? ???? ? ?????? ??? ? ???? ??? ????? ??? ??? Python ? ????????. ???? ? ??????? ??? ? FastAPI? ??????? ?? ??? ?? ??? ? ??? ?? ??? ??? ??? ?????.
????? FastAPI ?????? ???? ???? ???? ? ??????? ???? ??? ?? ???? ?? ??? ?????.
- ?? FastAPI? ?? ?? ?????? ???? ???. pip? ???? ??? ? ????:
pip install fastapi[all]
- app.py ??? ???? ? ??????? ?????:
from typing import Optional from fastapi import FastAPI from fastapi import Request, Depends from fastapi.templating import Jinja2Templates from fastapi.staticfiles import StaticFiles from fastapi.responses import HTMLResponse from starlette.templating import Jinja2Templates from starlette.requests import Request from fastapi.i18n import ( I18nMiddleware, get_accept_languages ) app = FastAPI() # 加載靜態(tài)文件 app.mount("/static", StaticFiles(directory="static"), name="static") # 初始化國際化中間件 app.add_middleware(I18nMiddleware, default_language="en", translation_directory="translations") templates = Jinja2Templates(directory="templates") # 通過GET方法獲取主頁面 @app.get("/", response_class=HTMLResponse) async def read_root(request: Request, languages: str = Depends(get_accept_languages)): return templates.TemplateResponse("index.html", {"request": request, "languages": languages}) # 通過POST方法獲取表單提交的數(shù)據(jù)并返回 @app.post("/form") async def form_post(request: Request): form_data = await request.form() return {"data": form_data}
- ???? ?? ????? ?? ??? ??? ? ?? ?? ??? ???? en ??? ????. ?? ??. en ??? message.po ??? ??? ?? ??? ?????:
msgid "Hello" msgstr "Hello" msgid "Submit" msgstr "Submit"
- templates ??? index.html ??? ??? ??? ???? ?????:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>{{ _('Welcome to my website') }}</title> </head> <body> <h1>{{ _('Hello') }}</h1> <p>{{ _('This is a sample web application') }}</p> <form action="/form" method="post"> <input type="text" name="name" placeholder="{{ _('Enter your name') }}"> <button type="submit">{{ _('Submit') }}</button> </form> <h2>{{ _('Supported Languages') }}</h2> <ul> {% for language in languages %} <li><a href="/?language={{ language }}">{{ language }}</a></li> {% endfor %} </ul> </body> </html>
- ?????? ??:
uvicorn app:app --reload
You http://localhost:8000? ???? ??????? ? ? ????. ?? ??? ?????. http://localhost:8000/???? ?? URL ???? ??
? ?? ??? ??? ? ????. =zh . language
來切換語言,比如http://localhost:8000/?language=zh。
以上示例中,我們使用了FastAPI提供的國際化中間件,通過在HTTP請求頭中添加Accept-Language來指定用戶的語言偏好,從而實(shí)現(xiàn)多語言支持。在應(yīng)用中我們使用了Jinja2模板引擎來渲染頁面,通過在模板中使用{{ _('xxx') }}
{{ _('xxx') }}
? ???? ??? ?????. ?? ?? ?? FastAPI ??????? ???? ???? ? ??????? ?? ???? ? ?? ??? ??? ??? ???? ??? ? ????. ??? ??? FastAPI ?????? ???? ???? ? ?????? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

FastAPI ?????? ???? ???? ? ??????? ?????. FastAPI? Python ?? ??? ??? ??? ??? ???? ? ?????? ??? ?? ???? ??? ????? ??? ??? Python ? ????????. ???? ? ??????? ??? ? FastAPI? ??????? ?? ??? ?? ??? ? ??? ?? ??? ??? ??? ?????. ????? FastAPI ?????? ???? ???? ??? ???? ???? ?? ??? ?????.

Django, Flask ? FastAPI: ?? ?? ??? ?? ??? ?? ?????. ?? ?? ??? ?????. ??: ?? ? ????? ??? ?????? ???? ?? ?????. ? ?? ???? Python? ????? ???? ?? Django, Flask ? FastAPI? ?? ?????? ???? ???? ?? ? ??? ?? ????. ? ????? ?? ?? ??? ?? ??? ?????? ???? ? ??? ??? ?? ?? ??? ??? ? ? ?? ?????? ?? ? ?? ??? ????? ?????. 1.?

PHP8? JIT ???? ?? ? ??????? ??? ??? ?????? ? ??????? ???? ??? ?? ??? ?? ? ??????? ?? ??? ???? ?? ??? ? ??? ?????. ????? ???? ?? ? ???? ??? PHP? ?? ?????? ??? ??????. JIT(Just-In-Time ???) ????? PHP8? ???? ????? ??? ?? ??? ???? ?????. ? ????? PHP8? JIT ???? ?? ? ??????? ??? ???? ? ?? ??? ??? ???? ???? ?? ??? ?????.

1. ?? ??? ???? ??? ???? ?? ? ???? ???? ?? ??????? ?????. CREATETABLEtranslations(idINTNOTNULLAUTO_INCREMENT,localeVARCHAR(255)NOTNULL,keyVARCHAR(255)NOTNULL,valueTEXTNOTNULL,PRIMARYKEY(id)). ???? ???? ????? ?? ???? ???? ???? ???? ??? ??? ? ????. //?? ??? ?????. $current_locale=isset($_GET["locale"])?$_

PHP ???? ??? ? ??? ??? ????? ???? ?? ??? ?????. ???? ???? ?? ??? ? ???? ?? ???? ??? ?? ? ???? ????. PHP ??? ??? ??? ??? ??? ??? ??? ????? ?????? ???? ???? ? ??? ??? ?????. ?? ??? ?? PHP ????? ?? ?? ???? ???? ????? ???? ???. ?? ?? ????? UTF-8 ???? ???? ?? ?? ???? ?????. PHP ??? ??? ?? ??? ??? ? ????: header('C

? ?? ?????? MySQL? ?? ??? ???? ???? ???? ????. 1. MySQL? ??? ??, ?? ????, ???? ??? ? ?? ???? ????? ?????. 2. SQL ??? ?? ???? ???????? ??? ???? ?? ???? ?? ? ? ????. 3.mysql? ?????-?? ??? ???? ???? ?? ??? ?? ??? ?????.

?? ??? ?? Hyperf ?????? ???? ?? ???? ??? ???? ?? ?? ?? ????? ??? ?? ? ??? ??? ??? ???? ?? ?? ?? ?? ??? ???? ???. ??? ??? ?????? Hyperf ?????? ?? ?? ??? ???? ???? ??? ?? ????? ??? ???? ? ??? ? ? ????. ? ????? Hyperf ??????? ??? ??? ???? ??? ???? ?? ?? ??? ?????. 1. ?? ?? ??? ????? ?? Hyperf ?? ??? ???? ???.

FlaskvsFastAPI: ???? API? ???? ?? ??? ??, ?? ?? ??? ?????. ??: ???? ????? API? ?? ??????? ?? ?? ?? ? ??? ?????. ????? ????? ???? ?? API? ???? ?? ???? ??? ?? ? ?????. Python ????? ?? ?? ???? ? ?? ? ?????? Flask? FastAPI? API ??? ?? ?????. ? ????? ? ?????? ???? ???? ???? ?? ??? ?????.
