@@ -155,13 +155,20 @@ class SafeString(str):
155155
156156# # Python
157157
158- You can declare and invoke Python functions from within a Jinja template. This
159- allows the reuse of existing code to generate data models. Cube uses Python 3.9 to execute Python code.
160- It also installs packages listed in the `requirements.txt` with pip on the startup.
158+ # ## Template context
161159
162- These helper functions must be located in `model/globals.py` file or explicitly loaded from the YAML files.
163- In the following example, we declare a function called `load_data()` which will load data from a remote
164- API endpoint. We will then use the function to generate a data model in Cube.
160+ You can use Python to declare functions that can be invoked and variables that can be
161+ referenced from within a Jinja template. These functions and variables must be defined
162+ in `model/globals.py` file and registered in the `TemplateContext` instance.
163+
164+ <ReferenceBox>
165+
166+ See the [`TemplateContext` reference][ref-cube-template-context] for more details.
167+
168+ </ReferenceBox>
169+
170+ In the following example, we declare a function called `load_data` that supposedly loads
171+ data from a remote API endpoint. We will then use the function to generate a data model :
165172
166173` ` ` python
167174from cube import TemplateContext
@@ -237,12 +244,46 @@ cubes:
237244 {%- endfor %}
238245` ` `
239246
240- <ReferenceBox>
247+ # ## Imports
241248
242- If you'd like to split your Python code into several files, see
243- [this issue](https://github.com/cube-js/cube/issues/8443#issuecomment-2219804266).
249+ In the `model/globals.py` file (or the `cube.py` configuration file), you can
250+ import modules from the current directory. In the following example, we import a function
251+ from the `utils` module and use it to populate a variable in the template context :
244252
245- </ReferenceBox>
253+ ` ` ` python filename="model/utils.py"
254+ def answer_to_main_question() -> str:
255+ return "42"
256+ ` ` `
257+
258+ ` ` ` python filename="model/globals.py"
259+ from cube import TemplateContext
260+ from utils import answer_to_main_question
261+
262+ template = TemplateContext()
263+
264+ answer = answer_to_main_question()
265+ template.add_variable('answer', answer)
266+ ` ` `
267+ # ## Dependencies
268+
269+ If you need to use dependencies in your dynamic data model (or your `cube.py`
270+ configuration file), you can list them in the `requirements.txt` file in the root
271+ directory of your Cube deployment. They will be automatically installed with `pip` on
272+ the startup.
273+
274+ <InfoBox>
275+
276+ [`cube` package][ref-cube-package] is available out of the box, it doesn't need to be
277+ listed in `requirements.txt`.
278+
279+ </InfoBox>
280+
281+ If you use dbt for data transformation, you might find the [`cube_dbt`
282+ package][ref-cube-dbt-package] useful. It provides a set of utilities that simplify
283+ defining the data model in YAML [based on dbt models][ref-cube-with-dbt].
284+
285+ If you need to use dependencies with native extensions, build a [custom Docker
286+ image][ref-docker-image-extension].
246287
247288
248289[jinja] : https://jinja.palletsprojects.com/
@@ -253,4 +294,9 @@ If you'd like to split your Python code into several files, see
253294[jinja-docs-autoescaping] : https://jinja.palletsprojects.com/en/3.1.x/api/#autoescaping
254295[jinja-docs-filters-safe] : https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.safe
255296[ref-cube-dbt] : /reference/python/cube_dbt
256- [ref-visual-model] : /product/workspace/visual-model
297+ [ref-visual-model] : /product/workspace/visual-model
298+ [ref-docker-image-extension] : /product/deployment/core#extend-the-docker-image
299+ [ref-cube-package] : /reference/python/cube
300+ [ref-cube-template-context] : /reference/python/cube#templatecontext-class
301+ [ref-cube-dbt-package] : /reference/python/cube_dbt
302+ [ref-cube-with-dbt] : /guides/dbt
0 commit comments