https://dagster.io/ logo
#ask-community
Title
# ask-community
l

Lilian GARCIA PAILLE

06/15/2023, 12:33 PM
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

Nicolas Guary

06/15/2023, 2:01 PM
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

owen

06/15/2023, 4:54 PM
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

Lilian GARCIA PAILLE

06/16/2023, 8:06 AM
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

owen

06/16/2023, 8:16 PM
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
14 Views