Write interactive web app in script way.
Project description
PyWebIO
Write interactive web app in script way.
[Document] | [Demos] | [Why PyWebIO?]
PyWebIO provides a series of imperative functions to obtain user input and output on the browser, turning the browser into a "rich text terminal", and can be used to build simple web applications or browser-based GUI applications without the need to have knowledge of HTML and JS. PyWebIO can also be easily integrated into existing Web services. PyWebIO is very suitable for quickly building applications that do not require complex UI.
Features:
- Use synchronization instead of callback-based method to get input
- Non-declarative layout, simple and efficient
- Less intrusive: old script code can be transformed into a Web application only by modifying the input and output operation
- Support integration into existing web services, currently supports Flask, Django, Tornado, aiohttp framework
- Support for
asyncio
and coroutine - Support data visualization with third-party libraries, e.g.,
plotly
,bokeh
,pyecharts
.
Installation
Stable version:
pip3 install -U pywebio
Development version:
pip3 install -U https://code.aliyun.com/wang0618/pywebio/repository/archive.zip
Prerequisites: PyWebIO requires Python 3.5.2 or newer
Quick start
Hello, world
Here is a simple PyWebIO script to calculate the BMI:
from pywebio.input import input, FLOAT
from pywebio.output import put_text
def bmi():
height = input("Your Height(cm):", type=FLOAT)
weight = input("Your Weight(kg):", type=FLOAT)
BMI = weight / (height / 100) ** 2
top_status = [(14.9, 'Severely underweight'), (18.4, 'Underweight'),
(22.9, 'Normal'), (27.5, 'Overweight'),
(40.0, 'Moderately obese'), (float('inf'), 'Severely obese')]
for top, status in top_status:
if BMI <= top:
put_text('Your BMI: %.1f, category: %s' % (BMI, status))
break
if __name__ == '__main__':
bmi()
This is just a very simple script if you ignore PyWebIO, but using the input and output functions provided by PyWebIO, you can interact with the code in the browser [demo]:
Serve as web service
The above BMI program will exit immediately after the calculation, you can use pywebio.start_server()
to publish the bmi()
function as a web application:
from pywebio import start_server
from pywebio.input import input, FLOAT
from pywebio.output import put_text
def bmi(): # bmi() keep the same
...
if __name__ == '__main__':
start_server(bmi, port=80)
Integration with web framework
To integrate PyWebIO application into Tornado, all you need is adding a RequestHandler
to the existing Tornado application:
import tornado.ioloop
import tornado.web
from pywebio.platform.tornado import webio_handler
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
if __name__ == "__main__":
application = tornado.web.Application([
(r"/", MainHandler),
(r"/bmi", webio_handler(bmi)), # bmi is the same function as above
])
application.listen(port=80, address='localhost')
tornado.ioloop.IOLoop.current().start()
Now, you can open http://localhost/bmi
for BMI calculation.
For integration with other web frameworks, please refer to document.
Demos
- Basic demo : PyWebIO basic input and output demos and some small applications written using PyWebIO.
- Data visualization demo : Data visualization with the third-party libraries, e.g.,
plotly
,bokeh
,pyecharts
.
Document
Document is on https://pywebio.readthedocs.io
PyWebIO
Write interactive web app in script way.
[Document] | [Demos] | [Why PyWebIO?]
PyWebIO提供了一系列命令式的交互函数来在浏览器上获取用户输入和进行输出,将浏览器变成了一个“富文本终端”,可以用于构建简单的Web应用或基于浏览器的GUI应用。 PyWebIO还可以方便地整合进现有的Web服务,让你不需要编写HTML和JS代码,就可以构建出具有良好可用性的应用。
功能特性:
- 使用同步而不是基于回调的方式获取输入,代码编写逻辑更自然
- 非声明式布局,布局方式简单高效
- 代码侵入性小,旧脚本代码仅需修改输入输出逻辑便可改造为Web服务
- 支持整合到现有的Web服务,目前支持与Flask、Django、Tornado、aiohttp框架集成
- 同时支持基于线程的执行模型和基于协程的执行模型
- 支持结合第三方库实现数据可视化
Installation
稳定版安装:
pip3 install -U pywebio
开发版安装:
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()
如果没有使用PyWebIO,这只是一个非常简单的脚本,而通过使用PyWebIO提供的输入输出函数,你可以在浏览器中与代码进行交互 [demo]:
作为Web服务提供
上文BMI程序会在计算完毕后立刻退出,可以使用 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, port=80)
与现有Web框架整合
Tornado应用整合:仅需在现有的Tornado应用中添加一个 RequestHandler
,就可以将PyWebIO应用整合进Tornado Web服务中
import tornado.ioloop
import tornado.web
from pywebio.platform.tornado import webio_handler
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
if __name__ == "__main__":
application = tornado.web.Application([
(r"/", MainHandler),
(r"/bmi", webio_handler(bmi)), # bmi 即为上文计算BMI指数的函数
])
application.listen(port=80, address='localhost')
tornado.ioloop.IOLoop.current().start()
在 http://localhost/bmi
页面上就可以计算BMI了。
与其他Web框架整合请见文档
Demos
Document
使用手册和实现文档见 https://pywebio.readthedocs.io
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pywebio-1.2.3.tar.gz
.
File metadata
- Download URL: pywebio-1.2.3.tar.gz
- Upload date:
- Size: 317.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3470ab8e8ea2fdfb564cca85221ab8fec457e00260c5e7f2f57640683f2f4a3 |
|
MD5 | de53146832b21dae433d537205938151 |
|
BLAKE2b-256 | c3bb7d1c997292bd00e4c9dd8877b1b0f546f1f6766ebbdd2936d1418a0f27b7 |