Jupyter 服务开发指南
Posted 疯吻IT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jupyter 服务开发指南相关的知识,希望对你有一定的参考价值。
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 服务开发指南的主要内容,如果未能解决你的问题,请参考以下文章
最全指南如何在 Jupyter Notebook 中切换/使用 conda 虚拟环境?