Hi, I want the hooks to only be executed when `env...
# questions
a
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
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
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
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
Thanks, I'll test it!