cube
package
cube
package contains tools for setting configuration
options and providing the Jinja template context for
the data model in YAML.
cube
package is available out of the box, it doesn't need to be installed explicitly- Submit issues to
cube
(opens in a new tab) on GitHub
Reference
config
object
This object is used to set configuration options in the cube.py
file.
You can set properties of this object, named after supported configuration options, to values or functions that would be used as these configuration options.
from cube import config
config.base_path = '/cube-api'
config.context_to_app_id = lambda ctx: ctx['securityContext']['tenant_id']
def rewrite(query, ctx):
query['measures'].append('orders.count')
return query
config.query_rewrite = rewrite
Alternatively, this object can be used as a decorator with a single
name
argument. In that case, the decorated function will be set as the
configuration option under that name
.
from cube import config
@config('context_to_app_id')
def app_id(ctx):
return ctx['securityContext']['tenant_id']
@config('query_rewrite')
def rewrite(query, ctx):
query['measures'].append('orders.count')
return query
TemplateContext
class
Instances of this class are used for registering variables, functions, and filters so that they are accessible from Jinja templates when defining the data model in YAML.
from cube import TemplateContext
template = TemplateContext()
template.variable
This method registers a variable so that it's available in a Jinja template.
Should be called with two arguments, a name
and a value
of the variable.
from cube import TemplateContext
template = TemplateContext()
# Accessible from Jinja as 'my_var'
template.variable('my_var', 123)
template.function
This method registers a function so that it's callable from a Jinja template as a Python function.
In case this method is used as a decorator with a single name
argument,
the decorated function will be registered under that name
.
from cube import TemplateContext
template = TemplateContext()
# Accessible from Jinja as 'get_data()'
@template.function('get_data')
def get_data_1():
return 1
In case this method is called with two arguments, a name
and a function,
the passed function will be registered under that name
.
from cube import TemplateContext
template = TemplateContext()
# Accessible from Jinja as 'get_more_data()'
def get_data_2():
return 2
template.function('get_more_data', get_data_2)
template.filter
This method registers a function so that it's callable from a Jinja template as a Jinja filter (opens in a new tab).
In case this method is used as a decorator with a single name
argument,
the decorated function will be registered as a filter under that name
.
from cube import TemplateContext
template = TemplateContext()
# Accessible from Jinja as 'data | wrap'
@template.filter('wrap')
def wrap_1(data):
return f"<{data}>"
In case this method is called with two arguments, a name
and a function,
the passed function will be registered as a filter under that name
.
from cube import TemplateContext
template = TemplateContext()
# Accessible from Jinja as 'data | wrap_more'
def wrap_2(data):
return f"<<<{data}>>>"
template.filter('wrap_more', wrap_2)