Static Files - StaticFiles
¶
You can use the StaticFiles
class to serve static files, like JavaScript, CSS, images, etc.
Read more about it in the FastAPI docs for Static Files.
You can import it directly from fastapi.staticfiles
:
from fastapi.staticfiles import StaticFiles
fastapi.staticfiles.StaticFiles
¶
StaticFiles(*, <span data-autorefs-optional="fastapi.staticfiles.StaticFiles(directory)">directory</span>=None, <span data-autorefs-optional="fastapi.staticfiles.StaticFiles(packages)">packages</span>=None, <span data-autorefs-optional="fastapi.staticfiles.StaticFiles(html)">html</span>=False, <span data-autorefs-optional="fastapi.staticfiles.StaticFiles(check_dir)">check_dir</span>=True, <span data-autorefs-optional="fastapi.staticfiles.StaticFiles(follow_symlink)">follow_symlink</span>=False)
PARAMETER | DESCRIPTION |
---|---|
directory
|
TYPE:
|
packages
|
TYPE:
|
html
|
TYPE:
|
check_dir
|
TYPE:
|
follow_symlink
|
TYPE:
|
Source code in starlette/staticfiles.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
get_directories
¶
get_directories(<span data-autorefs-optional="fastapi.staticfiles.StaticFiles.get_directories(directory)">directory</span>=None, <span data-autorefs-optional="fastapi.staticfiles.StaticFiles.get_directories(packages)">packages</span>=None)
Given directory
and packages
arguments, return a list of all the
directories that should be used for serving static files from.
PARAMETER | DESCRIPTION |
---|---|
directory
|
TYPE:
|
packages
|
TYPE:
|
Source code in starlette/staticfiles.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
get_path
¶
get_path(<span data-autorefs-optional="fastapi.staticfiles.StaticFiles.get_path(scope)">scope</span>)
Given the ASGI scope, return the path
string to serve up,
with OS specific path separators, and any '..', '.' components removed.
PARAMETER | DESCRIPTION |
---|---|
scope
|
TYPE:
|
Source code in starlette/staticfiles.py
106 107 108 109 110 111 112 |
|
get_response
async
¶
get_response(<span data-autorefs-optional="fastapi.staticfiles.StaticFiles.get_response(path)">path</span>, <span data-autorefs-optional="fastapi.staticfiles.StaticFiles.get_response(scope)">scope</span>)
Returns an HTTP response, given the incoming path, method and request headers.
PARAMETER | DESCRIPTION |
---|---|
path
|
TYPE:
|
scope
|
TYPE:
|
Source code in starlette/staticfiles.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
lookup_path
¶
lookup_path(<span data-autorefs-optional="fastapi.staticfiles.StaticFiles.lookup_path(path)">path</span>)
PARAMETER | DESCRIPTION |
---|---|
path
|
TYPE:
|
Source code in starlette/staticfiles.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
file_response
¶
file_response(<span data-autorefs-optional="fastapi.staticfiles.StaticFiles.file_response(full_path)">full_path</span>, <span data-autorefs-optional="fastapi.staticfiles.StaticFiles.file_response(stat_result)">stat_result</span>, <span data-autorefs-optional="fastapi.staticfiles.StaticFiles.file_response(scope)">scope</span>, <span data-autorefs-optional="fastapi.staticfiles.StaticFiles.file_response(status_code)">status_code</span>=200)
PARAMETER | DESCRIPTION |
---|---|
full_path
|
TYPE:
|
stat_result
|
TYPE:
|
scope
|
TYPE:
|
status_code
|
TYPE:
|
Source code in starlette/staticfiles.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
check_config
async
¶
check_config()
Perform a one-off configuration check that StaticFiles is actually pointed at a directory, so that we can raise loud errors rather than just returning 404 responses.
Source code in starlette/staticfiles.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
is_not_modified
¶
is_not_modified(<span data-autorefs-optional="fastapi.staticfiles.StaticFiles.is_not_modified(response_headers)">response_headers</span>, <span data-autorefs-optional="fastapi.staticfiles.StaticFiles.is_not_modified(request_headers)">request_headers</span>)
Given the request and response headers, return True
if an HTTP
"Not Modified" response could be returned instead.
PARAMETER | DESCRIPTION |
---|---|
response_headers
|
TYPE:
|
request_headers
|
TYPE:
|
Source code in starlette/staticfiles.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|