mirror of https://github.com/tiangolo/fastapi.git
383 lines
11 KiB
Markdown
383 lines
11 KiB
Markdown
# ⚒ & 🌐 🔢
|
||
|
||
📚 💼 👆 🈸 💪 💪 🔢 ⚒ ⚖️ 📳, 🖼 ㊙ 🔑, 💽 🎓, 🎓 📧 🐕🦺, ♒️.
|
||
|
||
🏆 👫 ⚒ 🔢 (💪 🔀), 💖 💽 📛. & 📚 💪 🚿, 💖 ㊙.
|
||
|
||
👉 🤔 ⚫️ ⚠ 🚚 👫 🌐 🔢 👈 ✍ 🈸.
|
||
|
||
## 🌐 🔢
|
||
|
||
!!! tip
|
||
🚥 👆 ⏪ 💭 ⚫️❔ "🌐 🔢" & ❔ ⚙️ 👫, 💭 🆓 🚶 ⏭ 📄 🔛.
|
||
|
||
<a href="https://en.wikipedia.org/wiki/Environment_variable" class="external-link" target="_blank">🌐 🔢</a> (💭 "🇨🇻 {") 🔢 👈 🖖 🏞 🐍 📟, 🏃♂ ⚙️, & 💪 ✍ 👆 🐍 📟 (⚖️ 🎏 📋 👍).
|
||
|
||
👆 💪 ✍ & ⚙️ 🌐 🔢 🐚, 🍵 💆♂ 🐍:
|
||
|
||
=== "💾, 🇸🇻, 🚪 🎉"
|
||
|
||
<div class="termy">
|
||
|
||
```console
|
||
// You could create an env var MY_NAME with
|
||
$ export MY_NAME="Wade Wilson"
|
||
|
||
// Then you could use it with other programs, like
|
||
$ echo "Hello $MY_NAME"
|
||
|
||
Hello Wade Wilson
|
||
```
|
||
|
||
</div>
|
||
|
||
=== "🚪 📋"
|
||
|
||
<div class="termy">
|
||
|
||
```console
|
||
// Create an env var MY_NAME
|
||
$ $Env:MY_NAME = "Wade Wilson"
|
||
|
||
// Use it with other programs, like
|
||
$ echo "Hello $Env:MY_NAME"
|
||
|
||
Hello Wade Wilson
|
||
```
|
||
|
||
</div>
|
||
|
||
### ✍ 🇨🇻 {🐍
|
||
|
||
👆 💪 ✍ 🌐 🔢 🏞 🐍, 📶 (⚖️ ⏮️ 🙆 🎏 👩🔬), & ⤴️ ✍ 👫 🐍.
|
||
|
||
🖼 👆 💪 ✔️ 📁 `main.py` ⏮️:
|
||
|
||
```Python hl_lines="3"
|
||
import os
|
||
|
||
name = os.getenv("MY_NAME", "World")
|
||
print(f"Hello {name} from Python")
|
||
```
|
||
|
||
!!! tip
|
||
🥈 ❌ <a href="https://docs.python.org/3.8/library/os.html#os.getenv" class="external-link" target="_blank">`os.getenv()`</a> 🔢 💲 📨.
|
||
|
||
🚥 🚫 🚚, ⚫️ `None` 🔢, 📥 👥 🚚 `"World"` 🔢 💲 ⚙️.
|
||
|
||
⤴️ 👆 💪 🤙 👈 🐍 📋:
|
||
|
||
<div class="termy">
|
||
|
||
```console
|
||
// Here we don't set the env var yet
|
||
$ python main.py
|
||
|
||
// As we didn't set the env var, we get the default value
|
||
|
||
Hello World from Python
|
||
|
||
// But if we create an environment variable first
|
||
$ export MY_NAME="Wade Wilson"
|
||
|
||
// And then call the program again
|
||
$ python main.py
|
||
|
||
// Now it can read the environment variable
|
||
|
||
Hello Wade Wilson from Python
|
||
```
|
||
|
||
</div>
|
||
|
||
🌐 🔢 💪 ⚒ 🏞 📟, ✋️ 💪 ✍ 📟, & 🚫 ✔️ 🏪 (💕 `git`) ⏮️ 🎂 📁, ⚫️ ⚠ ⚙️ 👫 📳 ⚖️ ⚒.
|
||
|
||
👆 💪 ✍ 🌐 🔢 🕴 🎯 📋 👼, 👈 🕴 💪 👈 📋, & 🕴 🚮 📐.
|
||
|
||
👈, ✍ ⚫️ ▶️️ ⏭ 📋 ⚫️, 🔛 🎏 ⏸:
|
||
|
||
<div class="termy">
|
||
|
||
```console
|
||
// Create an env var MY_NAME in line for this program call
|
||
$ MY_NAME="Wade Wilson" python main.py
|
||
|
||
// Now it can read the environment variable
|
||
|
||
Hello Wade Wilson from Python
|
||
|
||
// The env var no longer exists afterwards
|
||
$ python main.py
|
||
|
||
Hello World from Python
|
||
```
|
||
|
||
</div>
|
||
|
||
!!! tip
|
||
👆 💪 ✍ 🌅 🔃 ⚫️ <a href="https://12factor.net/config" class="external-link" target="_blank">1️⃣2️⃣-⚖ 📱: 📁</a>.
|
||
|
||
### 🆎 & 🔬
|
||
|
||
👫 🌐 🔢 💪 🕴 🍵 ✍ 🎻, 👫 🔢 🐍 & ✔️ 🔗 ⏮️ 🎏 📋 & 🎂 ⚙️ (& ⏮️ 🎏 🏃♂ ⚙️, 💾, 🚪, 🇸🇻).
|
||
|
||
👈 ⛓ 👈 🙆 💲 ✍ 🐍 ⚪️➡️ 🌐 🔢 🔜 `str`, & 🙆 🛠️ 🎏 🆎 ⚖️ 🔬 ✔️ 🔨 📟.
|
||
|
||
## Pydantic `Settings`
|
||
|
||
👐, Pydantic 🚚 👑 🚙 🍵 👫 ⚒ 👟 ⚪️➡️ 🌐 🔢 ⏮️ <a href="https://pydantic-docs.helpmanual.io/usage/settings/" class="external-link" target="_blank">Pydantic: ⚒ 🧾</a>.
|
||
|
||
### ✍ `Settings` 🎚
|
||
|
||
🗄 `BaseSettings` ⚪️➡️ Pydantic & ✍ 🎧-🎓, 📶 🌅 💖 ⏮️ Pydantic 🏷.
|
||
|
||
🎏 🌌 ⏮️ Pydantic 🏷, 👆 📣 🎓 🔢 ⏮️ 🆎 ✍, & 🎲 🔢 💲.
|
||
|
||
👆 💪 ⚙️ 🌐 🎏 🔬 ⚒ & 🧰 👆 ⚙️ Pydantic 🏷, 💖 🎏 📊 🆎 & 🌖 🔬 ⏮️ `Field()`.
|
||
|
||
```Python hl_lines="2 5-8 11"
|
||
{!../../../docs_src/settings/tutorial001.py!}
|
||
```
|
||
|
||
!!! tip
|
||
🚥 👆 💚 🕳 ⏩ 📁 & 📋, 🚫 ⚙️ 👉 🖼, ⚙️ 🏁 1️⃣ 🔛.
|
||
|
||
⤴️, 🕐❔ 👆 ✍ 👐 👈 `Settings` 🎓 (👉 💼, `settings` 🎚), Pydantic 🔜 ✍ 🌐 🔢 💼-😛 🌌,, ↖-💼 🔢 `APP_NAME` 🔜 ✍ 🔢 `app_name`.
|
||
|
||
⏭ ⚫️ 🔜 🗜 & ✔ 💽. , 🕐❔ 👆 ⚙️ 👈 `settings` 🎚, 👆 🔜 ✔️ 📊 🆎 👆 📣 (✅ `items_per_user` 🔜 `int`).
|
||
|
||
### ⚙️ `settings`
|
||
|
||
⤴️ 👆 💪 ⚙️ 🆕 `settings` 🎚 👆 🈸:
|
||
|
||
```Python hl_lines="18-20"
|
||
{!../../../docs_src/settings/tutorial001.py!}
|
||
```
|
||
|
||
### 🏃 💽
|
||
|
||
⏭, 👆 🔜 🏃 💽 🚶♀️ 📳 🌐 🔢, 🖼 👆 💪 ⚒ `ADMIN_EMAIL` & `APP_NAME` ⏮️:
|
||
|
||
<div class="termy">
|
||
|
||
```console
|
||
$ ADMIN_EMAIL="deadpool@example.com" APP_NAME="ChimichangApp" uvicorn main:app
|
||
|
||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
||
```
|
||
|
||
</div>
|
||
|
||
!!! tip
|
||
⚒ 💗 🇨🇻 {👁 📋 🎏 👫 ⏮️ 🚀, & 🚮 👫 🌐 ⏭ 📋.
|
||
|
||
& ⤴️ `admin_email` ⚒ 🔜 ⚒ `"deadpool@example.com"`.
|
||
|
||
`app_name` 🔜 `"ChimichangApp"`.
|
||
|
||
& `items_per_user` 🔜 🚧 🚮 🔢 💲 `50`.
|
||
|
||
## ⚒ ➕1️⃣ 🕹
|
||
|
||
👆 💪 🚮 👈 ⚒ ➕1️⃣ 🕹 📁 👆 👀 [🦏 🈸 - 💗 📁](../tutorial/bigger-applications.md){.internal-link target=_blank}.
|
||
|
||
🖼, 👆 💪 ✔️ 📁 `config.py` ⏮️:
|
||
|
||
```Python
|
||
{!../../../docs_src/settings/app01/config.py!}
|
||
```
|
||
|
||
& ⤴️ ⚙️ ⚫️ 📁 `main.py`:
|
||
|
||
```Python hl_lines="3 11-13"
|
||
{!../../../docs_src/settings/app01/main.py!}
|
||
```
|
||
|
||
!!! tip
|
||
👆 🔜 💪 📁 `__init__.py` 👆 👀 🔛 [🦏 🈸 - 💗 📁](../tutorial/bigger-applications.md){.internal-link target=_blank}.
|
||
|
||
## ⚒ 🔗
|
||
|
||
🍾 ⚫️ 5️⃣📆 ⚠ 🚚 ⚒ ⚪️➡️ 🔗, ↩️ ✔️ 🌐 🎚 ⏮️ `settings` 👈 ⚙️ 🌐.
|
||
|
||
👉 💪 ✴️ ⚠ ⏮️ 🔬, ⚫️ 📶 ⏩ 🔐 🔗 ⏮️ 👆 👍 🛃 ⚒.
|
||
|
||
### 📁 📁
|
||
|
||
👟 ⚪️➡️ ⏮️ 🖼, 👆 `config.py` 📁 💪 👀 💖:
|
||
|
||
```Python hl_lines="10"
|
||
{!../../../docs_src/settings/app02/config.py!}
|
||
```
|
||
|
||
👀 👈 🔜 👥 🚫 ✍ 🔢 👐 `settings = Settings()`.
|
||
|
||
### 👑 📱 📁
|
||
|
||
🔜 👥 ✍ 🔗 👈 📨 🆕 `config.Settings()`.
|
||
|
||
```Python hl_lines="5 11-12"
|
||
{!../../../docs_src/settings/app02/main.py!}
|
||
```
|
||
|
||
!!! tip
|
||
👥 🔜 🔬 `@lru_cache()` 🍖.
|
||
|
||
🔜 👆 💪 🤔 `get_settings()` 😐 🔢.
|
||
|
||
& ⤴️ 👥 💪 🚚 ⚫️ ⚪️➡️ *➡ 🛠️ 🔢* 🔗 & ⚙️ ⚫️ 🙆 👥 💪 ⚫️.
|
||
|
||
```Python hl_lines="16 18-20"
|
||
{!../../../docs_src/settings/app02/main.py!}
|
||
```
|
||
|
||
### ⚒ & 🔬
|
||
|
||
⤴️ ⚫️ 🔜 📶 ⏩ 🚚 🎏 ⚒ 🎚 ⏮️ 🔬 🏗 🔗 🔐 `get_settings`:
|
||
|
||
```Python hl_lines="9-10 13 21"
|
||
{!../../../docs_src/settings/app02/test_main.py!}
|
||
```
|
||
|
||
🔗 🔐 👥 ⚒ 🆕 💲 `admin_email` 🕐❔ 🏗 🆕 `Settings` 🎚, & ⤴️ 👥 📨 👈 🆕 🎚.
|
||
|
||
⤴️ 👥 💪 💯 👈 ⚫️ ⚙️.
|
||
|
||
## 👂 `.env` 📁
|
||
|
||
🚥 👆 ✔️ 📚 ⚒ 👈 🎲 🔀 📚, 🎲 🎏 🌐, ⚫️ 5️⃣📆 ⚠ 🚮 👫 🔛 📁 & ⤴️ ✍ 👫 ⚪️➡️ ⚫️ 🚥 👫 🌐 🔢.
|
||
|
||
👉 💡 ⚠ 🥃 👈 ⚫️ ✔️ 📛, 👫 🌐 🔢 🛎 🥉 📁 `.env`, & 📁 🤙 "🇨🇻".
|
||
|
||
!!! tip
|
||
📁 ▶️ ⏮️ ❣ (`.`) 🕵♂ 📁 🖥-💖 ⚙️, 💖 💾 & 🇸🇻.
|
||
|
||
✋️ 🇨🇻 📁 🚫 🤙 ✔️ ✔️ 👈 ☑ 📁.
|
||
|
||
Pydantic ✔️ 🐕🦺 👂 ⚪️➡️ 👉 🆎 📁 ⚙️ 🔢 🗃. 👆 💪 ✍ 🌖 <a href="https://pydantic-docs.helpmanual.io/usage/settings/#dotenv-env-support" class="external-link" target="_blank">Pydantic ⚒: 🇨🇻 (.🇨🇻) 🐕🦺</a>.
|
||
|
||
!!! tip
|
||
👉 👷, 👆 💪 `pip install python-dotenv`.
|
||
|
||
### `.env` 📁
|
||
|
||
👆 💪 ✔️ `.env` 📁 ⏮️:
|
||
|
||
```bash
|
||
ADMIN_EMAIL="deadpool@example.com"
|
||
APP_NAME="ChimichangApp"
|
||
```
|
||
|
||
### ✍ ⚒ ⚪️➡️ `.env`
|
||
|
||
& ⤴️ ℹ 👆 `config.py` ⏮️:
|
||
|
||
```Python hl_lines="9-10"
|
||
{!../../../docs_src/settings/app03/config.py!}
|
||
```
|
||
|
||
📥 👥 ✍ 🎓 `Config` 🔘 👆 Pydantic `Settings` 🎓, & ⚒ `env_file` 📁 ⏮️ 🇨🇻 📁 👥 💚 ⚙️.
|
||
|
||
!!! tip
|
||
`Config` 🎓 ⚙️ Pydantic 📳. 👆 💪 ✍ 🌖 <a href="https://pydantic-docs.helpmanual.io/usage/model_config/" class="external-link" target="_blank">Pydantic 🏷 📁</a>
|
||
|
||
### 🏗 `Settings` 🕴 🕐 ⏮️ `lru_cache`
|
||
|
||
👂 📁 ⚪️➡️ 💾 🛎 ⚠ (🐌) 🛠️, 👆 🎲 💚 ⚫️ 🕴 🕐 & ⤴️ 🏤-⚙️ 🎏 ⚒ 🎚, ↩️ 👂 ⚫️ 🔠 📨.
|
||
|
||
✋️ 🔠 🕰 👥:
|
||
|
||
```Python
|
||
Settings()
|
||
```
|
||
|
||
🆕 `Settings` 🎚 🔜 ✍, & 🏗 ⚫️ 🔜 ✍ `.env` 📁 🔄.
|
||
|
||
🚥 🔗 🔢 💖:
|
||
|
||
```Python
|
||
def get_settings():
|
||
return Settings()
|
||
```
|
||
|
||
👥 🔜 ✍ 👈 🎚 🔠 📨, & 👥 🔜 👂 `.env` 📁 🔠 📨. 👶 👶
|
||
|
||
✋️ 👥 ⚙️ `@lru_cache()` 👨🎨 🔛 🔝, `Settings` 🎚 🔜 ✍ 🕴 🕐, 🥇 🕰 ⚫️ 🤙. 👶 👶
|
||
|
||
```Python hl_lines="1 10"
|
||
{!../../../docs_src/settings/app03/main.py!}
|
||
```
|
||
|
||
⤴️ 🙆 🏁 🤙 `get_settings()` 🔗 ⏭ 📨, ↩️ 🛠️ 🔗 📟 `get_settings()` & 🏗 🆕 `Settings` 🎚, ⚫️ 🔜 📨 🎏 🎚 👈 📨 🔛 🥇 🤙, 🔄 & 🔄.
|
||
|
||
#### `lru_cache` 📡 ℹ
|
||
|
||
`@lru_cache()` 🔀 🔢 ⚫️ 🎀 📨 🎏 💲 👈 📨 🥇 🕰, ↩️ 💻 ⚫️ 🔄, 🛠️ 📟 🔢 🔠 🕰.
|
||
|
||
, 🔢 🔛 ⚫️ 🔜 🛠️ 🕐 🔠 🌀 ❌. & ⤴️ 💲 📨 🔠 👈 🌀 ❌ 🔜 ⚙️ 🔄 & 🔄 🕐❔ 🔢 🤙 ⏮️ ⚫️❔ 🎏 🌀 ❌.
|
||
|
||
🖼, 🚥 👆 ✔️ 🔢:
|
||
|
||
```Python
|
||
@lru_cache()
|
||
def say_hi(name: str, salutation: str = "Ms."):
|
||
return f"Hello {salutation} {name}"
|
||
```
|
||
|
||
👆 📋 💪 🛠️ 💖 👉:
|
||
|
||
```mermaid
|
||
sequenceDiagram
|
||
|
||
participant code as Code
|
||
participant function as say_hi()
|
||
participant execute as Execute function
|
||
|
||
rect rgba(0, 255, 0, .1)
|
||
code ->> function: say_hi(name="Camila")
|
||
function ->> execute: execute function code
|
||
execute ->> code: return the result
|
||
end
|
||
|
||
rect rgba(0, 255, 255, .1)
|
||
code ->> function: say_hi(name="Camila")
|
||
function ->> code: return stored result
|
||
end
|
||
|
||
rect rgba(0, 255, 0, .1)
|
||
code ->> function: say_hi(name="Rick")
|
||
function ->> execute: execute function code
|
||
execute ->> code: return the result
|
||
end
|
||
|
||
rect rgba(0, 255, 0, .1)
|
||
code ->> function: say_hi(name="Rick", salutation="Mr.")
|
||
function ->> execute: execute function code
|
||
execute ->> code: return the result
|
||
end
|
||
|
||
rect rgba(0, 255, 255, .1)
|
||
code ->> function: say_hi(name="Rick")
|
||
function ->> code: return stored result
|
||
end
|
||
|
||
rect rgba(0, 255, 255, .1)
|
||
code ->> function: say_hi(name="Camila")
|
||
function ->> code: return stored result
|
||
end
|
||
```
|
||
|
||
💼 👆 🔗 `get_settings()`, 🔢 🚫 ✊ 🙆 ❌, ⚫️ 🕧 📨 🎏 💲.
|
||
|
||
👈 🌌, ⚫️ 🎭 🌖 🚥 ⚫️ 🌐 🔢. ✋️ ⚫️ ⚙️ 🔗 🔢, ⤴️ 👥 💪 🔐 ⚫️ 💪 🔬.
|
||
|
||
`@lru_cache()` 🍕 `functools` ❔ 🍕 🐍 🐩 🗃, 👆 💪 ✍ 🌅 🔃 ⚫️ <a href="https://docs.python.org/3/library/functools.html#functools.lru_cache" class="external-link" target="_blank">🐍 🩺 `@lru_cache()`</a>.
|
||
|
||
## 🌃
|
||
|
||
👆 💪 ⚙️ Pydantic ⚒ 🍵 ⚒ ⚖️ 📳 👆 🈸, ⏮️ 🌐 🏋️ Pydantic 🏷.
|
||
|
||
* ⚙️ 🔗 👆 💪 📉 🔬.
|
||
* 👆 💪 ⚙️ `.env` 📁 ⏮️ ⚫️.
|
||
* ⚙️ `@lru_cache()` ➡️ 👆 ❎ 👂 🇨🇻 📁 🔄 & 🔄 🔠 📨, ⏪ 🤝 👆 🔐 ⚫️ ⏮️ 🔬.
|