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(
*,
directory=None,
packages=None,
html=False,
check_dir=True,
follow_symlink=False
)
PARAMETER | DESCRIPTION |
---|---|
directory
|
TYPE:
|
packages
|
TYPE:
|
html
|
TYPE:
|
check_dir
|
TYPE:
|
follow_symlink
|
TYPE:
|
Source code in starlette/staticfiles.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
get_directories
¶
get_directories(directory=None, packages=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
58 59 60 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 |
|
get_path
¶
get_path(scope)
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
101 102 103 104 105 106 107 |
|
get_response
async
¶
get_response(path, scope)
Returns an HTTP response, given the incoming path, method and request headers.
PARAMETER | DESCRIPTION |
---|---|
path
|
TYPE:
|
scope
|
TYPE:
|
Source code in starlette/staticfiles.py
109 110 111 112 113 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 |
|
lookup_path
¶
lookup_path(path)
PARAMETER | DESCRIPTION |
---|---|
path
|
TYPE:
|
Source code in starlette/staticfiles.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
file_response
¶
file_response(
full_path, stat_result, scope, status_code=200
)
PARAMETER | DESCRIPTION |
---|---|
full_path
|
TYPE:
|
stat_result
|
TYPE:
|
scope
|
TYPE:
|
status_code
|
TYPE:
|
Source code in starlette/staticfiles.py
169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
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
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
is_not_modified
¶
is_not_modified(response_headers, request_headers)
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
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|