码迷,mamicode.com
首页 > 其他好文 > 详细

Jupyter 服务开发指南

时间:2016-09-05 17:09:23      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

1. 取kylin 数据

import requests
import pandas as pd

def getDtu(dtuid,addr):
    sqlData = ‘{ "sql":"select * from dtu where dtuid=\‘%s\‘ and addr=\‘%s\‘ order by DTUTIME desc", "project":"yongli" , "offset":0, "limit":100}‘ %(dtuid, addr)
    response = requests.post(url     = ‘http://kylin1.wdp:7070/kylin/api/query‘,
                             data    = sqlData,
                             auth    = (‘admin‘, ‘admin‘), 
                             headers = {"Content-Type":"application/json"})
    dfCols = pd.DataFrame(response.json()["columnMetas"])
    df = pd.DataFrame(response.json()["results"], columns=dfCols["label"].values)

    values = pd.DataFrame({ ‘dtutime‘: df["DTUTIME"].map(pd.Timestamp),
                            addr: df["DTUVALUE"]})
    
    return values

getDtu(‘8627427973‘, ‘1800‘)

?


2. 合并行

from pandas import Series, DataFrame
import pandas as pd

def getDtuStd(dtuid, addrs):
    addrList = addrs.strip().split(‘,‘)
    result = pd.DataFrame({‘addr‘:[],
                           ‘std‘:[]})
    
    for i in addrList:
        std = getStd(dtuid, i)
        result = result.append(std, ignore_index=True)
    
    return result
    
getDtuStd(‘8627427973‘, ‘1820,1810,0004‘)

?

3. 合并列

import pandas as pd

def getStdJson(dtuid,addr=‘0002,0004,1019,101A,101B,101C,101D,1023,1024,1025,1800,1802,1804,1806,1808,180A,180C,180E,1810,1812,1814,1816,1818,181A,181C,181E,1820,1822,1824,1826,1828,182A,182C,182E,1830,2000,2002,2004,2006,2008,200A,200C,200E,2100,2102,2104,2106,2108,210A,210C,210E‘):
    data = getDtuStd(dtuid,addr)
    comments = pd.DataFrame({‘addr‘:[‘0002‘,‘0004‘,‘1019‘,‘101A‘,‘101B‘,‘101C‘,‘101D‘,‘1023‘,‘1024‘,‘1025‘,‘1800‘,‘1802‘,‘1804‘,‘1806‘,‘1808‘,‘180A‘,‘180C‘,‘180E‘,‘1810‘,‘1812‘,‘1814‘,‘1816‘,‘1818‘,‘181A‘,‘181C‘,‘181E‘,‘1820‘,‘1822‘,‘1824‘,‘1826‘,‘1828‘,‘182A‘,‘182C‘,‘182E‘,‘1830‘,‘2000‘,‘2002‘,‘2004‘,‘2006‘,‘2008‘,‘200A‘,‘200C‘,‘200E‘,‘2100‘,‘2102‘,‘2104‘,‘2106‘,‘2108‘,‘210A‘,‘210C‘,‘210E‘],
                            ‘comment‘:[‘电压变比‘,‘电流变比‘,‘A相功率因数‘,‘B相功率因数‘,‘C相功率因数‘,‘总功率因数‘,‘频率‘,‘A相相角‘,‘B相相角‘,‘C相相角‘,‘A相电压‘,‘B相电压‘,‘C相电压‘,‘平均相电压‘,‘AB线电压‘,‘BC线电压‘,‘CA线电压‘,‘平均线电压‘,‘A相电流‘,‘B相电流‘,‘C相电流‘,‘平均电流‘,‘零线电流‘,‘A相有功功率‘,‘B相有功功率‘,‘C相有功功率‘,‘总有功功率‘,‘A相无功功率‘,‘B相无功功率‘,‘C相无功功率‘,‘总无功功率‘,‘A相视在功率‘,‘B相视在功率‘,‘C相视在功率‘,‘总视在功率‘,‘A相正向有功电能‘,‘B相正向有功电能‘,‘C相正向有功电能‘,‘总正向有功电能‘,‘A相正向无功电能‘,‘B相正向无功电能‘,‘C相正向无功电能‘,‘总正向无功电能‘,‘A相反向有功电能‘,‘B相反向有功电能‘,‘C相反向有功电能‘,‘总反向有功电能‘,‘A相反向无功电能‘,‘B相反向无功电能‘,‘C相反向无功电能‘,‘总反向无功电能‘]})
    
    result = pd.merge(data, comments, on=‘addr‘)
    #print result
    return "{\"code\":200,\"message\":\"SUCCESS\",\"data\":" + result.to_json(orient=‘records‘,force_ascii=False) + "}"
    
#getStdJson(‘8627427973‘, ‘1820,1810,0004‘)?

?


4. 画图

%matplotlib inline
import matplotlib.pyplot as plt, mpld3
from matplotlib.ticker import MultipleLocator, FuncFormatter
import matplotlib.dates as mdate

def drawDTU(dtuid,addr):
    #print "------- ENTER drawDTU (%s)-------" %dtuid
    
    data = getDTU(dtuid,addr)
    fig, ax = plt.subplots(figsize=(5,3))
    ax.plot(data[0], data[1], ‘-‘,label="%s" %addr, color = ‘blue‘)
    
    majorLocator = MultipleLocator(5)
    majorFormatter = mdate.DateFormatter(‘%H‘)
    minorLocator = MultipleLocator(1)
    ax.xaxis.set_major_locator(majorLocator)
    ax.xaxis.set_major_formatter(majorFormatter)

    # for the minor ticks, use no labels; default NullFormatter
    ax.xaxis.set_minor_locator(minorLocator)
    
    #plt.xlabel("Date")
    #plt.ylabel("Value")
    #plt.title("DTU Monitor")
    plt.legend(loc=‘upper center‘, bbox_to_anchor=(0.5,0.98),ncol=3,fancybox=True,shadow=True)
    ax.grid(color="lightgray", alpha=0.7)
    #fig.set_size_inches(4, 4)
    #plt.show()
    
    html = mpld3.fig_to_html(fig)
    return html


#drawDTU(‘8627427973‘, ‘0004‘)
?

5. 发布服务

from flask import Flask, make_response, request
app = Flask(__name__)

@app.route("/dtustd/", methods=["GET","OPTIONS"])
def dtuStd(dtuid):
    #o = drawDtuStd(dtuid)
    o = getStdJson(dtuid)
    resp = make_response(o)
    resp.headers["Access-Control-Allow-Origin"] = "*"
    resp.headers["Access-Control-Request-Method"] = "POST,GET,PUT,DELETE,OPTIONS"
    resp.headers["Access-Control-Allow-Methods"] = "POST,GET,PUT,DELETE,OPTIONS"
    resp.headers["Access-Control-Allow-Headers"] = "X-Requested-With,Content-Type"
    
    if request.method == ‘OPTIONS‘:
        print "it‘s OPTIONS"
    return resp

app.run(host="0.0.0.0", port=5007)

Jupyter 服务开发指南

标签:

原文地址:http://www.cnblogs.com/fengwenit/p/5842627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!