A script for rendering Apache Superset SQL datasets which contain jinja templating code. Running this cript on a .sql file containing jinja templates will replace jinja code with real values and turn it into executable SQL code.
- Python 3
- Jinja2 python package (install with pip)
- Copy the "rendersql.py" script onto your local machine to some place where it won't be moved from.
- Paste this bash function into your .zshrc or .bashrc file:
rendersql() {
	python3  "full/path/to/rendersql.py"  --template  "$1"
}- Add execution permission for rendersql.py by running the following command in terminal (replace with your file path):
chmod +x /full/path/to/rendersql.py- Run the script on any file by executing it in terminal:
rendersql path/to/your/jinja/dataset.sqlThis will create a new file with the same name as your original sql file, just concatenating "_render" to the end of filename.
- If you are using the "filter_values" Superset macro, always define its placeholder in a variable at the beginning of your dataset.
{% set test_variable = filter_values('FILTER_2') if filter_values('FILTER_2') else [1, 2, 3] %}In this way, if the dataset is used within Superset dashboard it will use the filter values for rendering, and if it is used outside of the dashboard context it will use the placeholder for rendering. See test.sql file to understand how this is done.
- dataset()macro can't be used with this script. All macros that this cript can use must be defined within the dataset sql file and must be executable on Superset.