Skip to content

prefect.cli.variable

get(name) async

Get a variable's value.

Parameters:

Name Type Description Default
name str

the name of the variable to get

required
Source code in src/prefect/cli/variable.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
@variable_app.command("get")
async def get(
    name: str,
):
    """
    Get a variable's value.

    Arguments:
        name: the name of the variable to get
    """

    async with get_client() as client:
        variable = await client.read_variable_by_name(
            name=name,
        )
        if variable:
            app.console.print(json.dumps(variable.value))
        else:
            exit_with_error(f"Variable {name!r} not found.")

inspect(name) async

View details about a variable.

Parameters:

Name Type Description Default
name str

the name of the variable to inspect

required
Source code in src/prefect/cli/variable.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@variable_app.command("inspect")
async def inspect(
    name: str,
):
    """
    View details about a variable.

    Arguments:
        name: the name of the variable to inspect
    """

    async with get_client() as client:
        variable = await client.read_variable_by_name(
            name=name,
        )
        if not variable:
            exit_with_error(f"Variable {name!r} not found.")

        app.console.print(Pretty(variable))

list_variables(limit=typer.Option(100, '--limit', help='The maximum number of variables to return.')) async

List variables.

Source code in src/prefect/cli/variable.py
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
@variable_app.command("ls")
async def list_variables(
    limit: int = typer.Option(
        100,
        "--limit",
        help="The maximum number of variables to return.",
    ),
):
    """
    List variables.
    """
    async with get_client() as client:
        variables = await client.read_variables(
            limit=limit,
        )

        table = Table(
            title="Variables",
            caption="List Variables using `prefect variable ls`",
            show_header=True,
        )

        table.add_column("Name", style="blue", no_wrap=True)
        # values can be up 5000 characters so truncate early
        table.add_column("Value", style="blue", no_wrap=True, max_width=50)
        table.add_column("Created", style="blue", no_wrap=True)
        table.add_column("Updated", style="blue", no_wrap=True)

        for variable in sorted(variables, key=lambda x: f"{x.name}"):
            table.add_row(
                variable.name,
                json.dumps(variable.value),
                pendulum.instance(variable.created).diff_for_humans(),
                pendulum.instance(variable.updated).diff_for_humans(),
            )

        app.console.print(table)

unset(name) async

Unset a variable.

Parameters:

Name Type Description Default
name str

the name of the variable to unset

required
Source code in src/prefect/cli/variable.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
@variable_app.command("unset", aliases=["delete"])
async def unset(
    name: str,
):
    """
    Unset a variable.

    Arguments:
        name: the name of the variable to unset
    """

    async with get_client() as client:
        try:
            if is_interactive() and not typer.confirm(
                f"Are you sure you want to unset variable {name!r}?"
            ):
                exit_with_error("Unset aborted.")
            await client.delete_variable_by_name(
                name=name,
            )
        except ObjectNotFound:
            exit_with_error(f"Variable {name!r} not found.")

        exit_with_success(f"Unset variable {name!r}.")