prefect.utilities.templating
apply_values(template, values, remove_notset=True)
Replaces placeholders in a template with values from a supplied dictionary.
Will recursively replace placeholders in dictionaries and lists.
If a value has no placeholders, it will be returned unchanged.
If a template contains only a single placeholder, the placeholder will be fully replaced with the value.
If a template contains text before or after a placeholder or there are multiple placeholders, the placeholders will be replaced with the corresponding variable values.
If a template contains a placeholder that is not in values
, NotSet will
be returned to signify that no placeholder replacement occurred. If
template
is a dictionary that contains a key with a value of NotSet,
the key will be removed in the return value unless remove_notset
is set to False.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template
|
T
|
template to discover and replace values in |
required |
values
|
Dict[str, Any]
|
The values to apply to placeholders in the template |
required |
remove_notset
|
bool
|
If True, remove keys with an unset value |
True
|
Returns:
Type | Description |
---|---|
Union[T, Type[NotSet]]
|
The template with the values applied |
Source code in src/prefect/utilities/templating.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
|
determine_placeholder_type(name)
Determines the type of a placeholder based on its name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the placeholder |
required |
Returns:
Type | Description |
---|---|
PlaceholderType
|
The type of the placeholder |
Source code in src/prefect/utilities/templating.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
find_placeholders(template)
Finds all placeholders in a template.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template
|
T
|
template to discover placeholders in |
required |
Returns:
Type | Description |
---|---|
Set[Placeholder]
|
A set of all placeholders in the template |
Source code in src/prefect/utilities/templating.py
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 |
|
resolve_block_document_references(template, client=None)
async
Resolve block document references in a template by replacing each reference with the data of the block document.
Recursively searches for block document references in dictionaries and lists.
Identifies block document references by the as dictionary with the following structure:
{
"$ref": {
"block_document_id": <block_document_id>
}
}
where <block_document_id>
is the ID of the block document to resolve.
Once the block document is retrieved from the API, the data of the block document is used to replace the reference.
Accessing Values:
To access different values in a block document, use dot notation combined with the block document's prefix, slug, and block name.
For a block document with the structure:
{
"value": {
"key": {
"nested-key": "nested-value"
},
"list": [
{"list-key": "list-value"},
1,
2
]
}
}
examples of value resolution are as follows:
-
Accessing a nested dictionary: Format: prefect.blocks.
. .value.key Example: Returns {"nested-key": "nested-value"} -
Accessing a specific nested value: Format: prefect.blocks.
. .value.key.nested-key Example: Returns "nested-value" -
Accessing a list element's key-value: Format: prefect.blocks.
. .value.list[0].list-key Example: Returns "list-value"
Default Resolution for System Blocks:
For system blocks, which only contain a value
attribute, this attribute is resolved by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template
|
T
|
The template to resolve block documents in |
required |
Returns:
Type | Description |
---|---|
Union[T, Dict[str, Any]]
|
The template with block documents resolved |
Source code in src/prefect/utilities/templating.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|
resolve_variables(template, client=None)
async
Resolve variables in a template by replacing each variable placeholder with the value of the variable.
Recursively searches for variable placeholders in dictionaries and lists.
Strips variable placeholders if the variable is not found.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template
|
T
|
The template to resolve variables in |
required |
Returns:
Type | Description |
---|---|
The template with variables resolved |
Source code in src/prefect/utilities/templating.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
|