7
7
.. tutorial ::
8
8
Configuration Consolidation Tutorial <tutorial-cylc-consolidating-configuration>
9
9
10
- Cylc supports use of the `Jinja2 `_ template processor in workflow
11
- configurations. Jinja2 variables, expressions, loop control structures,
12
- conditional logic, etc., are processed to generate the final configuration. To
13
- the template processor, Jinja2 code is embedded in arbitrary text, but the
14
- result after processing must be valid Cylc syntax.
10
+ Cylc supports the `Jinja2 `_ template processor in workflow configurations.
11
+ Jinja2 code can appear anywhere in the file. The result after Jinja2 processing
12
+ must be valid Cylc syntax.
15
13
16
14
To use Jinja2, put a hash-bang comment in the first line of :cylc:conf: `flow.cylc `:
17
15
18
16
.. code-block :: cylc
19
17
20
18
#!jinja2
21
19
22
- Template processing is the first thing done on parsing a workflow configuration
23
- so Jinja2 can appear anywhere in the file.
24
-
25
20
Embedded Jinja2 code should be reasonably easy to understand for those with
26
- coding experience; but if not, Jinja2 is well documented `here
27
- <Jinja2_> `_.
21
+ coding experience; otherwise Jinja2 is documented `here <Jinja2 _>`_.
28
22
29
23
Uses of Jinja2 in Cylc include:
30
24
31
25
- Inclusion or exclusion of config sections by logical switch, e.g. to make
32
26
portable workflows
33
- - Computation of config values from in input data
27
+ - Computation of config values from input data
34
28
- Inclusion of files and sub-templates
35
- - Loop over parameters to generate groups of similar tasks and associated
29
+ - Looping over parameters to generate groups of similar tasks and associated
36
30
dependencies - but see :ref: `Parameterized Tasks <User Guide Param >` for a
37
- simpler alternative to this use case
31
+ simpler alternative to this where appropriate
38
32
39
33
.. _fig-jinja2-ensemble :
40
34
@@ -99,7 +93,7 @@ to generate task names automatically with built-in
99
93
R1 = "foo => mem<m> => post<m> => bar"
100
94
101
95
102
- The next example , which generates weather forecasts over a number of cities, is
96
+ The next workflow , which generates weather forecasts over a number of cities, is
103
97
more complex. To add a new city and associated tasks and dependencies just add
104
98
the new city name to list at the top of the file. It makes use of Jinja2
105
99
variables, loops, math, and logical flags to include or exclude tasks.
@@ -120,54 +114,83 @@ variables, loops, math, and logical flags to include or exclude tasks.
120
114
New York City task family expanded.
121
115
122
116
123
- Accessing Environment Variables
124
- -------------------------------
117
+ .. _jinja2.workflow_files :
125
118
126
- Cylc automatically imports the environment to the template's global namespace
127
- (see :ref: ` CustomJinja2Filters `) in a dictionary called `` environ ``:
119
+ Access to Workflow Files
120
+ ------------------------
128
121
129
- .. code-block :: cylc
122
+ Your Jinja2 code can see the workflow directory by using
123
+ :ref: `Python code <jinja2.importing_python_modules >`
124
+ that simply reads from the *current working directory *.
130
125
131
- #!Jinja2
132
- #...
133
- [runtime]
134
- [[root]]
135
- [[[environment]]]
136
- WORKFLOW_OWNER_HOME_DIR_ON_WORKFLOW_HOST = {{environ['HOME']}}
126
+ This will be the source directory if parsing a source workflow, or the run
127
+ directory if parsing an installed workflow.
137
128
138
- In addition, the following variables are exported to this environment
139
- (hence are available in the ``environ `` dict) to provide workflow context:
140
129
141
- .. code-block :: sub
130
+ .. _ jinja2.workflow_context :
142
131
143
- CYLC_VERBOSE # Verbose mode, true or false
144
- CYLC_DEBUG # Debug mode (even more verbose), true or false
132
+ Workflow Context variables
133
+ --------------------------
145
134
146
- CYLC_WORKFLOW_ID # Workflow ID
147
- CYLC_WORKFLOW_NAME # Workflow name
148
- # (the ID with the run name removed)
135
+ Jinja2 CYLC variables available when parsing any workflow (source or installed):
149
136
150
- CYLC_WORKFLOW_LOG_DIR # Workflow log directory.
151
- CYLC_WORKFLOW_RUN_DIR # Location of the run directory in
152
- # workflow host, e.g. ~/cylc-run/foo
153
- CYLC_WORKFLOW_SHARE_DIR # Workflow (or task post parsing!)
154
- # shared directory.
155
- CYLC_WORKFLOW_WORK_DIR # Workflow work directory.
137
+ .. table ::
156
138
157
- .. warning ::
139
+ ====================== ==============
140
+ CYLC_VERSION Version of Cylc parsing the configuration
141
+ CYLC_WORKFLOW_NAME Workflow name (source, or run ID minus run name)
142
+ CYLC_TEMPLATE_VARS Variables set by '--set' CLI options or plugins
143
+ ====================== ==============
144
+
145
+
146
+ Additional Jinja2 CYLC variables available when parsing an installed workflow:
147
+
148
+ .. table ::
158
149
159
- The environment is read on the workflow host when the configuration is
160
- parsed. It is not read at run time by jobs on the job platform.
150
+ ====================== ==============
151
+ CYLC_WORKFLOW_ID Workflow ID
152
+ CYLC_WORKFLOW_RUN_DIR Workflow run directory
153
+ ====================== ==============
161
154
162
- The following Jinja2 variables are also available (i.e. standalone,
163
- not in the ``environ `` dict):
164
155
165
- ``CYLC_VERSION ``
166
- Version of Cylc used.
156
+ Additional Jinja2 CYLC variables available when the scheduler is parsing an
157
+ installed workflow at run time:
158
+
159
+ .. table ::
160
+
161
+ ======================= ==============
162
+ CYLC_WORKFLOW_LOG_DIR Workflow log sub-directory
163
+ CYLC_WORKFLOW_SHARE_DIR Workflow share sub-directory
164
+ CYLC_WORKFLOW_WORK_DIR Workflow work sub-directory
165
+ ======================= ==============
166
+
167
+ .. note ::
168
+
169
+ Set default values for CYLC variables that are only defined for installed or
170
+ running workflows, to allow successful parsing in other contexts as well:
171
+ ``{{CYLC_WORKFLOW_RUN_DIR | default("not-defined")}} ``.
172
+
173
+
174
+ Environment Variables
175
+ ---------------------
176
+
177
+ Cylc automatically imports the parse-time environment to the template
178
+ processor's global namespace (see :ref: `CustomJinja2Filters `),
179
+ in a dictionary called ``environ ``:
180
+
181
+ .. code-block :: cylc
182
+
183
+ #!Jinja2
184
+ #...
185
+ [runtime]
186
+ [[root]]
187
+ [[[environment]]]
188
+ HOME_DIR_ON_WORKFLOW_HOST = {{environ['HOME']}}
189
+
190
+ .. warning ::
167
191
168
- ``CYLC_TEMPLATE_VARS ``
169
- All variables set by the ``-s ``, ``--set-file `` or ``--set-list `` options,
170
- or by a plugin.
192
+ The environment is read during configuration parsing. It is not the run time
193
+ job environment.
171
194
172
195
173
196
.. _CustomJinja2Filters :
0 commit comments