prefect.variables
Variable
Bases: VariableCreate
Variables are named, mutable string values, much like environment variables. Variables are scoped to a Prefect server instance or a single workspace in Prefect Cloud. https://docs.prefect.io/latest/concepts/variables/
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
A string identifying the variable. |
required | |
value |
A string that is the value of the variable. |
required | |
tags |
An optional list of strings to associate with the variable. |
required |
Source code in src/prefect/variables.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 86 87 88 89 90 91 92 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 |
|
get(name, default=None, as_object=False)
async
classmethod
Get a variable's value by name.
If the variable does not exist, return the default value.
If as_object=True
, return the full variable object. default
is ignored in this case.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
- |
name
|
The name of the variable to get. |
required |
- |
default
|
The default value to return if the variable does not exist. |
required |
- |
as_object
|
Whether to return the full variable object. |
required |
Example
Get a variable's value by name.
from prefect import flow
from prefect.variables import Variable
@flow
def my_flow():
var = Variable.get("my_var")
Source code in src/prefect/variables.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
set(name, value, tags=None, overwrite=False, as_object=False)
async
classmethod
Sets a new variable. If one exists with the same name, must pass overwrite=True
Returns the newly set value. If as_object=True
, return the full Variable object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
- |
name
|
The name of the variable to set. |
required |
- |
value
|
The value of the variable to set. |
required |
- |
tags
|
An optional list of strings to associate with the variable. |
required |
- |
overwrite
|
Whether to overwrite the variable if it already exists. |
required |
- |
as_object
|
Whether to return the full Variable object. |
required |
Example
Set a new variable and overwrite it if it already exists.
from prefect.variables import Variable
@flow
def my_flow():
Variable.set(name="my_var",value="test_value", tags=["hi", "there"], overwrite=True)
Source code in src/prefect/variables.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
unset(name)
async
classmethod
Unset a variable by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
- |
name
|
The name of the variable to unset. |
required |
Returns True
if the variable was deleted, False
if the variable did not exist.
Example
Unset a variable by name.
from prefect import flow
from prefect.variables import Variable
@flow
def my_flow():
Variable.unset("my_var")
Source code in src/prefect/variables.py
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 |
|