Hi! I am using a job that launch several assets wh...
# ask-community
l
Hi! I am using a job that launch several assets which are almost the same. The thing that change between them is that I have written in hard-code a variable inside. I'd like to know if there is a way to create "dynamically" each asset with, for each, a diferent name giving them in dependancy that variable without hardcode it. Thank you :)
n
If your asset names can be created using prefixes, you can do something like :
Copy code
def create_assets(your_config: dict) -> AssetsDefinition:
    # Parse your config to get your previously hard-coded variables
    @asset(
        group_name=???,
        compute_kind="???",
        io_manager_key="???",
        key_prefix=f"???" #get from config,
    )
    def your_asset():
        return your_asset
o
the above is totally correct, and for cases where you want to change the asset keys completely (not just varying the prefix), you can always do
Copy code
def create_assets(your_config: dict) -> AssetsDefinition:
    # Parse your config to get your previously hard-coded variables
    @asset(
        group_name=???,
        compute_kind="???",
        io_manager_key="???",
        name="???", #set the name of the asset
    )
    def your_asset():
        ...
    return your_asset
🙌 1
l
Unfortunately, it doesn't work for me. Even if I call the function that creates an asset, it isn't lauched when I start my job.
I think that's because I import my assets with load_assets_from_package_module.
It works if I launch my function at the root of my project and I give the result to the parameter "selection" of my job.
Thank you 🙂
o
ah I see what you mean, I think another way to make it work with load_assets_from_package_module would be:
Copy code
def create_assets(your_config: dict):
    ...

assets = create_assets(...)
load_assets_from_package_module
is looking for variables in the module that are a specific type (AssetsDefinition or list of AssetsDefinition), so I believe defining it in this way would allow those assets to be noticed
👍 1