James Barkley
04/16/2024, 6:12 PMMarvin
04/16/2024, 6:13 PMkey
when using the create_link_artifact()
function within Prefect to create artifacts with a meaningful key for tracking purposes. Here's an example of its usage:
python
from prefect import flow, task
from prefect.artifacts import create_link_artifact
@task
def my_task():
# ... task logic
create_link_artifact(
key="your-artifact-key", # <-- This is where you put the slugged key
link="<https://link.to/your/artifact>",
description="Description of the artifact"
)
@flow
def my_flow():
my_task()
if __name__ == "__main__":
my_flow()
For more information, you may refer to the Artifacts documentation.
It's important to create the key manually and ensure that it conforms to any rules or constraints you might have for valid artifact keys. This often involves slugging it – converting it to lowercase, replacing spaces with hyphens, and removing invalid characters.
If there's an internal utility function used by Prefect for slugging, it is not documented in the public API or easily discoverable through GitHub issues. Therefore, I recommend using a custom slugging function or a widely-used library like python-slugify
to generate slugged strings for artifact keys. Here's an example with `python-slugify`:
python
from slugify import slugify
# Usage
valid_artifact_key = slugify("Your String Here")
You would need to install the python-slugify
library using pip
to use this:
shell
pip install python-slugify
Keep in mind that while it might be slightly depressing that there isn't a built-in Prefect utility for this, it's just one of those things you'll have to manage yourself. After all, life – don't talk to me about life.Nate
04/16/2024, 6:28 PMIn [1]: from prefect.utilities.slugify import slugify
In [2]: slugify("At_First_Not_Valid")
Out[2]: 'at-first-not-valid'
James Barkley
04/16/2024, 6:29 PM