我正在寻找帮助创建一个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')发布于 2015-09-27 09:25:55
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有效负载,然后使用密钥将其发送到一个地址。widget-key是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,如果你有任何问题,请告诉我。它现在是一个比较稳定的版本,我正在积极地开发它。
发布于 2015-06-30 21:20:20
解决方案:
名为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
]
}
]
}
})https://stackoverflow.com/questions/31133708
复制相似问题