pywebio最大的好處就是可以像編寫終端腳本一樣編寫web網頁,通過提供一系列的交互函數在瀏覽器的層面上獲取用戶的輸入與輸出。
pycharm 編輯器下運行效果
ipython 編輯器下運行效果
安裝 python web 插件
pip3 install -U pywebio
導入相關模塊
from pywebio.input import *
from pywebio.output import *
from pywebio import start_server
輸入型信息提交
def validate_age(age):
if age < 1:
return "年齡太小了"
elif age > 120:
return "年齡太大了"
else:
pass
name = input('請輸入你的姓名:')
age = input('請輸入你的年齡:', type=NUMBER, validate=validate_age, help_text='必須輸入1到120之間的數字')
sex = select("選擇性別:", ['男', '女'])
rsrv = textarea("請填寫備注信息", rows=3, placeholder='備注信息')
# todo 根據提交的信息處理業務
print(name, age, sex, rsrv)
輸出型信息提交
put_text('輸出輸入的信息:')
put_table(
tdata=[
['序號', '姓名', '年齡', '性別', '備注'],
[1, name, age, sex, rsrv]
]
)
put_table(
tdata=[
['序號', '姓名', '年齡', '性別', '備注'],
[1, 'Python 集中營', 12, '未知', '我是一個專注於知識分享的公眾號']
]
)
put_html(
'<font color="green">公眾號[Python 集中營],我是一個專注於知識分享的公眾號!</font>'
)
arraies = [['列名1', '列名2', '列名3', '列名4', '列名5', '列名6', '列名7', '列名8', '列名9', '列名10'],
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'o'],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'o'],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'o'],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'o'],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'o'],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'o'],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'o'],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'o'], ]
put_table(
tdata=arraies
)
start_server 調起服務
if __name__ == '__main__':
'''start_server 函數啟動web應用'''
start_server(
applications=[app_exec],
reconnect_timeout=3000,
debug=True,
auto_open_webbrowser=True,
remote_access=True
)
【往期精選】
python回調函數能做什么?
解決pyinstaller打包過程中外部資源無法加載的問題 ...
pyqt5做了一個二維碼生成器,已打包成exe可執行程序...
如何在控制台實現一個數據管理系統(包括MYSQL數據庫的增刪改查)
自制文檔格式轉換器,支持 .txt/.xlsx/.csv格式轉換...