elyra_engine.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #
  2. # Copyright 2018-2022 Elyra Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. """Papermill Engine that configures a KernelManager to hit a Gateway Server."""
  17. from papermill.clientwrap import PapermillNotebookClient
  18. from papermill.engines import NBClientEngine
  19. from papermill.log import logger
  20. from papermill.utils import merge_kwargs
  21. from papermill.utils import remove_args
  22. class ElyraEngine(NBClientEngine):
  23. """
  24. A notebook engine representing an nbclient process that runs local pipeline notebooks.
  25. This can execute a notebook document and update the `nb_man.nb` object with the results.
  26. """
  27. @classmethod
  28. def execute_managed_notebook(
  29. cls,
  30. nb_man,
  31. kernel_name,
  32. log_output=False,
  33. stdout_file=None,
  34. stderr_file=None,
  35. start_timeout=60,
  36. execution_timeout=None,
  37. **kwargs,
  38. ):
  39. """
  40. Performs the actual execution of the parameterized notebook. Note that kwargs may
  41. specify an alternate 'kernel_manager_class' for nbclient to use, and 'kernel_env'
  42. and 'kernel_cwd' to pass to the kernel process's environment.
  43. Args:
  44. nb (NotebookNode): Executable notebook object.
  45. kernel_name (str): Name of kernel to execute the notebook against.
  46. log_output (bool): Flag for whether or not to write notebook output to the
  47. configured logger.
  48. startup_timeout (int): Duration to wait for kernel start-up.
  49. execution_timeout (int): Duration to wait before failing execution (default: never).
  50. kernel_env (dict): Passed as the kernel 'env' parameter to the execute() method
  51. kernel_cwd (str): Passed as the kernel 'cwd' parameter to the execute() method
  52. kernel_manager_class: (str) If set, specifies the use of an alternate kernel manager.
  53. """
  54. # Exclude parameters that named differently downstream
  55. safe_kwargs = remove_args(["timeout", "startup_timeout", "kernel_env", "kernel_cwd"], **kwargs)
  56. # Nicely handle preprocessor arguments prioritizing values set by engine
  57. final_kwargs = merge_kwargs(
  58. safe_kwargs,
  59. timeout=execution_timeout or kwargs.get("timeout"),
  60. startup_timeout=start_timeout,
  61. kernel_name=kernel_name,
  62. log=logger,
  63. log_output=log_output,
  64. stdout_file=stdout_file,
  65. stderr_file=stderr_file,
  66. )
  67. kernel_kwargs = {"env": kwargs.get("kernel_env")}
  68. # Only include kernel_name and set path if GatewayKernelManager will be used
  69. kernel_manager_class = final_kwargs.get("kernel_manager_class")
  70. if kernel_manager_class == "jupyter_server.gateway.managers.GatewayKernelManager":
  71. kernel_kwargs["kernel_name"] = kernel_name
  72. kernel_kwargs["path"] = kwargs.get("kernel_cwd")
  73. return PapermillNotebookClient(nb_man, **final_kwargs).execute(**kernel_kwargs)