1
0

Тайлбар байхгүй

WangWeimin 0adc994926 Merge pull request #5 from wang0618/dev 5 жил өмнө
.github 1938d331e3 workflow: add release to PyPi workflow 5 жил өмнө
demos cbe33d3c0b doc: update demos docs 5 жил өмнө
docs a9860c5ad3 version 0.2.0 🎉 5 жил өмнө
pywebio a9860c5ad3 version 0.2.0 🎉 5 жил өмнө
test f0d622a7f4 test: fix a case 5 жил өмнө
.drone.yml cbc2879536 feat: add demos 5 жил өмнө
.gitattributes 3574109ccf maint: code style and typo fix 5 жил өмнө
.gitignore 7a5169f8c8 update doc 5 жил өмнө
.readthedocs.yml 8087b4c192 doc: fix latex/pdf build error 5 жил өмнө
LICENSE fe95a98a77 add LICENSE 5 жил өмнө
MANIFEST.in 885704f757 update MANIFEST.in 5 жил өмнө
Procfile af694f8db3 add port option to demo app 5 жил өмнө
README.md 5e882ec45c doc: add aiohttp backend doc 5 жил өмнө
requirements.txt ee1fe10e4d feat: support aiohttp backend 🌟 5 жил өмнө
setup.py ee1fe10e4d feat: support aiohttp backend 🌟 5 жил өмнө

README.md

PyWebIO

<em>Write interactive web app in script way.</em>

<a href="https://percy.io/pywebio/pywebio">
    <img src="https://percy.io/static/images/percy-badge.svg" alt="Percy visual test">
</a>
<a href="https://codecov.io/gh/wang0618/PyWebIO">
    <img src="https://codecov.io/gh/wang0618/PyWebIO/branch/dev/graph/badge.svg" />
</a>
<a href="https://pywebio.readthedocs.io/zh_CN/latest/?badge=latest">
    <img src="https://readthedocs.org/projects/pywebio/badge/?version=latest" alt="Documentation Status">
</a>
<a href="https://pypi.org/project/PyWebIO/">
    <img src="https://img.shields.io/pypi/v/pywebio?colorB=brightgreen" alt="Package version">
</a>
<a href="https://pypi.org/project/PyWebIO/">
    <img src="https://img.shields.io/pypi/pyversions/PyWebIO.svg?colorB=brightgreen" alt="Python Version">
</a>
<a href="https://github.com/wang0618/PyWebIO/blob/master/LICENSE">
    <img src="https://img.shields.io/github/license/wang0618/PyWebIO.svg" alt="License">
</a>
<br/>
<a href="https://pywebio.readthedocs.io">[Document]</a> | <a href="http://pywebio-demos.wangweimin.site/">[Demos]</a>

PyWebIO是一个用于在浏览器上获取输入和进行输出的工具库。能够将原有的通过终端交互的脚本快速服务化,供其他人在网络上通过浏览器访问使用; PyWebIO还可以方便地整合进现有的Web服务,让你不需要编写Html和JS代码,就可以构建出具有良好可用性的Web程序。

特点:

  • 使用同步而不是基于回调的方式获取输入,无需在各个步骤之间保存状态,使用更方便
  • 代码侵入性小,对于旧脚本代码仅需修改输入输出逻辑
  • 支持多用户与并发请求
  • 支持结合第三方库实现数据可视化
  • 支持整合到现有的Web服务,目前支持与Flask、Django、Tornado、aiohttp框架集成
  • 同时支持基于线程的执行模型和基于协程的执行模型

Install

PyPi安装:

pip3 install -U pywebio

目前PyWebIO处于快速开发迭代中,PyPi上的包更新可能滞后,建议使用源码安装:

pip3 install -U https://code.aliyun.com/wang0618/pywebio/repository/archive.zip

系统要求: PyWebIO要求 Python 版本在 3.5.2 及以上

Quick start

Hello, world

这是一个使用PyWebIO计算 BMI指数 的脚本:

from pywebio.input import input, FLOAT
from pywebio.output import put_text

def bmi():
    height = input("请输入你的身高(cm):", type=FLOAT)
    weight = input("请输入你的体重(kg):", type=FLOAT)

    BMI = weight / (height / 100) ** 2

    top_status = [(14.9, '极瘦'), (18.4, '偏瘦'),
                  (22.9, '正常'), (27.5, '过重'),
                  (40.0, '肥胖'), (float('inf'), '非常肥胖')]

    for top, status in top_status:
        if BMI <= top:
            put_text('你的 BMI 值: %.1f,身体状态:%s' % (BMI, status))
            break

if __name__ == '__main__':
    bmi()

如果没有使用PywWebIO,这只是一个非常简单的脚本,而通过使用PywWebIO提供的输入输出函数,你可以在浏览器中与代码进行交互:

PyWebIO demo

向外提供服务

上文对使用PyWebIO进行改造的程序,运行模式还是脚本,程序计算完毕后立刻退出。可以使用 pywebio.start_server()bmi() 函数作为Web服务提供:

from pywebio import start_server
from pywebio.input import input, FLOAT
from pywebio.output import put_text

def bmi():
    ...  # bmi() 函数内容不变

if __name__ == '__main__':
    start_server(bmi)

[demo]

与现有Web框架整合

仅需在现有的Tornado应用中加入加入两个 RequestHandler ,就可以将使用PyWebIO编写的函数整合进Tornado应用中

import tornado.ioloop
import tornado.web
from pywebio.platform.tornado import webio_handler
from pywebio import STATIC_PATH

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/", MainHandler),
        (r"/bmi/io", webio_handler(bmi)),  # bmi 即为上文计算BMI指数的函数
        (r"/bmi/(.*)", tornado.web.StaticFileHandler, {"path": STATIC_PATH, 'default_filename': 'index.html'})
    ])
    application.listen(port=80, address='localhost')
    tornado.ioloop.IOLoop.current().start()

http://localhost/bmi/ 页面上就可以计算BMI了

Demos

  • 数据可视化demo : 使用 plotly、pyecharts 等库创建图表
  • 其他demo : 包含PyWebIO基本输入输出演示和使用PyWebIO编写的小应用

Document

使用手册和实现文档见 https://pywebio.readthedocs.io