mirror of
https://github.com/fastapi/fastapi.git
synced 2026-04-01 21:53:57 -04:00
* 🌐 Refactor file structure to support internationalization * ✅ Update tests changed after i18n * 🔀 Merge Typer style from master * 🔧 Update MkConfig with Typer-styles * 🎨 Format mkdocs.yml with cannonical form * 🎨 Format mkdocs.yml * 🔧 Update MkDocs config * ➕ Add docs translation scripts dependencies * ✨ Add Typer scripts to handle translations * ✨ Add missing translation snippet to include * ✨ Update contributing docs, add docs for translations * 🙈 Add docs_build to gitignore * 🔧 Update scripts with new locations and docs scripts * 👷 Update docs deploy action with translations * 📝 Add note about languages not supported in the theme * ✨ Add first translation, for Spanish
54 lines
1.7 KiB
Markdown
54 lines
1.7 KiB
Markdown
# Static Files
|
|
|
|
You can serve static files automatically from a directory using `StaticFiles`.
|
|
|
|
## Install `aiofiles`
|
|
|
|
First you need to install `aiofiles`:
|
|
|
|
<div class="termy">
|
|
|
|
```console
|
|
$ pip install aiofiles
|
|
|
|
---> 100%
|
|
```
|
|
|
|
</div>
|
|
|
|
## Use `StaticFiles`
|
|
|
|
* Import `StaticFiles`.
|
|
* "Mount" a `StaticFiles()` instance in a specific path.
|
|
|
|
```Python hl_lines="2 6"
|
|
{!../../../docs_src/static_files/tutorial001.py!}
|
|
```
|
|
|
|
!!! note "Technical Details"
|
|
You could also use `from starlette.staticfiles import StaticFiles`.
|
|
|
|
**FastAPI** provides the same `starlette.staticfiles` as `fastapi.staticfiles` just as a convenience for you, the developer. But it actually comes directly from Starlette.
|
|
|
|
### What is "Mounting"
|
|
|
|
"Mounting" means adding a complete "independent" application in a specific path, that then takes care of handling all the sub-paths.
|
|
|
|
This is different from using an `APIRouter` as a mounted application is completely independent. The OpenAPI and docs from your main application won't include anything from the mounted application, etc.
|
|
|
|
You can read more about this in the **Advanced User Guide**.
|
|
|
|
## Details
|
|
|
|
The first `"/static"` refers to the sub-path this "sub-application" will be "mounted" on. So, any path that starts with `"/static"` will be handled by it.
|
|
|
|
The `directory="static"` refers to the name of the directory that contains your static files.
|
|
|
|
The `name="static"` gives it a name that can be used internally by **FastAPI**.
|
|
|
|
All these parameters can be different than "`static`", adjust them with the needs and specific details of your own application.
|
|
|
|
## More info
|
|
|
|
For more details and options check <a href="https://www.starlette.io/staticfiles/" class="external-link" target="_blank">Starlette's docs about Static Files</a>.
|