<@ULVA73B9P> is there a utility library in the pre...
# marvin-in-the-wild
j
@Marvin is there a utility library in the prefect code base for "slugging" a string into a valid artifact key?
m
I couldn't find a dedicated utility library in the Prefect codebase for slugging a string into a valid artifact key. However, it seems the practice is to manually provide a string as an artifact key when creating an artifact, rather than relying on a specific function for slugging. You can specify a
key
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:
Copy code
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`:
Copy code
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:
Copy code
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.
n
Copy code
In [1]: from prefect.utilities.slugify import slugify

In [2]: slugify("At_First_Not_Valid")
Out[2]: 'at-first-not-valid'
j
thanks thanks!