Execution Parameters¶
When you run a notebook as a job in ML Workbench, you often want to run the same code with different settings, e.g. another country, threshold, or date. The @parameters decorator lets you declare these settings once, in a plain Python class:
- When you run the notebook interactively, the default values from the class are used.
- When the notebook runs as a triggered job or triggered transformation, the execution parameters passed with the trigger replace the defaults.
So the same notebook works for testing and for production without any code changes.
Scheduled runs and deployments do not receive execution parameters, so they always use the defaults.
Prerequisites¶
- This tutorial is intended to run in ML Workbench.
- No extra packages are required;
@parametersis part ofpycelonis.
from pycelonis.ml_workbench.utils.parameters import parameters
@parameters
class Params:
country: str = "DE"
threshold: float = 0.5
max_rows: int = 1000
send_report: bool = False
columns: list = ["order_id", "amount"]
options: dict = {"mode": "fast"}
The decorator turns the class into a dataclass and returns a ready-to-use instance, so you can read the values directly. You do not need to call Params():
print(Params.country)
print(Params.threshold)
print(Params.columns)
print(Params)
2. Rules for the class¶
Keep the class simple:
| Rule | Example |
|---|---|
| Every parameter needs a type annotation. | country: str = "DE" |
| Every parameter needs a default value. It is used when you run the notebook interactively. | max_rows: int = 1000 |
Supported types are str, int, float, bool, list, and dict. Use the plain types, not List[str] or Optional[str]. |
columns: list = ["a", "b"] |
Lists and dicts can be written directly as defaults; there is no need for dataclasses.field(default_factory=...). |
options: dict = {"mode": "fast"} |
| Methods are not treated as parameters. | def describe(self): ... |
Every annotated attribute is a parameter, even if its name starts with _. Attributes without a type annotation are only allowed if their name starts with _. |
_cache = None |
Unsupported types are only detected when a job sets the parameter, so an interactive run does not catch them. Stick to the plain types above.
A parameter without a type annotation is rejected straight away:
@parameters
class BrokenParams:
country = "DE" # missing type annotation
3. Use the parameters in your code¶
Use the instance like any other object:
for column in Params.columns:
print(f"Processing column '{column}' for country {Params.country}")
if Params.send_report:
print("Sending report...")
else:
print("Report disabled")
4. Run the notebook as a job¶
When you trigger the notebook as a job or transformation, pass its execution parameters as a JSON object (at most 256 KB) whose keys match the field names, for example:
{
"country": "US",
"threshold": 0.8,
"send_report": true
}
ML Workbench provides these values to the triggered run, and @parameters applies them when the class is decorated:
- Values are checked against the type annotation, so
"threshold": "high"fails with aTypeErrorinstead of silently running with the wrong value. Types must match exactly: write1.0(not1) for afloat, andtrue/falseis only accepted for abool. - Parameters you do not set (
max_rows,columns, andoptionsabove) keep their defaults; a warning lists them. - Unknown keys are ignored with a warning.
If you need to know whether the values came from a job or from the defaults, use is_externally_set:
from pycelonis.ml_workbench.utils.parameters import is_externally_set
if is_externally_set(Params):
print("Running as a job with configured execution parameters")
else:
print("Running interactively with default values")
Conclusion¶
You have learned how to declare execution parameters with @parameters, which types are supported, and how the defaults are replaced by the execution parameters of a triggered job or transformation.