目 录CONTENT

文章目录

使用pywebio快速构建web应用-pywebio.md

phyger
2025-09-24 / 1 评论 / 1 点赞 / 60 阅读 / 868 字 / 正在检测是否收录...

效果图

更多用法

input

# 输入框
input_res = input("please input your name:")
print('browser input is:', input_res)

# 密码框
pwd_res = input("please input your password:",type=PASSWORD)
print('password:', pwd_res)

# 下拉框
select_res = select("please select your city:",['北京','西安','成都'])
print('your city is:',select_res)

# checkbox
checkbox_res = checkbox("please confirm the checkbox:",options=['agree','disagree'])
print('checkbox:', checkbox_res)

# 文本框
text_res = textarea("please input what you want to say:",rows=3,placeholder='...')
print('what you said is:',text_res)

# 文件上传
upload_res = file_upload("please upload what you want to upload:",accept="image/*")

with open(upload_res.get('filename'),mode='wb') as f: # 因为读取的图片内容是二进制,所以要以wb模式打开
    f.write(upload_res.get('content'))
print('what you uploaded is:',upload_res.get('filename'))

# 滑动条

sld = slider('这是滑动条',help_text='请滑动选择')  # 缺点是不能显示当前滑动的值
toast('提交成功')
print(sld)

# 单选选项

radio_res = radio(
    '这是单选',
    options=['西安','北京','成都']
    )
print(radio_res)

# 更新输入项

Country2City={
    'China':['西安','北京','成都'],
    'USA': ['纽约', '芝加哥', '佛罗里达'],
}

countries = list(Country2City.keys())

update_res = input_group(
    "国家和城市联动",
    [
        # 当国家发生变化的时候,onchange触发input_update方法去更新name=city的选项,更新内容为Country2City[c],c代表国家的选项值
        select('国家',options=countries,name='country',onchange=lambda c: input_update('city',options=Country2City[c])),
        select('城市',options=Country2City[countries[0]],name='city')
    ]
)

print(update_res)

输入框

密码框

选择框

勾选框

文本框

文件上传

滑动条

单选框

输入框联动-1

输入框联动-2

output

# 文本输出
put_text('这是输出的内容')

# 表格输出
put_table(
    tdata=[
        ['序号','名称'],
        [1,'中国'],
        [2,'美国']
    ]
)

# MarkDown输出
put_markdown('~~删除线~~')

# 文件输出
put_file('秘籍.txt','降龙十八掌')

# 按钮输出
put_buttons(
    buttons=['A','B'],
    onclick=toast
)

效果

部分高级用法

# ========================== 1-输入框的参数 ==============================

# input的更多参数
ipt = input(
    'This is label',
    type=TEXT,
    placeholder='This is placeholder',  # 占位
    help_text='This is help text',  # 提示
    required=True,                  # 必填
    datalist=['a1', 'b2', 'c3'])    # 常驻输入联想
print('what you input is:',ipt)

# =========================== 2-输入框自定义校验 =============================

# input的合法性校验
# 自定义校验函数

def check_age(n):
    if n<1:
        return "Too Small!@"
    if n>100:
        return "Too Big!@"
    else:
        pass

myAge = input('please input your age:',type=NUMBER,validate=check_age,help_text='must in 1,100')
print('myAge is:',myAge)

# ============================ 3-代码编辑 ============================

# textare的代码模式

code = textarea(
    label='这是代码模式',
    code={
        'mode':'python',
        'theme':'darcula',
    },
    value='import time\n\ntime.sleep(2)'
    )
print('code is:',code)

# ============================== 4-输入组 ==========================

def check_age(n):
    if n<1:
        return "Too Small!@"
    if n>100:
        return "Too Big!@"
    else:
        pass

def check_form(datas):
    print(datas)
    if datas.get("age")==1:
        #return 'you are only one years old!'
        return ('age','you are only one years old!')
    if len(datas.get("name"))<=3:
        return ('name','Name Too short!!!')

# 输入组
datas = input_group(
    "It's input groups...",
    inputs=[
        input('please input name',name='name'),
        input('please input age',name='age',type=NUMBER,validate=check_age)
    ],
    validate=check_form
    )

# ====================================== 5-输入框的action =========================================

import time

def set_today(set_value):
    set_value(time.time())
    print(time.time())

tt = input('选择时间',action=('Today',set_today),readonly=True)
print(tt)

# ====================================== 5-输入框的弹窗 =========================================

def set_some(set_value):  # 此方法可以将选择的英文转换为中文
    with popup('It is popup'):    # popup 是 output 模块中的方法
        put_buttons(['Today','Tomorrow'],onclick=[lambda: set_value('今天','Today1'),lambda:set_value('明天','Tomorrow2')])   # set_value('今天','Today') 按Today的按钮输入Today1,实际对应:今天
        put_buttons(['Exit'],onclick=lambda _: close_popup())

pp = input('go popup',type=TEXT,action=('按钮弹窗',set_some))
print(pp)

联想词常驻+help_text

代码编辑模式

输入组

输入框的action

输入框的弹窗

按钮的onclick

代码中的 print 内容会在后台控制台打印出来。

参考

以上就是今天的全部内容了,感谢您的阅读,我们下节再会。

1

评论区