使用手册

功能分类

  • 输入模块 pywebio.input
  • 输出模块 pywebio.output
  • 会话相关 pywebio.session
  • 应用部署 pywebio.platform
  • 持续性输入 pywebio.pin

pywebio是一个用python直接渲染web页面的库,主要实现了页面输入,输出的操作,支持多线程/携程. 并且可以整合到主流的python web框架中。功能小巧,比较好玩。

试用了几个例子,直接可以页面展示

  1. 输入

输入包含了表单页面中常用的文本框,下拉选择,文本域,单选,多选,文件上传等功能,可以直接生成表单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from pywebio.input import *
data = input_group("Basic info", [ # 1
input("what's name", type=TEXT, placeholder='This is placeholder',
help_text='This is help text', required=True, name="name"),
input("How old are you?", type=NUMBER, validate=check_age, name="age"), # 2
# Password input
input("Input password", type=PASSWORD, name="password"),

# Drop-down selection
select('Which gift you want?', ['keyboard', 'ipad'], name="gift"),

# Checkbox
checkbox("User Term", options=['I agree to terms and conditions'], name="term"),

# Single choice
radio("Choose one", options=['A', 'B', 'C', 'D'], name="choose"),

# Multi-line text input
textarea('Text Area', rows=3, placeholder='Some text', name="text"),

textarea('Code Edit', code={
'mode': "python",
'theme': 'darcula',
}, value='import something\n# Write your python code', name="code"),
# File Upload
file_upload("Select a image:", accept="image/*", name="file")
])
  1. 输出

输出包含了文本,图片,文件,弹框,Tab切换,进度条,按钮,链接,HTML等等功能。通过输出域Scope,能实现多种页面布局,基本上能完成一个较复杂页面的展示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from pywebio.output import *
# Text Output
put_text("Hello world!")

# Table Output
put_table([ # 1
['Commodity', 'Price'],
['Apple', '5.5'],
['Banana', '7'],
])

# Image Output
put_image(open(r'C:\Program Files (x86)\Microsoft\EdgeCore\97.0.1060.2\VisualElements\Logo.png', 'rb').read()) # local image
# put_image('http://example.com/some-image.png') # internet image

# Markdown Output
put_markdown('~~Strikethrough~~')

# File Output
put_file('hello_word.txt', b'hello word!')

# Show a PopUp
popup('popup title', 'popup text content')

# Show a notification message
toast('New message ')

popup('Popup title', [
put_html('<h3>Popup Content</h3>'),
'plain html: <br/>', # Equivalent to: put_text('plain html: <br/>')
put_table([['A', 'B'], ['C', 'D']]),
put_button('close_popup()', onclick=close_popup)
])

hobby = output('Coding') # equal to output(put_text('Coding')) # 2
put_table([
['Name', 'Hobbies'],
['Wang', hobby] # hobby is initialized to Coding
])
#
hobby.reset('Movie') # hobby is reset to Movie
hobby.append('Music', put_text('Drama')) # append Music, Drama to hobby
hobby.insert(0, put_markdown('**Coding**')) # insert the Coding into the top of the hobby


with put_collapse('This is title'): # 3
for i in range(4):
put_text(i)

put_table([
['Commodity', 'Price'],
['Apple', '5.5'],
['Banana', '7'],
])
  1. 会话

会话可推送文件,运行js代码,注册线程, 不同服务间跳转,协程管理等等操作,具体的参考文档。

  1. 事件回调

PyWebIO允许你输出一些控件并绑定回调函数,当控件被点击时相应的回调函数便会被执行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from functools import partial

def edit_row(choice, row):
put_text("You click %s button ar row %s" % (choice, row))

put_table([
['Idx', 'Actions'],
[1, put_buttons(['edit', 'delete'], onclick=partial(edit_row, row=1))],
[2, put_buttons(['edit', 'delete'], onclick=partial(edit_row, row=2))],
[3, put_buttons(['edit', 'delete'], onclick=partial(edit_row, row=3))],
])

def btn_click(btn_val):
put_text("You click %s button" % btn_val)

put_buttons(['A', 'B', 'C'], onclick=btn_click) # a group of buttons

put_button("Click me", onclick=lambda: toast("Clicked")) # single button

  1. 输出域

PyWebIO使用scope模型来控制内容输出的位置。scope为输出内容的容器,你可以创建一个scope并将内容输出到其中。

1
2
3
4
5
6
7
with use_scope('scope1'):  # 创建并进入scope 'scope1'
put_text('text1 in scope1') # 输出内容到 scope1

put_text('text in parent scope of scope1') # 输出内容到 ROOT scope

with use_scope('scope1'): # 进入之前创建的scope 'scope1'
put_text('text2 in scope1') # 输出内容到 scope1
  1. 持续性输入

用于非阻塞形式的输入,普通输入完成后表单即销毁,持续性输入可以继续接受输入。

  1. 运行
1
start_server(app, port=8080, debug=True)