Skip to content

Commit 8e4ed00

Browse files
authored
Update Jinja2 and EmPy templating sections. (#631)
1 parent b3ea16c commit 8e4ed00

File tree

2 files changed

+80
-49
lines changed

2 files changed

+80
-49
lines changed

src/user-guide/writing-workflows/empy.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,11 @@ reasons:*
4848
extensions makes it easier to refactor and maintain the template code
4949
as its complexity grows. Inline Python code can be gathered into subroutines
5050
and eventually into separate modules and packages in a seamless manner.
51+
52+
53+
Workflow files and Context Variables
54+
------------------------------------
55+
56+
The EmPy processor has the same access as Jinja2 to
57+
:ref:`workflow files <jinja2.workflow_files>` and
58+
:ref:`workflow context variables <jinja2.workflow_context>`.

src/user-guide/writing-workflows/jinja2.rst

Lines changed: 72 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,28 @@ Jinja2
77
.. tutorial::
88
Configuration Consolidation Tutorial <tutorial-cylc-consolidating-configuration>
99

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.
1513

1614
To use Jinja2, put a hash-bang comment in the first line of :cylc:conf:`flow.cylc`:
1715

1816
.. code-block:: cylc
1917
2018
#!jinja2
2119
22-
Template processing is the first thing done on parsing a workflow configuration
23-
so Jinja2 can appear anywhere in the file.
24-
2520
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_>`_.
2822

2923
Uses of Jinja2 in Cylc include:
3024

3125
- Inclusion or exclusion of config sections by logical switch, e.g. to make
3226
portable workflows
33-
- Computation of config values from in input data
27+
- Computation of config values from input data
3428
- 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
3630
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
3832

3933
.. _fig-jinja2-ensemble:
4034

@@ -99,7 +93,7 @@ to generate task names automatically with built-in
9993
R1 = "foo => mem<m> => post<m> => bar"
10094
10195
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
10397
more complex. To add a new city and associated tasks and dependencies just add
10498
the new city name to list at the top of the file. It makes use of Jinja2
10599
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.
120114
New York City task family expanded.
121115

122116

123-
Accessing Environment Variables
124-
-------------------------------
117+
.. _jinja2.workflow_files:
125118

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+
------------------------
128121

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*.
130125

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.
137128

138-
In addition, the following variables are exported to this environment
139-
(hence are available in the ``environ`` dict) to provide workflow context:
140129

141-
.. code-block:: sub
130+
.. _jinja2.workflow_context:
142131

143-
CYLC_VERBOSE # Verbose mode, true or false
144-
CYLC_DEBUG # Debug mode (even more verbose), true or false
132+
Workflow Context variables
133+
--------------------------
145134

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):
149136

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::
156138

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::
158149

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+
====================== ==============
161154

162-
The following Jinja2 variables are also available (i.e. standalone,
163-
not in the ``environ`` dict):
164155

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::
167191

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.
171194

172195

173196
.. _CustomJinja2Filters:

0 commit comments

Comments
 (0)