kedro run is able to run the hooks but when i am t...
# questions
v
kedro run is able to run the hooks but when i am trying to hit it using the api, it is able to run the nodes but not hooks, using kedro version 0.17.7, __ ___main.py_ __ code --
Copy code
import os

from kedro.framework.session import KedroSession
from kedro.framework.startup import bootstrap_project
from kedro.runner import SequentialRunner
from hooks import ControlTableHooks

if __name__ == "__main__":
    bootstrap_project(os.path.abspath(os.environ.get("PROJECT_PATH")))
    os.chdir(os.environ.get("PROJECT_PATH"))
    with KedroSession.create(env=os.environ.get("kedro_environment")) as session:
        runner = SequentialRunner()
        context = session.load_context()
        pipeline = context.pipelines[os.environ.get("pipeline_name")]
        catalog = context.catalog
        runner.run(pipeline, catalog)
        result_dict = {"message": "Success"}
any help
j
hi @Vandana Malik! I guess this is a continuation of your earlier query https://kedro-org.slack.com/archives/C03RKP2LW64/p1683020049388939 any reason you're manually instantiating the runner, context etc instead of
Copy code
metadata = bootstrap_project(Path.cwd())
with KedroSession.create(metadata.package_name) as session:
    session.run()
? (as offered in https://docs.kedro.org/en/0.17.7/04_kedro_project_setup/03_session.html)
v
can you please suggest me how to pass hooks in that.. as i am unable to see them running using this
found something, debugging it
@Juan Luis - thanks
j
good to know! what did you end up doing?
v
i ended up creating a sessioncontext inside projectContext and passing hooks as a param in it.. now i can see it hitting the hooks..
🥳 1
j
good to know it's working for you! just one question @Vandana Malik - you mentioned you're using Kedro 0.17.7, and
ProjectContext
was removed in 0.17.0 - could you share a code snippet to better understand your solution?
v
Copy code
class ProjectContext(KedroContext):
    def __init__(
        self,
        package_name: str,
        project_path: Union[Path, str],
        env: str = None,
        extra_params: Dict[str, Any] = None,
    ):
        """Init class."""
        super().__init__(
            package_name, project_path, env, extra_params,
        )
    def create_session(self, *args, **kwargs):
        hooks = [ControlTableHooks()]
        return super().create_session(*args, hooks=hooks, **kwargs)
like that i have done
n
Where do you get the
create_session
function? It doesn’t belong to the KedroContext API IIRC
j
@Vandana Malik I can't find any
create_session
method so I suppose this is non-public code. I'm still interested in understanding your solution but let's refrain from sharing more code here just in case 🙂
(happy to take this to private)