You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7.9 KiB

🥇 🔁

🙅 FastAPI 📁 💪 👀 💖 👉:

{* ../../docs_src/first_steps/tutorial001.py *}

📁 👈 📁 main.py.

🏃 🖖 💽:

$ uvicorn main:app --reload

<span style="color: green;">INFO</span>:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
<span style="color: green;">INFO</span>:     Started reloader process [28720]
<span style="color: green;">INFO</span>:     Started server process [28722]
<span style="color: green;">INFO</span>:     Waiting for application startup.
<span style="color: green;">INFO</span>:     Application startup complete.

/// note

📋 uvicorn main:app 🔗:

  • main: 📁 main.py (🐍 "🕹").
  • app: 🎚 ✍ 🔘 main.py ⏮️app = FastAPI().
  • --reload: ⚒ 💽⏮️ 📟 🔀. 🕴 ⚙️ 🛠️.

///

🔢, 📤⏮️ 🕳 💖:

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

👈🎦 📛 🌐 👆 📱 🍦, 👆 🇧🇿 🎰.

📂 👆 🖥 http://127.0.0.1:8000.

👆 🔜 👀 🎻 📨:

{"message": "Hello World"}

🎓 🛠️ 🩺

🔜 🚶 http://127.0.0.1:8000/docs.

👆 🔜 👀 🏧 🎓 🛠️ 🧾 (🚚 🦁 🎚):

Swagger UI

🎛 🛠️ 🩺

& 🔜, 🚶 http://127.0.0.1:8000/redoc.

👆 🔜 👀 🎛 🏧 🧾 (🚚 📄):

ReDoc

🗄

FastAPI 🏗 "🔗" ⏮️ 🌐 👆 🛠️ ⚙️ 🗄 🐩🔗.

"🔗"

"🔗" 🔑 ⚖️ 📛 🕳. 🚫 📟 👈 🛠️ ️, 📝 📛.

🛠️ "🔗"

👉 💼, 🗄 🔧 👈 🤔 🔬 🔗 👆 🛠️.

👉 🔗 🔑 🔌 👆 🛠️ ➡, 💪 🔢 👫 , ️.

💽 "🔗"

⚖ "🔗" 💪 🔗 💠 💽, 💖 🎻 🎚.

👈 💼, 🔜🎻 🔢, & 📊 🆎 👫 ✔️, ️.

🗄 & 🎻 🔗

🗄 🔬 🛠️ 🔗 👆 🛠️. & 👈 🔗 🔌 🔑 (⚖️ "🔗") 📊 📨 & 📨 👆 🛠️ ⚙️ 🎻 🔗, 🐩 🎻 📊 🔗.

openapi.json

🚥 👆 😟 🔃 🍣 🗄 🔗 👀 💖, FastAPI 🔁 🏗 🎻 (🔗) ⏮️ 📛 🌐 👆 🛠️.

👆 💪 👀 🔗: http://127.0.0.1:8000/openapi.json.

🔜 🎦 🎻 ▶️ ⏮️ 🕳 💖:

{
    "openapi": "3.0.2",
    "info": {
        "title": "FastAPI",
        "version": "0.1.0"
    },
    "paths": {
        "/items/": {
            "get": {
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {



...

🗄

🗄 🔗 🏋️ 2️⃣ 🎓 🧾 ⚙️ 🔌.

& 📤 💯 🎛, 🌐 🔛 🗄. 👆 💪 💪 🚮 🙆 📚 🎛 👆 🈸 🏗 ⏮️ FastAPI.

👆 💪 ⚙️ ️ 🏗 📟 🔁, 👩‍💻 👈 🔗 ⏮️ 👆 🛠️. 🖼, 🕸, 📱 ⚖️🈸.

🌃, 🔁 🔁

🔁 1️⃣: 🗄 FastAPI

{* ../../docs_src/first_steps/tutorial001.py hl[1] *}

FastAPI 🐍 🎓 👈 🚚 🌐 🛠️ 👆 🛠️.

/// note | 📡

FastAPI 🎓 👈 😖 🔗 ➡️ Starlette.

👆 💪 ⚙️ 🌐 💃 🛠️ ⏮️ FastAPI 💁‍♂️.

///

🔁 2️⃣: ✍ FastAPI "👐"

{* ../../docs_src/first_steps/tutorial001.py hl[3] *}

📥 app 🔢 🔜 "👐" 🎓 FastAPI.

👉 🔜 👑🔗🌐 👆 🛠️.

👉 app 🎏 1️⃣ 🔗 uvicorn 📋:

$ uvicorn main:app --reload

<span style="color: green;">INFO</span>:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

🚥 👆👆 📱 💖:

{* ../../docs_src/first_steps/tutorial002.py hl[3] *}

& 🚮 📁 main.py, ⤴️ 👆 🔜 🤙 uvicorn 💖:

$ uvicorn main:my_awesome_api --reload

<span style="color: green;">INFO</span>:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

🔁 3️⃣: ✍ 🛠️

"➡" 📥 🔗 🏁 🍕 📛 ▶️ ➡️ 🥇 /.

, 📛 💖:

https://example.com/items/foo

...➡ 🔜:

/items/foo

/// info

"➡" 🛎 🤙 "🔗" ⚖️ "🛣".

///

🏗 🛠️, "➡" 👑 🌌 🎏 "⚠" & "ℹ".

🛠️

"🛠️" 📥 🔗 1️⃣ 🇺🇸🔍 "👩‍🔬".

1️⃣:

  • POST
  • GET
  • PUT
  • DELETE

...& 🌅 😍 🕐:

  • OPTIONS
  • HEAD
  • PATCH
  • TRACE

🇺🇸🔍 🛠️, 👆 💪 🔗 🔠⚙️ 1️⃣ (⚖️ 🌅) 👫 "👩‍🔬".


🕐 🏗 🔗, 👆 🛎 ⚙️ 👫 🎯 🇺🇸🔍 👩‍🔬 🎭 🎯 🎯.

🛎 👆 ⚙️:

  • POST: ✍ 💽.
  • GET: ✍ 💽.
  • PUT: ℹ 💽.
  • DELETE: 💽.

, 🗄, 🔠 🇺🇸🔍 👩‍🔬 🤙 "🛠️".

👥 🔜 🤙 👫 "🛠️" 💁‍♂️.

🔬 🛠️ 👨‍🎨

{* ../../docs_src/first_steps/tutorial001.py hl[6] *}

@app.get("/") 💬 FastAPI 👈 🔢 ▶️🔛 🈚 🚚 📨 👈 🚶:

  • /
  • ⚙️ get 🛠️

/// info | @decorator

👈 @something 🐍 🤙 "👨‍🎨".

👆 🚮 🔛 🔝 🔢. 💖 📶 📔 👒 (👤 💭 👈 🌐👟 ➡️).

"👨‍🎨" 🔢 🔛 & 🔨 🕳 ⏮️ ️.

👆 💼, 👉 👨‍🎨 💬 FastAPI 👈 🔢 🔛 🔗 / ⏮️ 🛠️ get.

️ "🛠️ 👨‍🎨".

///

👆 💪 ⚙️ 🎏 🛠️:

  • @app.post()
  • @app.put()
  • @app.delete()

& 🌅 😍 🕐:

  • @app.options()
  • @app.head()
  • @app.patch()
  • @app.trace()

/// tip

👆 🆓 ⚙️ 🔠 🛠️ (🇺🇸🔍 👩‍🔬) 👆 🎋.

FastAPI 🚫 🛠️ 🙆 🎯 🔑.

📥 🎁 📄, 🚫 📄.

🖼, 🕐 ⚙️ 🕹 👆 🛎 🎭 🌐 🎯 ⚙️ 🕴 POST 🛠️.

///

🔁 4️⃣: 🔬 🛠️ 🔢

👉 👆 "🛠️ 🔢":

  • : /.
  • 🛠️: get.
  • 🔢: 🔢 🔛 "👨‍🎨" (🔛 @app.get("/")).

{* ../../docs_src/first_steps/tutorial001.py hl[7] *}

👉 🐍 🔢.

🔜 🤙 FastAPI 🕐 📨 📨 📛 "/" ⚙️ GET 🛠️.

👉 💼, async 🔢.


👆 💪 🔬 😐 🔢 ↩️ async def:

{* ../../docs_src/first_steps/tutorial003.py hl[7] *}

/// note

🚥 👆 🚫 💭 🔺, 🔁: "🏃 ❓"{.internal-link target=_blank}.

///

🔁 5️⃣: 📨 🎚

{* ../../docs_src/first_steps/tutorial001.py hl[8] *}

👆 💪 📨 dict, list, 💲 str, int, ️.

👆 💪 📨 Pydantic 🏷 (👆 🔜 👀 🌅 🔃 👈 ).

📤 📚 🎏 🎚 & 🏷 👈 🔜 🔁 🗜 🎻 (🔌 🐜, ️). 🔄 ⚙️ 👆 💕 🕐, 🏆 🎲 👈 👫 🐕‍🦺.

🌃

  • 🗄 FastAPI.
  • app 👐.
  • 🛠️ 👨‍🎨 (💖 @app.get("/")).
  • 🛠️ 🔢 (💖 def root(): ... 🔛).
  • 🏃 🛠️ 💽 (💖 uvicorn main:app --reload).