I have a situation where I can get the data from python so I can only publish via python. I need to visualize this data so I need to subscribe to the channel via JavaScript. My visualization libaray C3.js only takes data into the following format:
message: {
columns: [
['x', volume],
['y', timetamp]
]
}
To get my data like this in python is difficult for me. An easier solution would be to subscribe via JavaScript and again republish the data into my desired format. My question is, is it possible to do that in PUBNUB if so DO I need to publish the data into another channel or the same channel would be ok?
My python code is:
for each in data:
y = each.counter_volume
x = each.timestamp
pubnub.publish(channel='orbit_channel', message=y)
My Desired Output for is
message: {
"columns": [
["x", "2015-07-06T13:26:19", "2015-07-06T13:26:19","2015-07-06T13:26:19"],
["y", 5000.0, 5000.0, 5000.0]
]
}
To get my data like this in python is difficult for me.
That format is JSON and data can be serialized into this pretty easy in Python, try the json
module:
import json
from time import gmtime, strftime
now = str(strftime("%Y-%m-%dT%H:%M:%S", gmtime()))
n = 5000.0
message = {"columns": [["x", now, now, now], ["y", n, n, n]]}
print "message: " + json.dumps(message, indent=4, separators=(',', ': '))
Output:
message: {
"columns": [
[
"x",
"2015-07-16T12:56:47",
"2015-07-16T12:56:47",
"2015-07-16T12:56:47"
],
[
"y",
5000.0,
5000.0,
5000.0
]
]
}
Maybe providing the data like this in the expected way is easier to work with in your JS file?