Skip to main content

run-script

In this section, you will find a reference for a Declarative Hook of type run-script.


Declarative hooks of type run-script are used to run Python scripts inside a plugin.yaml or template.yaml file. See the following example of a declarative hook of type run-script:

hooks:
- type: run-script
trigger: before-render
script: path/to/script.py

The script attribute defines the path to the script in the Template or Plugin file structure.

caution

The script to be executed must be outside the templates folder of the Plugin or Template to avoid being interpolated.

Typically, this script defines a function named run. This function receives an object of the Metadata class from the template framework and must return a Metadata object, which can be the same instance or a new one created by the function.

Metadata

When run-script is executed and exported using a Metadata class, you may see a structure like the one in the example below:

from templateframework.metadata import Metadata

def run(metadata: Metadata = None):
all_inputs = metadata.all_inputs()
inputs_local = metadata.inputs
inputs_global = metadata.global_inputs
inputs_envs = metadata.inputs_envs
inputs_computed = metadata.computed_inputs
inputs_global_computed = metadata.global_computed_inputs

Below is the description of each item in the class:

  • all_inputs = metadata.all_inputs(): Returns all processed inputs of the Plugin or Template.
  • inputs_local = metadata.inputs: Dictionary of inputs.
  • inputs_global = metadata.global_inputs: Dictionary of global inputs.
  • inputs_envs = metadata.inputs_envs: Dictionary of environment inputs.
  • inputs_computed = metadata.computed_inputs: Dictionary of computed inputs.
  • inputs_global_computed = metadata.global_computed_inputs: Dictionary of global computed inputs.

The following is a simple example of a script definition, which can be executed as a Declarative Hook:

from templateframework.metadata import Metadata

def run(metadata: Metadata = None):
print("Hello from script.py!")
return metadata

In the script, you can use the Python 3.8 standard library and extra libraries that are dependencies of STK CLI (StackSpot Command Line Interface). Below are some of them:

Available Actions

trigger:

Field used to define triggers that specify when the execution of a Python script should occur.

before-input:
Runs the script before receiving the input parameters from the user.

trigger: before-input

before-render:
Runs the script before the Template generates files in the project.

trigger: before-render

after-render:
Runs the script after the Template generates files in the project.

trigger: after-render

script:

Defines the path of the Python script that will be executed.

script: path/to/script.py
caution

The script to be executed must be outside the templates folder of the Plugin or Template to avoid being interpolated.


For run-script to execute a Python script, the script must be defined with:

  • A function named run.
  • The run function must receive an object from the Metadata class of the templateframework as a parameter.
  • On return, the function must return a Metadata object. It can be the same object received or another one created by the function.

Example of run function in a Python script

from templateframework.metadata import Metadata

def run(metadata: Metadata = None):
print("Hello from script.py!")
return metadata

In the script, you can use the Python 3.8 standard library and the Requests and Questionary libraries, which are available as dependencies in STK CLI.