Skip to content

Commit

Permalink
Changing the way viz templates are defined using macros instead
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Sep 14, 2015
1 parent 6daf92e commit 0bc2e71
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 59 deletions.
2 changes: 1 addition & 1 deletion panoramix/templates/panoramix/datasource.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ <h3>{{ viz.verbose_name }}
</h3>
<hr/>
<div class="viz" style="height: 600px;">
{% block viz %}
{% block viz_html %}
{% if viz.error_msg %}
<div class="alert alert-danger">{{ viz.error_msg }}</div>
{% endif %}
Expand Down
9 changes: 9 additions & 0 deletions panoramix/templates/panoramix/viz.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
{% import viz.template as viz_macros %}

{% if standalone %}
{% extends 'panoramix/viz_standalone.html' %}
{% else %}
{% extends 'panoramix/datasource.html' %}
{% endif %}


{% block viz_html %}
{{ viz_macros.viz_html(viz) }}
{% endblock %}

{% block head %}
{{super()}}
{% if not skip_libs %}
{% for css in viz.css_files %}
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename=css) }}">
{% endfor %}
{% endif %}
{{ viz_macros.viz_css(viz) }}
{% endblock %}


Expand All @@ -25,4 +33,5 @@
<script src="{{ url_for('static', filename=js) }}"></script>
{% endfor %}
{% endif %}
{{ viz_macros.viz_js(viz) }}
{% endblock %}
18 changes: 9 additions & 9 deletions panoramix/templates/panoramix/viz_highcharts.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{% extends "panoramix/viz.html" %}
{% block viz %}
{{ super() }}
{% macro viz_html(viz) %}
<div id="chart" style="height:100%; width:100%;"></div>
{% endblock %}
{% endmacro %}

{% block tail %}
{{ super() }}
{% macro viz_js(viz) %}
<script>
$( document ).ready(function() {
Highcharts.setOptions({
Expand All @@ -20,9 +17,12 @@
$("#viz_type").click(function(){
$("#queryform").submit();
})
{% if chart_js %}
{{ chart_js|safe }}
{% if viz.chart_js %}
{{ viz.chart_js|safe }}
{% endif %}
});
</script>
{% endblock %}
{% endmacro %}

{% macro viz_css(viz) %}
{% endmacro %}
2 changes: 1 addition & 1 deletion panoramix/templates/panoramix/viz_standalone.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{% block tail %}{% endblock %}
</head>
<body>
{% block viz %}
{% block viz_html %}
{% endblock %}
</body>
</html>
79 changes: 38 additions & 41 deletions panoramix/templates/panoramix/viz_table.html
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
{% extends "panoramix/viz.html" %}
{% macro viz_html(viz) %}
{% set df = viz.df %}
<table class="dataframe table table-striped table-bordered table-condensed">
<thead>
<tr>
{% for col in df.columns if not col.endswith('__perc') %}
<th>{{ col }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in df.to_dict(orient="records") %}
<tr>
{% for col in df.columns if not col.endswith('__perc') %}
{% if col + '__perc' in df.columns %}
<td style="background-image: linear-gradient(to right, lightgrey, lightgrey {{ row[col+'__perc'] }}%, rgba(0,0,0,0) {{ row[col+'__perc'] }}%">
{{ row[col] }}
</td>
{% else %}
<td>{{ row[col] }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endmacro %}

{% block viz %}
{{ super() }}
{% if not error_msg %}
<table class="dataframe table table-striped table-bordered table-condensed">
<thead>
<tr>
{% for col in df.columns if not col.endswith('__perc') %}
<th>{{ col }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in df.to_dict(orient="records") %}
<tr>
{% for col in df.columns if not col.endswith('__perc') %}
{% if col + '__perc' in df.columns %}
<td style="background-image: linear-gradient(to right, lightgrey, lightgrey {{ row[col+'__perc'] }}%, rgba(0,0,0,0) {{ row[col+'__perc'] }}%">
{{ row[col] }}
</td>
{% else %}
<td>{{ row[col] }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% endblock %}

{% block tail %}
{{ super() }}
<script>
//$('table').css('background-color', 'white');
{% macro viz_js(viz) %}
<script>
//$('table').css('background-color', 'white');
$(document).ready(function() {
var table = $('table').DataTable({
paging: false,
});
table.column('-1').order( 'desc' ).draw();
var table = $('table').DataTable({
paging: false,
});
table.column('-1').order( 'desc' ).draw();
});
</script>
{% endblock %}
</script>
{% endmacro %}

{% macro viz_css(viz) %}
{% endmacro %}
16 changes: 9 additions & 7 deletions panoramix/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def check_and_render(self, *args, **kwards):
def render(self, *args, **kwargs):
form = self.form_class(self.form_data)
return self.view.render_template(
self.template, form=form, viz=self, datasource=self.datasource,
"panoramix/viz.html", form=form, viz=self, datasource=self.datasource,
results=self.results,
standalone=request.args.get('standalone') == 'true',
skip_libs=request.args.get('skip_libs') == 'true',
Expand Down Expand Up @@ -211,7 +211,8 @@ def render(self):
df['name'] = df[[self.entity]]
df['group'] = df[[self.series]]
chart = HighchartBubble(df)
return super(BubbleViz, self).render(chart_js=chart.javascript_cmd)
self.chart_js = chart.javascript_cmd
return super(BubbleViz, self).render()


class TimeSeriesViz(HighchartsViz):
Expand Down Expand Up @@ -259,7 +260,8 @@ def render(self):
stockchart=self.stockchart,
sort_legend_y=self.sort_legend_y,
**CHART_ARGS)
return super(TimeSeriesViz, self).render(chart_js=chart.javascript_cmd)
self.chart_js = chart.javascript_cmd
return super(TimeSeriesViz, self).render()

def bake_query(self):
"""
Expand Down Expand Up @@ -314,8 +316,8 @@ def render(self):
df = df.sort(self.metrics[0], ascending=False)
chart = Highchart(
df, chart_type=self.chart_type, **CHART_ARGS)
return super(DistributionBarViz, self).render(
chart_js=chart.javascript_cmd)
self.chart_js = chart.javascript_cmd
return super(DistributionBarViz, self).render()


class DistributionPieViz(HighchartsViz):
Expand All @@ -337,8 +339,8 @@ def render(self):
df = df.sort(self.metrics[0], ascending=False)
chart = Highchart(
df, chart_type=self.chart_type, **CHART_ARGS)
return super(DistributionPieViz, self).render(
chart_js=chart.javascript_cmd)
self.chart_js = chart.javascript_cmd
return super(DistributionPieViz, self).render()

viz_types = OrderedDict([
['table', TableViz],
Expand Down

0 comments on commit 0bc2e71

Please sign in to comment.