Skip to content

prefect.cli.artifact

delete async

Delete an artifact.

Parameters:

Name Type Description Default
key Optional[str]

the key of the artifact to delete

Argument(None, help='The key of the artifact to delete.')

Examples:

$ prefect artifact delete "my-artifact"

Source code in prefect/cli/artifact.py
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
176
177
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
@artifact_app.command("delete")
async def delete(
    key: Optional[str] = typer.Argument(
        None, help="The key of the artifact to delete."
    ),
    artifact_id: Optional[str] = typer.Option(
        None, "--id", help="The ID of the artifact to delete."
    ),
):
    """
    Delete an artifact.

    Arguments:
        key: the key of the artifact to delete

    Examples:
        $ prefect artifact delete "my-artifact"
    """
    if key and artifact_id:
        exit_with_error("Please provide either a key or an artifact_id but not both.")

    async with get_client() as client:
        if artifact_id is not None:
            try:
                confirm_delete = typer.confirm(
                    (
                        "Are you sure you want to delete artifact with id"
                        f" {artifact_id!r}?"
                    ),
                    default=False,
                )
                if not confirm_delete:
                    exit_with_error("Deletion aborted.")

                await client.delete_artifact(artifact_id)
                exit_with_success(f"Deleted artifact with id {artifact_id!r}.")
            except ObjectNotFound:
                exit_with_error(f"Artifact with id {artifact_id!r} not found!")

        elif key is not None:
            artifacts = await client.read_artifacts(
                artifact_filter=ArtifactFilter(key=ArtifactFilterKey(any_=[key])),
            )
            if not artifacts:
                exit_with_error(
                    f"Artifact with key {key!r} not found. You can also specify an"
                    " artifact id with the --id flag."
                )

            confirm_delete = typer.confirm(
                (
                    f"Are you sure you want to delete {len(artifacts)} artifact(s) with"
                    f" key {key!r}?"
                ),
                default=False,
            )
            if not confirm_delete:
                exit_with_error("Deletion aborted.")

            for a in artifacts:
                await client.delete_artifact(a.id)

            exit_with_success(f"Deleted {len(artifacts)} artifact(s) with key {key!r}.")

        else:
            exit_with_error("Please provide a key or an artifact_id.")

inspect async

View details about an artifact.

Arguments:
    key: the key of the artifact to inspect

Examples:
    $ prefect artifact inspect "my-artifact"
   [
    {
        'id': 'ba1d67be-0bd7-452e-8110-247fe5e6d8cc',
        'created': '2023-03-21T21:40:09.895910+00:00',
        'updated': '2023-03-21T21:40:09.895910+00:00',
        'key': 'my-artifact',
        'type': 'markdown',
        'description': None,
        'data': 'my markdown',
        'metadata_': None,
        'flow_run_id': '8dc54b6f-6e24-4586-a05c-e98c6490cb98',
        'task_run_id': None
    },
    {
        'id': '57f235b5-2576-45a5-bd93-c829c2900966',
        'created': '2023-03-27T23:16:15.536434+00:00',
        'updated': '2023-03-27T23:16:15.536434+00:00',
        'key': 'my-artifact',
        'type': 'markdown',
        'description': 'my-artifact-description',
        'data': 'my markdown',
        'metadata_': None,
        'flow_run_id': 'ffa91051-f249-48c1-ae0f-4754fcb7eb29',
        'task_run_id': None
    }

]

Source code in prefect/cli/artifact.py
 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
138
@artifact_app.command("inspect")
async def inspect(
    key: str,
    limit: int = typer.Option(
        10,
        "--limit",
        help="The maximum number of artifacts to return.",
    ),
):
    """
        View details about an artifact.

        Arguments:
            key: the key of the artifact to inspect

        Examples:
            $ prefect artifact inspect "my-artifact"
           [
            {
                'id': 'ba1d67be-0bd7-452e-8110-247fe5e6d8cc',
                'created': '2023-03-21T21:40:09.895910+00:00',
                'updated': '2023-03-21T21:40:09.895910+00:00',
                'key': 'my-artifact',
                'type': 'markdown',
                'description': None,
                'data': 'my markdown',
                'metadata_': None,
                'flow_run_id': '8dc54b6f-6e24-4586-a05c-e98c6490cb98',
                'task_run_id': None
            },
            {
                'id': '57f235b5-2576-45a5-bd93-c829c2900966',
                'created': '2023-03-27T23:16:15.536434+00:00',
                'updated': '2023-03-27T23:16:15.536434+00:00',
                'key': 'my-artifact',
                'type': 'markdown',
                'description': 'my-artifact-description',
                'data': 'my markdown',
                'metadata_': None,
                'flow_run_id': 'ffa91051-f249-48c1-ae0f-4754fcb7eb29',
                'task_run_id': None
            }
    ]
    """

    async with get_client() as client:
        artifacts = await client.read_artifacts(
            limit=limit,
            sort=ArtifactSort.UPDATED_DESC,
            artifact_filter=ArtifactFilter(key=ArtifactFilterKey(any_=[key])),
        )
        if not artifacts:
            exit_with_error(f"Artifact {key!r} not found.")

        artifacts = [a.dict(json_compatible=True) for a in artifacts]

        app.console.print(Pretty(artifacts))

list_artifacts async

List artifacts.

Source code in prefect/cli/artifact.py
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
@artifact_app.command("ls")
async def list_artifacts(
    limit: int = typer.Option(
        100,
        "--limit",
        help="The maximum number of artifacts to return.",
    ),
    all: bool = typer.Option(
        False,
        "--all",
        "-a",
        help="Whether or not to only return the latest version of each artifact.",
    ),
):
    """
    List artifacts.
    """
    table = Table(
        title="Artifacts",
        caption="List Artifacts using `prefect artifact ls`",
        show_header=True,
    )

    table.add_column("ID", justify="right", style="cyan", no_wrap=True)
    table.add_column("Key", style="blue", no_wrap=True)
    table.add_column("Type", style="blue", no_wrap=True)
    table.add_column("Updated", style="blue", no_wrap=True)

    async with get_client() as client:
        if all:
            artifacts = await client.read_artifacts(
                sort=ArtifactSort.KEY_ASC,
                limit=limit,
            )

            for artifact in sorted(artifacts, key=lambda x: f"{x.key}"):
                table.add_row(
                    str(artifact.id),
                    artifact.key,
                    artifact.type,
                    pendulum.instance(artifact.updated).diff_for_humans(),
                )

        else:
            artifacts = await client.read_latest_artifacts(
                sort=ArtifactCollectionSort.KEY_ASC,
                limit=limit,
            )

            for artifact in sorted(artifacts, key=lambda x: f"{x.key}"):
                table.add_row(
                    str(artifact.latest_id),
                    artifact.key,
                    artifact.type,
                    pendulum.instance(artifact.updated).diff_for_humans(),
                )

        app.console.print(table)