Skip to content

Commit

Permalink
Adding support for markup (html/markdown) widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Sep 22, 2015
1 parent 3d8fbaa commit 449441f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 14 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
* Add a per-datasource permission
* Reintroduce query and stopwatch
* Sort tooltip
* Add a "Test Connection" button in Add Connection menu
43 changes: 35 additions & 8 deletions panoramix/bin/panoramix
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def load_examples():
def get_slice_json(
slice_name, filter_value, viz_type="table", group_by=None,
granularity="all", filter_operator='in',
row_limit=config.ROW_LIMIT, flt_col_1="gender"):
row_limit=config.ROW_LIMIT, flt_col_1="gender", code=""):
group_by = group_by if group_by is not None else ["name"]
default_json = {
"compare_lag": "10",
Expand All @@ -130,7 +130,9 @@ def load_examples():
"slice_name": slice_name,
"until": "now",
"viz_type": viz_type,
"where": ""
"where": "",
"code": code,
"markup_type": "markdown",
}
return json.dumps(default_json, indent=4, sort_keys=True)
Slice = models.Slice
Expand Down Expand Up @@ -210,6 +212,24 @@ def load_examples():
session.add(slc)
slices.append(slc)

slice_name = "Title"
slc = session.query(Slice).filter_by(slice_name=slice_name).first()
code = """
### Birth Names Dashboard
The source dataset came from [here](https://github.com/hadley/babynames)
![img](http://monblog.system-linux.net/image/tux/baby-tux_overlord59-tux.png)
"""
if not slc:
slc = Slice(
slice_name=slice_name,
viz_type='markup',
datasource_type='table',
table=tbl,
params=get_slice_json(
slice_name, "", "markup", ['name'], code=code))
session.add(slc)
slices.append(slc)

print("Creating a dashboard")
Dash = models.Dashboard
Expand All @@ -222,44 +242,51 @@ def load_examples():
{
"size_y": 5,
"size_x": 2,
"col": 5,
"col": 10,
"slice_id": "1",
"row": 1
},
{
"size_y": 5,
"size_x": 2,
"col": 7,
"col": 8,
"slice_id": "2",
"row": 1
},
{
"size_y": 2,
"size_x": 2,
"col": 1,
"col": 6,
"slice_id": "3",
"row": 1
},
{
"size_y": 2,
"size_x": 2,
"col": 3,
"col": 4,
"slice_id": "4",
"row": 1
},
{
"size_y": 3,
"size_x": 4,
"col": 1,
"col": 4,
"slice_id": "5",
"row": 3
},
{
"size_y": 6,
"size_x": 8,
"size_x": 11,
"col": 1,
"slice_id": "6",
"row": 6
},
{
"size_y": 5,
"size_x": 3,
"col": 1,
"slice_id": "9",
"row": 1
}
]
"""
Expand Down
9 changes: 8 additions & 1 deletion panoramix/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from wtforms import Field, Form, SelectMultipleField, SelectField, TextField
from wtforms import (
Field, Form, SelectMultipleField, SelectField, TextField, TextAreaField)
from copy import copy


Expand Down Expand Up @@ -84,6 +85,12 @@ def form_factory(viz):
description="Based on granularity, number of time periods to compare against"),
'compare_suffix': TextField('Comparison suffix',
description="Suffix to apply after the percentage display"),
'markup_type': SelectField(
"Markup Type",
choices=[(s, s) for s in ['markdown', 'html']],
default="markdown",
description="Pick your favorite markup language"),
'code': TextAreaField("Code", description="Put your code here"),
}
field_css_classes = {k: ['form-control'] for k in px_form_fields.keys()}
select2 = [
Expand Down
10 changes: 7 additions & 3 deletions panoramix/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from wtforms.validators import ValidationError
from flask.ext.appbuilder.actions import action

from panoramix import appbuilder, db, models, viz, utils, app
from panoramix import appbuilder, db, models, viz, utils, app, config


def validate_json(form, field):
Expand Down Expand Up @@ -288,13 +288,17 @@ def datasource(self, datasource_type, datasource_id):
status=status,
mimetype="application/json")
else:
try:
resp = self.render_template("panoramix/viz.html", viz=obj)
#try:
resp = self.render_template("panoramix/viz.html", viz=obj)
'''
except Exception as e:
if config.DEBUG:
raise(e)
return Response(
str(e),
status=500,
mimetype="application/json")
'''
return resp

@has_access
Expand Down
18 changes: 16 additions & 2 deletions panoramix/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import uuid

from flask import flash
from markdown import markdown
from werkzeug.datastructures import MultiDict
from werkzeug.urls import Href
import numpy as np
Expand All @@ -21,7 +22,6 @@
class BaseViz(object):
verbose_name = "Base Viz"
template = None
hidden_fields = []
form_fields = [
'viz_type', 'metrics', 'groupby', 'granularity',
('since', 'until')]
Expand Down Expand Up @@ -161,6 +161,20 @@ def get_df(self):
return df


class MarkupViz(BaseViz):
verbose_name = "Markup Widget"
template = 'panoramix/viz_markup.html'
form_fields = ['viz_type', 'markup_type', 'code']

def rendered(self):
markup_type = self.form_data.get("markup_type")
code = self.form_data.get("code", '')
if markup_type == "markdown":
return markdown(code)
elif markup_type == "html":
return code


class HighchartsViz(BaseViz):
verbose_name = "Base Highcharts Viz"
template = 'panoramix/viz_highcharts.html'
Expand All @@ -174,7 +188,6 @@ class HighchartsViz(BaseViz):
class BubbleViz(HighchartsViz):
verbose_name = "Bubble Chart"
chart_type = 'bubble'
hidden_fields = ['granularity', 'metrics', 'groupby']
form_fields = [
'viz_type', 'since', 'until',
'series', 'entity', 'x', 'y', 'size', 'limit']
Expand Down Expand Up @@ -376,4 +389,5 @@ class DistributionBarViz(DistributionPieViz):
['dist_bar', DistributionBarViz],
['pie', DistributionPieViz],
['bubble', BubbleViz],
['markup', MarkupViz],
])
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ flask
flask-migrate
flask-appbuilder
gunicorn
markdown
mysql-python
pandas
parsedatetime
Expand Down

0 comments on commit 449441f

Please sign in to comment.