PyWebIO output之put_table用法

put_table将数据输出为表格
最常用的是将列表、字典输出成表格。
数据是列表:示例1和示例2
数据是字典:示例6和示例7

示例1

代码:

def put_table_case1():
    # 列表中第一个列表元素是表头
    put_table([
        ['Commodity', 'Price'],
        ['Apple', '5.5'],
        ['Banana', '7'],
    ])

if __name__ == '__main__':
    start_server(put_table_case1, port=19003, auto_open_webbrowser=False)

效果:
在这里插入图片描述

示例2


def put_table_case2():
    # 表头单独在header中声明
    put_table([
        ['Wang', 'M', 'China'],
        ['Liu', 'W', 'America'],
    ], header=['Name', 'Gender', 'Address'])

if __name__ == '__main__':
    start_server(put_table_case2, port=19003, auto_open_webbrowser=False)

效果:
在这里插入图片描述

示例3

# 组合输出
def put_table_case3():
    put_table([
        ['Type', 'Content'],
        ['html', put_html('X<sup>2</sup>')],
        ['text', '<hr/>'],
        ['buttons', put_buttons(['A', 'B'], onclick=put_text)],
        ['markdown', put_markdown('`Awesome PyWebIO!`')],
        ['file', put_file('hello.text', b'hello world')],
        ['table', put_table([['A', 'B'], ['C', 'D']])]
    ])


if __name__ == '__main__':
    start_server(put_table_case3, port=19003, auto_open_webbrowser=False)

效果:
在这里插入图片描述

示例4

def put_table_case4():
    # Name和Address是表头,Name第一列前两行单元格合并。Address第1行的第2列和第3列合并。
    # City和country是Address的副表头
    put_table([
        [span('Name', row=2), span('Address', col=2)],
        ['City', 'Country'],
        ['Wang', 'Beijing', 'China'],
        ['Liu', 'New York', 'America'],
    ])


if __name__ == '__main__':
    start_server(put_table_case4, port=19003, auto_open_webbrowser=False)

效果:
在这里插入图片描述

示例5

def put_table_case5():
    put_table([
        ['C'],
        [span('E', col=2)],  # 'E' across 2 columns
    ], header=[span('A', row=2), 'B']).show()  # 'A' across 2 rows


if __name__ == '__main__':
    start_server(put_table_case5, port=19003, auto_open_webbrowser=False)

效果:
在这里插入图片描述

示例6

# 表格数据用字典
def put_table_case6():
    put_table([
        {"Course": "OS", "Score": "80"},
        {"Course": "DB", "Score": "93"},
    ], header=["Course", "Score"])


if __name__ == '__main__':
    start_server(put_table_case6, port=19003, auto_open_webbrowser=False)

效果:
在这里插入图片描述

示例7

# 表头使用markdown样式
def put_table_case7():
    put_table([
        {"Course": "OS", "Score": "80"},
        {"Course": "DB", "Score": "93"},
    ], header=[(put_markdown("*Course*"), "Course"), (put_markdown("*Score*") ,"Score")] )


if __name__ == '__main__':
    start_server(put_table_case7, port=19003, auto_open_webbrowser=False)

效果:
在这里插入图片描述

所有代码

from pywebio import start_server
from pywebio.output import put_table, span, put_markdown, put_buttons, put_html, put_file, put_text


def put_table_case1():
    # 列表中第一个列表元素是表头
    put_table([
        ['Commodity', 'Price'],
        ['Apple', '5.5'],
        ['Banana', '7'],
    ])


def put_table_case2():
    # 表头单独在header中声明
    put_table([
        ['Wang', 'M', 'China'],
        ['Liu', 'W', 'America'],
    ], header=['Name', 'Gender', 'Address'])


# 组合输出
def put_table_case3():
    put_table([
        ['Type', 'Content'],
        ['html', put_html('X<sup>2</sup>')],
        ['text', '<hr/>'],
        ['buttons', put_buttons(['A', 'B'], onclick=put_text)],
        ['markdown', put_markdown('`Awesome PyWebIO!`')],
        ['file', put_file('hello.text', b'hello world')],
        ['table', put_table([['A', 'B'], ['C', 'D']])]
    ])


def put_table_case4():
    # Name和Address是表头,Name第一列前两行单元格合并。Address第1行的第2列和第3列合并。
    # City和country是Address的副表头
    put_table([
        [span('Name', row=2), span('Address', col=2)],
        ['City', 'Country'],
        ['Wang', 'Beijing', 'China'],
        ['Liu', 'New York', 'America'],
    ])


def put_table_case5():
    put_table([
        ['C'],
        [span('E', col=2)],  # 'E' across 2 columns
    ], header=[span('A', row=2), 'B']).show()  # 'A' across 2 rows


# 表格数据用字典
def put_table_case6():
    put_table([
        {"Course": "OS", "Score": "80"},
        {"Course": "DB", "Score": "93"},
    ], header=["Course", "Score"])


# 表头使用markdown样式
def put_table_case7():
    put_table([
        {"Course": "OS", "Score": "80"},
        {"Course": "DB", "Score": "93"},
    ], header=[(put_markdown("*Course*"), "Course"), (put_markdown("*Score*"), "Score")])


if __name__ == '__main__':
    start_server(put_table_case7, port=19003, auto_open_webbrowser=False)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Summer@123

不积跬步无以至千里,感谢支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值