How to output JSON in python so that it can be used for Geckoboard Highcharts plugin

I feel that there is a fairly simple solution to my problem.

I am doing some manipulation of data that eventually prints in highcharts format. I am currently pulling out the entire set of nested dictionaries and printing every part, but I was hoping there was something like JSON.dumps (dict) where the output was formatted with all keys without quotes. So in code-ish-stuff:

{ 'chart': {'backgroundColor': 'Blue', 'borderColor': 'Black', 'renderTo': 'container'}, 'xAxis': { ... }, ... } 

Conclusion on

 { chart: { backgroundColor: 'Blue', borderColor: 'Black', renderTo: 'container'}, xAxis: { ... }, ... } 

If I cannot conclude, is this a good way to interact with HighCharts from python? In fact, I did not come across this, despite some reasonable features of Google-Fu.

EDIT: I am working to make it compatible with Geckoboard - the Highcharts plugin where I do not get access to the full javascript feature for parsing output. I need it to be already formatted and ready to be uploaded when I send data.

+4
source share
2 answers

Just provide valid json text. The examples in your question are not valid json. Using the Click API :

 #!/usr/bin/env python import json import urllib2 try: r = urllib2.urlopen("https://push.geckoboard.com/v1/send/" + widget_key, json.dumps(nested_dict)) except IOError as e: if hasattr(e, 'reason'): print "connection error:", e.reason elif hasattr(e, 'code'): print "http error:", e.code print e.read() else: print "error:", e else: # success assert json.load(r)["success"] 
+4
source

Yes. You must decode the string on the client side (using javascript):

 JSON.parse('{"background": "black"}') 

This method will return you a javascript object that you can pass to Highcharts.

So you need to save python output to js string and then convert it to JS object.

+1
source

Source: https://habr.com/ru/post/1435707/


All Articles