Thanks!
FYI, this is also an effective workaround for issue #5755 .
If I'm not mistaken that's the best way atm to configure SQL Lab queries via
the URL parameters of the dashboard.
Just add the following snippet to your superset_config.py to use Manuels code
via the jinja context addons until this is merged.
You can then configure the charts via the preselect_filters.
SQL:
```
...
WHERE foo = {{ filter_values('bar', 'default_bar')[0] }}
...
```
URL:
```
.../superset/dashboard/baz/?standalone=true&preselect_filters={"CHART-ID":{"foo":["30"]}}
```
superset_config.py:
```
#
-----------------------------------------------------------------------------------
# Read filter values in jinja templates
#
-----------------------------------------------------------------------------------
import json
from flask import request
def filter_values(column, default=None):
""" Gets a values for a particular filter as a list
This is useful if:
- you want to use a filter box to filter a query where the name of
filter box
column doesn't match the one in the select statement
- you want to have the ability for filter inside the main query for
speed purposes
This searches for "filters" and "extra_filters" in form_data for a match
Usage example:
* SELECT action, count(*) as times
FROM logs
WHERE action in ( {{ "'" + "','".join(filter_values('action_type')) +
"'" )
GROUP BY 1
:param column: column/filter name to lookup
:type column: str
:param default: default value to return if there's no matching columns
:type default: str
:return: returns a list of filter values
:rtype: list
"""
form_data = json.loads(request.form.get('form_data', '{}'))
return_val = []
for filter_type in ['filters', 'extra_filters']:
if filter_type not in form_data:
continue
for f in form_data[filter_type]:
if f['col'] == column:
for v in f['val']:
return_val.append(v)
if return_val:
return return_val
if default:
return [default]
else:
return []
JINJA_CONTEXT_ADDONS = {
"filter_values": filter_values
}
```
[ Full content available at:
https://github.com/apache/incubator-superset/pull/5547 ]
This message was relayed via gitbox.apache.org for [email protected]