I am running Bottle and python to pull my data from MongoDB. my output comes in JSON object format.
I used to have the following
{u'product': u'mortgage', u'Total_Product': 146533}
{u'product': u'debt collection', u'Total_Product': 65639}
but wanted to get rid of the 'u and get the following format:
'product':'mortgage', Total_Product: 146533
'product':'debt collection' 'Total_Product:65639
After running this code, I am left with a blank screen with no results and no error message. Any suggestion?
# find a single with all aggregates
choices = dict(
aggr1 = db.complaints.aggregate([{"$group":{"_id":"$disputed", "Total_Disputed":{"$sum":1}}},{"$sort":{"Total_Disputed":-1}},{"$project":{"_id":0, "disputed":"$_id", "Total_Disputed":1}}]),
aggr2 = db.complaints.aggregate([{"$group":{"_id":"$product", "Total_Product":{"$sum":1}}},{"$sort":{"Total_Product":-1}},{"$project":{"_id":0, "product":"$_id", "Total_Product":1}}]),
aggr3 = db.complaints.aggregate([{"$group":{"_id":"$response", "Total_Response":{"$sum":1}}},{"$sort":{"Total_Response":-1}},{"$project":{"_id":0, "response":"$_id", "Total_Response":1}}]),
aggr4 = db.complaints.aggregate([{"$group":{"_id":"$submitted", "Total_Submitted":{"$sum":1}}},{"$sort":{"Total_Submitted":-1}},{"$project":{"_id":0, "submitted":"$_id", "Total_Submitted":1}}]),
aggr5 = db.complaints.aggregate([{"$group":{"_id":"$timely", "Total_Timely":{"$sum":1}}},{"$sort":{"Total_Timely":-1}},{"$project":{"_id":0, "Timely":"$_id", "Total_Timely":1}}])
)
def convert_keys_to_string(choices):
"""Recursively converts dictionary keys to strings."""
if not isinstance(choices, dict):
return choices
return dict((str(k), convert_keys_to_string(v))
for k, v in dictionary.items())
aggr1 = 'aggr1'
aggr2 = 'aggr2'
aggr3 = 'aggr3'
aggr4 = 'aggr4'
aggr5 = 'aggr5'
return bottle.template('analytics.tpl', things1 = choices[aggr1], things2 = choices[aggr2], things3 = choices[aggr3], things4 = choices[aggr4], things5 = choices[aggr5])
bottle.debug(True)
bottle.run(host='localhost', port=8082)
the tpl is as follows:
<!DOCTYPE html>
<html>
<head>
<title> @2015 </title>
</head>
<body>
<p>
</p>
<p>
<ul>
%for thing1 in things1:
<li>{{thing1}}</li>
%end
</ul></p>
<p>
<ul>
%for thing2 in things2:
<li>{{thing2}}</li>
%end
</ul></p>
</body>
</html>
kindest regards