Pretty print data from Query tab (#257)
This commit is contained in:
committed by
Corey Hammerton
parent
fac55edc98
commit
4887588662
@@ -21,7 +21,7 @@ from pypuppetdb import connect
|
||||
from puppetboard.forms import (CatalogForm, QueryForm)
|
||||
from puppetboard.utils import (
|
||||
get_or_abort, yield_or_stop,
|
||||
jsonprint, Pagination
|
||||
jsonprint, prettyprint, Pagination
|
||||
)
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ graph_facts += app.config['GRAPH_FACTS']
|
||||
app.secret_key = app.config['SECRET_KEY']
|
||||
|
||||
app.jinja_env.filters['jsonprint'] = jsonprint
|
||||
app.jinja_env.filters['prettyprint'] = prettyprint
|
||||
|
||||
puppetdb = connect(
|
||||
host=app.config['PUPPETDB_HOST'],
|
||||
|
||||
@@ -4,7 +4,7 @@ from __future__ import absolute_import
|
||||
from flask.ext.wtf import Form
|
||||
from wtforms import (
|
||||
HiddenField, RadioField, SelectField,
|
||||
TextAreaField, validators
|
||||
TextAreaField, BooleanField, validators
|
||||
)
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ class QueryForm(Form):
|
||||
('edges', 'Edges'),
|
||||
('environments', 'Environments'),
|
||||
])
|
||||
rawjson = BooleanField('Raw JSON')
|
||||
|
||||
class CatalogForm(Form):
|
||||
"""The form used to compare the catalogs of different nodes."""
|
||||
|
||||
@@ -26,6 +26,11 @@
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div class="inline fields">
|
||||
<div class="field">
|
||||
{{ form.rawjson(active=True) }} Raw JSON
|
||||
</div>
|
||||
</div>
|
||||
<input type=submit class="ui submit button" value='Submit'>
|
||||
<input type=reset class="ui red submit button" value='Cancel'>
|
||||
</form>
|
||||
@@ -35,7 +40,13 @@
|
||||
<div class="row">
|
||||
<div class="span12">
|
||||
<h2>Result</h2>
|
||||
{% if form.rawjson.data %}
|
||||
<pre><code>{{ result|jsonprint }}</code></pre>
|
||||
{% else %}
|
||||
{% autoescape false %}
|
||||
{{ result|prettyprint}}
|
||||
{% endautoescape %}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
@@ -22,6 +22,35 @@ log = logging.getLogger(__name__)
|
||||
def jsonprint(value):
|
||||
return json.dumps(value, indent=2, separators=(',', ': '))
|
||||
|
||||
def formatvalue(value):
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
elif isinstance(value, list):
|
||||
return ", ".join(value)
|
||||
elif isinstance(value, dict):
|
||||
ret = ""
|
||||
for k in value:
|
||||
ret += k+" => "+formatvalue(value[k])+",<br/>"
|
||||
return ret
|
||||
else:
|
||||
return str(value)
|
||||
|
||||
def prettyprint(value):
|
||||
html = '<table class="ui basic fixed table"><tr>'
|
||||
|
||||
# Get keys
|
||||
for k in value[0]:
|
||||
html += "<th>"+k+"</th>"
|
||||
|
||||
for e in value:
|
||||
html += "<tr>"
|
||||
for k in e:
|
||||
html += "<td>"+formatvalue(e[k])+"</td>"
|
||||
html += "</tr>"
|
||||
|
||||
html += "</tr>"
|
||||
html += "</table>"
|
||||
return(html)
|
||||
|
||||
def get_or_abort(func, *args, **kwargs):
|
||||
"""Execute the function with its arguments and handle the possible
|
||||
|
||||
Reference in New Issue
Block a user