https://kedro.org/ logo
#questions
Title
# questions
a

André Vinícius Mendes Barros

10/06/2023, 6:36 PM
Hi, I want the hooks to only be executed when
env=local
, how do I solve it? I tried to select the KedroContext env property but it didn't work...
n

Nok Lam Chan

10/06/2023, 6:55 PM
can you show your implementation? The after_context_created hook has env as an argument and you can add your logic conditioning on that
a

André Vinícius Mendes Barros

10/07/2023, 12:04 PM
I tried:
Copy code
# src/<package_name>/hooks.py
from kedro.framework.hooks import hook_impl
from kedro.io import DataCatalog

class DataCatalogHooks:
    @hook_impl
    def after_catalog_created(self, catalog: DataCatalog) -> None:
        print('Testing...')
Copy code
# src/<package_name>/settings.py
from pathlib import Path
from <package_name>.hooks import ProjectHooks, DataCatalogHooks
from kedro.framework.session import KedroSession

project_path = Path.cwd()
session = KedroSession.create(project_path=project_path)
context = session.load_context()
if context.env == 'local':
    HOOKS = (DataCatalogHooks())
But
context.env=null
...
n

Nok Lam Chan

10/07/2023, 4:29 PM
You shouldn't create context yourself, because this isn't the same context being used in the run. Instead of conditionally create hook, you can create hook that execute logic with condition.
a

André Vinícius Mendes Barros

10/09/2023, 11:47 AM
Thanks, I'll test it!