Skip to content

Commit

Permalink
Better form handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Sep 28, 2015
1 parent 721113d commit a674fca
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 70 deletions.
2 changes: 1 addition & 1 deletion panoramix/bin/panoramix
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def load_examples(sample):
with gzip.open(config.get("BASE_DIR") + '/data/birth_names.csv.gz') as f:
bb_csv = csv.reader(f)
for i, (state, year, name, gender, num) in enumerate(bb_csv):
if i == 0:
if i == 0 or year < "1965": # jumpy data before 1965
continue
if num == "NA":
num = 0
Expand Down
2 changes: 1 addition & 1 deletion panoramix/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def form_factory(viz):
"Range Selector", default=True,
description="Whether to display the time range interactive selector"),
'show_legend': BooleanField(
"Legend", default=True,
"Legend", default=True, false_values=["f"],
description="Whether to display the legend (toggles)"),
'rich_tooltip': BooleanField(
"Rich Tooltip", default=True,
Expand Down
8 changes: 4 additions & 4 deletions panoramix/templates/panoramix/viz.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{% import viz.template as viz_macros %}

{% if viz.args.get("json") == "true" %}
{% if viz.form_data.get("json") == "true" %}
{{ viz.get_json() }}
{% else %}
{% if viz.args.get("standalone") == "true" %}
{% if viz.form_data.get("standalone") == "true" %}
{% extends 'panoramix/viz_standalone.html' %}
{% else %}
{% extends 'panoramix/datasource.html' %}
Expand All @@ -16,7 +16,7 @@

{% block head_css %}
{{super()}}
{% if viz.args.get("skip_libs") != "true" %}
{% if viz.form_data.get("skip_libs") != "true" %}
{% for css in viz.css_files %}
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename=css) }}">
{% endfor %}
Expand All @@ -27,7 +27,7 @@

{% block tail %}
{{super()}}
{% if viz.args.get("skip_libs") != "true" %}
{% if viz.form_data.get("skip_libs") != "true" %}
<script src="{{ url_for("static", filename="d3.min.js") }}"></script>
<script>
f = d3.format(".4s");
Expand Down
26 changes: 14 additions & 12 deletions panoramix/templates/panoramix/viz_nvd3.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,25 @@
nv.addGraph(function() {
// chart_type is {{ viz.chart_type }}
{% if viz.chart_type == 'line' %}
{% if viz.args.show_brush == 'y' %}
{% if viz.form_data.show_brush == 'y' %}
var chart = nv.models.lineWithFocusChart()
var xext = chart.xAxis.scale().domain();
chart
.x2Axis
.tickFormat(function (d) {return tickMultiFormat(UTC(new Date(d))); })
.tickValues([]);
chart.y2Axis.tickFormat(d3.format('.3s'));
{% else %}
var chart = nv.models.lineChart()
{% endif %}
chart.xScale(d3.time.scale());
chart.xAxis
.showMaxMin(false)
.tickFormat(function (d) {return tickMultiFormat(new Date(d)); });
chart.showLegend({{ "{}".format(viz.args.show_legend=='y')|lower }});
chart.showLegend({{ "{}".format(viz.form_data.show_legend=='y')|lower }});
chart.yAxis.tickFormat(d3.format('.3s'));
{% if viz.args.contribution=='y' or viz.args.get("num_period_compare") %}

{% if viz.form_data.contribution=='y' or viz.form_data.get("num_period_compare") %}
chart.yAxis.tickFormat(d3.format('.3p'));
chart.y2Axis.tickFormat(d3.format('.3p'));
{% endif %}
Expand All @@ -69,8 +71,8 @@

{% elif viz.chart_type == 'pie' %}
var chart = nv.models.pieChart()
chart.showLegend({{ "{}".format(viz.args.show_legend=='y')|lower }});
{% if viz.args.donut=='y' %}
chart.showLegend({{ "{}".format(viz.form_data.show_legend=='y')|lower }});
{% if viz.form_data.donut=='y' %}
chart.donut(true);
chart.donutLabelsOutside(true);
{% endif %}
Expand All @@ -89,14 +91,14 @@
chart.xAxis
.showMaxMin(false)
.tickFormat(function (d) {return tickMultiFormat(new Date(d)); });
chart.showLegend({{ "{}".format(viz.args.show_legend=='y')|lower }});
chart.showLegend({{ "{}".format(viz.form_data.show_legend=='y')|lower }});
chart.yAxis.tickFormat(d3.format('.3p'));

{% elif viz.chart_type == 'bubble' %}
var chart = nv.models.scatterChart();
chart.xAxis.tickFormat(d3.format('.3s'));
chart.yAxis.tickFormat(d3.format('.3s'));
chart.showLegend({{ "{}".format(viz.args.show_legend=='y')|lower }});
chart.showLegend({{ "{}".format(viz.form_data.show_legend=='y')|lower }});
chart.pointRange([5, 5000]);

{% elif viz.chart_type == 'stacked' %}
Expand All @@ -105,21 +107,21 @@
chart.xAxis
.showMaxMin(false)
.tickFormat(function (d) {return tickMultiFormat(new Date(d)); });
chart.showLegend({{ "{}".format(viz.args.show_legend=='y')|lower }});
chart.showLegend({{ "{}".format(viz.form_data.show_legend=='y')|lower }});
chart.yAxis.tickFormat(d3.format('.3s'));

{% endif %}

{% if viz.chart_type in ("line", "stacked") and viz.args.rich_tooltip == 'y' %}
{% if viz.chart_type in ("line", "stacked") and viz.form_data.rich_tooltip == 'y' %}
chart.useInteractiveGuideline(true);
{% endif %}
{% if viz.args.y_axis_zero == 'y' %}
{% if viz.form_data.y_axis_zero == 'y' %}
chart.forceY([0, 1]);
{% elif viz.args.y_log_scale == 'y' %}
{% elif viz.form_data.y_log_scale == 'y' %}
chart.yScale(d3.scale.log());
{% endif %}

{% if viz.args.x_log_scale == 'y' %}
{% if viz.form_data.x_log_scale == 'y' %}
chart.xScale(d3.scale.log());
{% endif %}

Expand Down
2 changes: 1 addition & 1 deletion panoramix/templates/panoramix/viz_standalone.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>
<head>
{% if viz.args.get("skip_libs") != "true" %}
{% if viz.form_data.get("skip_libs") != "true" %}
{% block head %}
<script src="{{url_for('appbuilder.static',filename='js/jquery-latest.js')}}"></script>
{% endblock %}
Expand Down
4 changes: 2 additions & 2 deletions panoramix/templates/panoramix/viz_table.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% macro viz_html(viz) %}
{% if viz.args.get("async") == "true" %}
{% if viz.form_data.get("async") == "true" %}
{% set df = viz.get_df() %}
<table class="dataframe table table-striped table-bordered table-condensed">
<thead>
Expand Down Expand Up @@ -32,7 +32,7 @@
{% endmacro %}

{% macro viz_js(viz) %}
{% if viz.args.get("async") != "true" %}
{% if viz.form_data.get("async") != "true" %}
<script>
$( document ).ready(function() {
var url = "{{ viz.get_url(async="true", standalone="true", skip_libs="true")|safe }}";
Expand Down
6 changes: 3 additions & 3 deletions panoramix/templates/panoramix/viz_word_cloud.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
var token = d3.select("#{{ viz.token }}");
function refresh() {
var range = [
{{ viz.args.get('size_from') or 20 }},
{{ viz.args.get('size_to') or 100 }}
{{ viz.form_data.get('size_from') or 20 }},
{{ viz.form_data.get('size_to') or 100 }}
];
var rotation = "{{ viz.args.get("rotation", "random") }}";
var rotation = "{{ viz.form_data.get("rotation", "random") }}";
if (rotation == "square")
var f_rotation = function() { return ~~(Math.random() * 2) * 90; };
else if (rotation == "flat")
Expand Down
92 changes: 46 additions & 46 deletions panoramix/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,27 @@ class BaseViz(object):
css_files = []

def __init__(self, datasource, form_data):
self.datasource = datasource
if isinstance(form_data, MultiDict):
self.args = form_data.to_dict(flat=False)
else:
self.args = form_data
self.form_data = form_data
self.token = self.args.get('token', 'token_' + uuid.uuid4().hex[:8])
if isinstance(form_data, MultiDict):
self.form_data = form_data.to_dict(flat=False)
self.datasource = datasource
self.token = self.form_data.get(
'token', 'token_' + uuid.uuid4().hex[:8])

as_list = ('metrics', 'groupby')
for k, v in self.args.items():
for k, v in self.form_data.items():
if k in as_list and not isinstance(v, list):
self.args[k] = [v]
self.form_data[k] = [v]
elif k not in as_list and isinstance(v, list) and v:
self.args[k] = v[0]
self.form_data[k] = v[0]
for i in range(50):
print('show_legend' in form_data)

self.metrics = self.args.get('metrics') or ['count']
self.groupby = self.args.get('groupby') or []
self.metrics = self.form_data.get('metrics') or ['count']
self.groupby = self.form_data.get('groupby') or []

def get_url(self, **kwargs):
d = self.args.copy()
d = self.form_data.copy()
if 'action' in d:
del d['action']
d.update(kwargs)
Expand All @@ -74,21 +75,20 @@ def get_df(self):

@property
def form(self):
return self.form_class(**self.args)
return self.form_class(self.form_data)
return self.form_class(**self.form_data)

@property
def form_class(self):
return form_factory(self)

def query_filters(self):
args = self.args
form_data = self.form_data
# Building filters
filters = []
for i in range(1, 10):
col = args.get("flt_col_" + str(i))
op = args.get("flt_op_" + str(i))
eq = args.get("flt_eq_" + str(i))
col = form_data.get("flt_col_" + str(i))
op = form_data.get("flt_op_" + str(i))
eq = form_data.get("flt_eq_" + str(i))
if col and op and eq:
filters.append((col, op, eq))
return filters
Expand All @@ -100,21 +100,21 @@ def query_obj(self):
"""
Building a query object
"""
args = self.args
groupby = args.get("groupby") or []
metrics = args.get("metrics") or ['count']
granularity = args.get("granularity", "1 day")
form_data = self.form_data
groupby = form_data.get("groupby") or []
metrics = form_data.get("metrics") or ['count']
granularity = form_data.get("granularity", "1 day")
if granularity != "all":
granularity = utils.parse_human_timedelta(
granularity).total_seconds() * 1000
limit = int(args.get("limit", 0))
limit = int(form_data.get("limit", 0))
row_limit = int(
args.get("row_limit", config.get("ROW_LIMIT")))
since = args.get("since", "1 year ago")
form_data.get("row_limit", config.get("ROW_LIMIT")))
since = form_data.get("since", "1 year ago")
from_dttm = utils.parse_human_datetime(since)
if from_dttm > datetime.now():
from_dttm = datetime.now() - (from_dttm-datetime.now())
until = args.get("until", "now")
until = form_data.get("until", "now")
to_dttm = utils.parse_human_datetime(until)
if from_dttm >= to_dttm:
flash("The date range doesn't seem right.", "danger")
Expand All @@ -123,7 +123,7 @@ def query_obj(self):
# extras are used to query elements specific to a datasource type
# for instance the extra where clause that applies only to Tables
extras = {
'where': args.get("where", '')
'where': form_data.get("where", '')
}
d = {
'granularity': granularity,
Expand Down Expand Up @@ -197,10 +197,10 @@ class WordCloudViz(BaseViz):
def query_obj(self):
d = super(WordCloudViz, self).query_obj()
d['granularity'] = 'all'
metric = self.args.get('metric')
metric = self.form_data.get('metric')
if not metric:
raise Exception("Pick a metric!")
d['metrics'] = [self.args.get('metric')]
d['metrics'] = [self.form_data.get('metric')]
d['groupby'] = [d['groupby'][0]]
return d

Expand Down Expand Up @@ -232,18 +232,18 @@ class BubbleViz(NVD3Viz):
]

def query_obj(self):
args = self.form_data
form_data = self.form_data
d = super(BubbleViz, self).query_obj()
d['granularity'] = 'all'
d['groupby'] = list({
args.get('series'),
args.get('entity')
form_data.get('series'),
form_data.get('entity')
})
self.x_metric = args.get('x')
self.y_metric = args.get('y')
self.z_metric = args.get('size')
self.entity = args.get('entity')
self.series = args.get('series')
self.x_metric = form_data.get('x')
self.y_metric = form_data.get('y')
self.z_metric = form_data.get('size')
self.entity = form_data.get('entity')
self.series = form_data.get('series')
d['metrics'] = [
self.z_metric,
self.x_metric,
Expand Down Expand Up @@ -292,23 +292,23 @@ class BigNumberViz(BaseViz):

def query_obj(self):
d = super(BigNumberViz, self).query_obj()
metric = self.args.get('metric')
metric = self.form_data.get('metric')
if not metric:
raise Exception("Pick a metric!")
d['metrics'] = [self.args.get('metric')]
d['metrics'] = [self.form_data.get('metric')]
return d

def get_json(self):
args = self.args
form_data = self.form_data
df = self.get_df()
df = df.sort(columns=df.columns[0])
df['timestamp'] = df[[0]].astype(np.int64) // 10**9
compare_lag = args.get("compare_lag", "")
compare_lag = form_data.get("compare_lag", "")
compare_lag = int(compare_lag) if compare_lag.isdigit() else 0
d = {
'data': df.values.tolist(),
'compare_lag': compare_lag,
'compare_suffix': args.get('compare_suffix', ''),
'compare_suffix': form_data.get('compare_suffix', ''),
}
return json.dumps(d)

Expand All @@ -330,7 +330,7 @@ class NVD3TimeSeriesViz(NVD3Viz):
]

def get_df(self):
args = self.args
form_data = self.form_data
df = super(NVD3TimeSeriesViz, self).get_df()
df = df.fillna(0)
metrics = self.metrics
Expand All @@ -344,7 +344,7 @@ def get_df(self):
dfs.sort(ascending=False)
df = df[dfs.index]

if self.args.get("contribution") == "y":
if self.form_data.get("contribution") == "y":
dft = df.T
df = (dft / dft.sum()).T

Expand All @@ -354,8 +354,8 @@ def get_df(self):
df = df / df.shift(num_period_compare)
df = df[num_period_compare:]

rolling_periods = args.get("rolling_periods")
rolling_type = args.get("rolling_type")
rolling_periods = form_data.get("rolling_periods")
rolling_type = form_data.get("rolling_type")
if rolling_periods and rolling_type:
if rolling_type == 'mean':
df = pd.rolling_mean(df, int(rolling_periods))
Expand Down

0 comments on commit a674fca

Please sign in to comment.