我正在寻找帮助来创建一个将数据推送的python脚本,特别是以折线图形式的geckoboard。目前我所拥有的代码如下所示,已经从其他来源汇总到一起,如果有人可以合作并帮助我完成这项工作会很棒,我不知道如何在线图上插入我想要的值,如果它可以用一些很好的示例值来完成。感谢
import requests
import json
class Gecko(object):
def __init__(self, api_key):
self.api_key = api_key
def push(self, widget_key, data):
ret = requests.post("https://push.geckoboard.com/v1/send/%s" % widget_key, json.dumps({'api_key' : self.api_key, 'data' : data}), verify=False)
if not (ret.status_code == 200 and ret.json().get('success') == True):
raise ValueError(ret.content)
def line(self, widget_key, values, **kwargs):
data = {'item' : [], 'settings' :kwargs}
for item in values:
data['item'].append(item)
return self.push(widget_key, data)
run=Gecko(****************)
print run.push(150634-85f8db34-af52-4fa3-9963-3200a9a6fe74,some_data?)
print run.line(150634-85f8db34-af52-4fa3-9963-3200a9a6fe74,100,'text')
答案 0 :(得分:3)
修改强> 因此,最大的问题似乎是形成您的JSON有效负载。根据geckoboard API文档,它应该如下所示:
{"api_key" : "some-api-key-goes-here",
"data": {
"y_axis": {
"format": "currency",
"unit": "USD"
},
"series": [
{
"name": "GBP -> USD",
"data": [
1.62529,
1.56991,
1.50420,
1.52265,
1.55356,
1.51930,
1.52148,
1.51173,
1.55170,
1.61966,
1.59255,
1.63762
]
}
]
}
}
在API调用期间,您基本上组装了一个JSON有效负载,然后将其发布到带有密钥的地址。小部件密钥是geckoboard要您发布的唯一地址或URL,api_key是您帐户的密钥。您希望程序运行的流程如下: 1)收集您的数据 2)将您的数据组装成类似JSON的结构(嵌套的字典和列表) 3)转储您的JSON(这意味着将您的python特定结构转换为JSON结构)。 4)将此JSON发布到特定服务器。
这是您更新的代码:
import requests
import json
class Gecko(object):
def __init__(self, widget_key, api_key):
# You put the widget and api_key values here so you can set it once you
# call your object and set it.
self.api_key = api_key
self.widget_key = widget_key
self.payload = {
'api_key' : self.api_key,
'data' : None
}
def push(self):
ret = requests.post(
"https://push.geckoboard.com/v1/send/%s" % self.widget_key,
json.dumps(self.payload),
verify=False
)
if not (ret.status_code == 200 and ret.json().get('success') == True):
raise ValueError(ret.content)
# This function is the batteries included version. It takes all allowable
# values and checks if they have been provided. If so, it'll post.
def line(self, name, data_list, x_axis_labels=None,
x_axis_type=None, y_axis_format=None, y_axis_unit=None):
_data = {"series": [{"name": name,
"data": data_list}]}
# Add x_axis to _data if there is an additional param.
if x_axis_labels is not None or x_axis_type is not None:
_data["x_axis"] = None
if x_axis_labels is not None:
_data["x_axis"]["labels"] = x_axis_labels
if x_axis_type is not None:
_data["x_axis"]["type"] = x_axis_type
# Add y_axis to _data if there are y_axis params.
if y_axis_format is not None or y_axis_unit is not None:
_data['y_axis'] = None
if y_axis_format is not None:
_data["y_axis"]["format"] = y_axis_format
if y_axis_unit is not None:
_data["y_axis"]["unit"] = y_axis_unit
self.payload["data"] = _data
# Usage:
# Step 1: Form your object and assign it to a variable. The class now requires
# widget_key and API key to start off with.
line_widget = Gecko(
widget_key="12345-12315-asfdasdf-asdfasfd",
api_key="1234-1234-1234-1234")
# Step 2: Add data to the payload:
line_widget.line(name="Something Line graph",
data_list=[1,2,3,4,5,6,7],
x_axis_labels=["one", "two", "three", "four", "five", "six", "seven"])
# Step 3: Push the data
line_widget.push()
很抱歉,我无法测试代码是否正常运行,但我相信它有95%的存在。如果您正在寻找一种更简单的方法,我构建了一个库,用于处理形成JSON并将数据推送到geckoboard上的自定义小部件。
您可以查看Geckopush,如果您有任何疑问,请与我们联系。它现在处于一个有点稳定的版本,我正在积极研究它。
答案 1 :(得分:1)
解决方案:
文件名为test.py
import requests
import json
class Gecko(object):
def __init__(self, api_key):
self.api_key = api_key
def push(self,data):
ret = requests.post("paste URL here", data=json.dumps(data), verify=False)
if not (ret.status_code == 200 and ret.json().get('success') == True):
raise ValueError(ret.content)
文件名为test1.py
from test import Gecko
gecko=Gecko("*************")
gecko.push({
"api_key": "**************",
"data": {
"x_axis": {
"labels": ["Week 1", "Week 2", "Week 3", "Week 4"]
},
"series": [
{
"data": [
10000, 13500, 21000, 1900
]
}
]
}
})