Static Files -StaticFiles¶
You can use theStaticFiles class to serve static files, like JavaScript, CSS, images, etc.
Read more about it in theFastAPI docs for Static Files.
You can import it directly fromfastapi.staticfiles:
fromfastapi.staticfilesimportStaticFilesfastapi.staticfiles.StaticFiles¶
StaticFiles(*,directory=None,packages=None,html=False,check_dir=True,follow_symlink=False)Source code instarlette/staticfiles.py
4041424344454647484950515253545556 | |
directoryinstance-attribute¶
directory=directorypackagesinstance-attribute¶
packages=packagesall_directoriesinstance-attribute¶
all_directories=get_directories(directory,packages)htmlinstance-attribute¶
html=htmlconfig_checkedinstance-attribute¶
config_checked=Falsefollow_symlinkinstance-attribute¶
follow_symlink=follow_symlinkget_directories¶
get_directories(directory=None,packages=None)Givendirectory andpackages arguments, return a list of all thedirectories that should be used for serving static files from.
Source code instarlette/staticfiles.py
58596061626364656667686970717273747576777879808182838485 | |
get_path¶
get_path(scope)Given the ASGI scope, return thepath string to serve up,with OS specific path separators, and any '..', '.' components removed.
Source code instarlette/staticfiles.py
101102103104105106107 | |
get_responseasync¶
get_response(path,scope)Returns an HTTP response, given the incoming path, method and request headers.
Source code instarlette/staticfiles.py
109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 | |
lookup_path¶
lookup_path(path)Source code instarlette/staticfiles.py
151152153154155156157158159160161162163164165166167 | |
file_response¶
file_response(full_path,stat_result,scope,status_code=200)Source code instarlette/staticfiles.py
169170171172173174175176177178179180181 | |
check_configasync¶
check_config()Perform a one-off configuration check that StaticFiles is actuallypointed at a directory, so that we can raise loud errors rather thanjust returning 404 responses.
Source code instarlette/staticfiles.py
183184185186187188189190191192193194195196197 | |
is_not_modified¶
is_not_modified(response_headers,request_headers)Given the request and response headers, returnTrue if an HTTP"Not Modified" response could be returned instead.
Source code instarlette/staticfiles.py
199200201202203204205206207208209210211212213214215216217 | |







