kantopark的python实例的实用程序库
kantop的Python项目详细描述
KantoparkPython
这个包包含kantopark的python实用程序函数。里面的函数和类型使得开发python实例更加容易。
安装
pip install kantopy
用法
下面列出了一个典型的函数。
importjsondefmy_func(event):payload=event.json# some operations with the payload later# ...returnjson.dumps(payload)
由于kantopark的python函数必须始终返回一个字符串或字节对象(通过web传输的数据本质上是字节数据),因此使用json.dumps(...)
可能会很乏味。
使用kantopy
,下面是如何重新编写上述内容的方法
fromkantopyimportEvent,as_jsondefmy_func(event:Event):# notice the typings herepayload=event.json# minimize misspelling with typings# some operations later# ...returnas_json(payload)
如果要返回图形(matplotlib或plotly),请使用以下方法。
fromkantopyimportEvent,as_pngfrommatplotlib.figureimportFigurefrommatplotlib.backends.backend_aggimportFigureCanvasAggasFigureCanvasdefcreate_figure():fig=Figure()axis=fig.add_subplot(1,1,1)xs=range(100)ys=[x*2+3forxinxs]axis.plot(xs,ys)# just a straight linereturnfigdefmy_func(event:Event):fig=create_figure()returnas_png(fig)# this would return the output as a png image