Skip to content

Commit

Permalink
Mostly working
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Sep 30, 2015
1 parent f3f807d commit 74e5a3e
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 192 deletions.
17 changes: 13 additions & 4 deletions panoramix/bin/panoramix
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def load_examples(sample):
Column("num", Integer),
Column("ds", String(20)),
Column("gender", String(10)),
Column("sum_boys", Integer),
Column("sum_girls", Integer),
)
try:
BirthNames.drop(db.engine)
Expand All @@ -82,7 +84,10 @@ def load_examples(sample):
state=state,
year=year,
ds=ds,
name=name, num=num, gender=gender)
name=name, num=num, gender=gender,
sum_boys=num if gender == 'boy' else 0,
sum_girls=num if gender == 'girl' else 0,
)
if i % 5000 == 0:
print("{} loaded out of 82527 rows".format(i))
session.commit()
Expand All @@ -109,8 +114,11 @@ def load_examples(sample):
obj.main_dttm_col = 'ds'
obj.default_endpoint = "/panoramix/datasource/table/1/?viz_type=table&granularity=one+day&since=100+years&until=now&row_limit=10&where=&flt_col_0=ds&flt_op_0=in&flt_eq_0=&flt_col_1=ds&flt_op_1=in&flt_eq_1=&slice_name=TEST&datasource_name=birth_names&datasource_id=1&datasource_type=table"
obj.database = dbobj
obj.columns = [models.TableColumn(
column_name="num", sum=True, type="INTEGER")]
obj.columns = [
models.TableColumn(column_name="num", sum=True, type="INTEGER"),
models.TableColumn(column_name="sum_boys", sum=True, type="INTEGER"),
models.TableColumn(column_name="sum_girls", sum=True, type="INTEGER"),
]
models.Table
session.add(obj)
session.commit()
Expand Down Expand Up @@ -200,7 +208,7 @@ def load_examples(sample):
session.add(slc)
slices.append(slc)

slice_name = "States"
slice_name = "Gender by State"
slc = session.query(Slice).filter_by(slice_name=slice_name).first()
if not slc:
slc = Slice(
Expand All @@ -210,6 +218,7 @@ def load_examples(sample):
table=tbl,
params=get_slice_json(
slice_name, flt_eq_1="other", viz_type="dist_bar",
metrics=['sum__sum_girls', 'sum__sum_boys'],
groupby=['state'], flt_op_1='not in', flt_col_1='state'))
session.add(slc)
slices.append(slc)
Expand Down
328 changes: 178 additions & 150 deletions panoramix/forms.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion panoramix/templates/panoramix/datasource.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ <h4>Filters</h4>
<input type="hidden" name="datasource_name" value="{{ datasource.name }}">
<input type="hidden" name="datasource_id" value="{{ datasource.id }}">
<input type="hidden" name="datasource_type" value="{{ datasource.type }}">
{{ form.previous_viz_type() }}
</form><br>
</div>

Expand All @@ -132,7 +133,7 @@ <h3>{{ viz.verbose_name }}
<span class="label label-success">
{{ "{0:0.4f}".format(results.duration.total_seconds()) }} s
</span>
<span class="label label-info btn"
<span class="label label-info btn"
data-toggle="modal" data-target="#query_modal">query</span>
{% endif %}
</h3>
Expand Down
8 changes: 4 additions & 4 deletions panoramix/templates/panoramix/viz.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% if viz.form_data.get("json") == "true" %}
{{ viz.get_json() }}
{% else %}
{% if viz.form_data.get("standalone") == "true" %}
{% if viz.request.args.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.form_data.get("skip_libs") != "true" %}
{% if viz.request.args.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,11 +27,11 @@

{% block tail %}
{{super()}}
{% if viz.form_data.get("skip_libs") != "true" %}
{% if viz.request.args.get("skip_libs") != "true" %}
{% for js in viz.js_files %}
<script src="{{ url_for('static', filename=js) }}"></script>
{% endfor %}
{{ viz_macros.viz_js(viz) }}
{% endif %}
{{ viz_macros.viz_js(viz) }}
{% endblock %}
{% endif %}
24 changes: 12 additions & 12 deletions panoramix/templates/panoramix/viz_nvd3.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
nv.addGraph(function() {
// chart_type is {{ viz.chart_type }}
{% if viz.chart_type == 'line' %}
{% if viz.form_data.show_brush == 'y' %}
{% if viz.form_data.show_brush %}
var chart = nv.models.lineWithFocusChart()
var xext = chart.xAxis.scale().domain();
chart
Expand All @@ -52,10 +52,10 @@
chart.xAxis
.showMaxMin(false)
.tickFormat(function (d) {return tickMultiFormat(new Date(d)); });
chart.showLegend({{ "{}".format(viz.form_data.show_legend=='y')|lower }});
chart.showLegend({{ "{}".format(viz.form_data.show_legend)|lower }});
chart.yAxis.tickFormat(d3.format('.3s'));

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

{% elif viz.chart_type == 'pie' %}
var chart = nv.models.pieChart()
chart.showLegend({{ "{}".format(viz.form_data.show_legend=='y')|lower }});
{% if viz.form_data.donut=='y' %}
chart.showLegend({{ "{}".format(viz.form_data.show_legend)|lower }});
{% if viz.form_data.donut %}
chart.donut(true);
chart.donutLabelsOutside(true);
{% endif %}
Expand All @@ -91,14 +91,14 @@
chart.xAxis
.showMaxMin(false)
.tickFormat(function (d) {return tickMultiFormat(new Date(d)); });
chart.showLegend({{ "{}".format(viz.form_data.show_legend=='y')|lower }});
chart.showLegend({{ "{}".format(viz.form_data.show_legend)|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.form_data.show_legend=='y')|lower }});
chart.showLegend({{ "{}".format(viz.form_data.show_legend)|lower }});
chart.pointRange([5, 5000]);

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

{% endif %}

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

{% if viz.form_data.x_log_scale == 'y' %}
{% if viz.form_data.x_log_scale %}
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.form_data.get("skip_libs") != "true" %}
{% if viz.request.args.get("skip_libs") != "true" %}
{% block head %}
<script src="{{url_for('appbuilder.static',filename='js/jquery-latest.js')}}"></script>
{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion 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.form_data.get("async") == "true" %}
{% if viz.request.args.get("async") == "true" %}
{% set df = viz.get_df() %}
<table class="dataframe table table-striped table-bordered table-condensed">
<thead>
Expand Down
60 changes: 41 additions & 19 deletions panoramix/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@
import json
import uuid

from flask import flash
from flask import flash, request
from markdown import markdown
from pandas.io.json import dumps
from werkzeug.datastructures import MultiDict
from werkzeug.datastructures import ImmutableMultiDict
from werkzeug.urls import Href
import numpy as np
import pandas as pd

from panoramix import app, utils
from panoramix.forms import form_factory
from panoramix.forms import FormFactory

config = app.config

CHART_ARGS = {
'title': None,
}


class BaseViz(object):
verbose_name = "Base Viz"
Expand All @@ -32,12 +28,26 @@ class BaseViz(object):

def __init__(self, datasource, form_data):
self.datasource = datasource
form = self.form_class(form_data)
form.validate()
raise
self.form_data = form_data
if isinstance(form_data, MultiDict):
self.form_data = form_data.to_dict(flat=False)
self.request = request

ff = FormFactory(self)
form_class = ff.get_form()
defaults = form_class().data.copy()
if isinstance(form_data, ImmutableMultiDict):
form = form_class(form_data)
else:
form = form_class(**form_data)
data = form.data.copy()
previous_viz_type = form_data.get('previous_viz_type')
if previous_viz_type in viz_types:
data = {
k: form.data[k]
for k in form_data.keys()
if k in viz_types[previous_viz_type].flat_form_fields() and k in form.data}
defaults.update(data)
self.form_data = defaults

self.form_data['previous_viz_type'] = form_data.get("viz_type")
self.token = self.form_data.get(
'token', 'token_' + uuid.uuid4().hex[:8])

Expand All @@ -48,9 +58,19 @@ def __init__(self, datasource, form_data):
elif k not in as_list and isinstance(v, list) and v:
self.form_data[k] = v[0]

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

@classmethod
def flat_form_fields(cls):
l = []
for obj in cls.form_fields:
if isinstance(obj, (tuple, list)):
l += [a for a in obj]
else:
l.append(obj)
return l

def get_url(self, **kwargs):
d = self.form_data.copy()
if 'action' in d:
Expand Down Expand Up @@ -80,7 +100,7 @@ def form(self):

@property
def form_class(self):
return form_factory(self)
return FormFactory(self).get_form()

def query_filters(self):
form_data = self.form_data
Expand Down Expand Up @@ -334,11 +354,13 @@ def get_df(self):
form_data = self.form_data
df = super(NVD3TimeSeriesViz, self).get_df()
df = df.fillna(0)
metrics = self.metrics
if form_data.get("granularity") == "all":
raise Exception("Pick a time granularity for your time series")

df = df.pivot_table(
index="timestamp",
columns=self.groupby,
values=metrics,)
columns=self.form_data.get('groupby'),
values=self.form_data.get('metrics'))

if self.sort_series:
dfs = df.sum()
Expand Down Expand Up @@ -379,7 +401,7 @@ def get_json(self):
series_title = name
else:
name = ["{}".format(s) for s in name]
if len(self.metrics) > 1:
if len(self.form_data.get('metrics')) > 1:
series_title = ", ".join(name)
else:
series_title = ", ".join(name[1:])
Expand Down

0 comments on commit 74e5a3e

Please sign in to comment.