首页 > 代码库 > Jupyter 服务开发指南

Jupyter 服务开发指南

1. 取kylin 数据

import requests
import pandas as pd

def getDtu(dtuid,addr):
    sqlData = http://www.mamicode.com/‘{"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    = http://www.mamicode.com/sqlData,"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 = http://www.mamicode.com/getDtuStd(dtuid,addr)"{\"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 = http://www.mamicode.com/getDTU(dtuid,addr)"%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 服务开发指南