put_table将数据输出为表格
最常用的是将列表、字典输出成表格。
数据是列表:示例1和示例2
数据是字典:示例6和示例7
代码:
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)
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)
# 组合输出
def put_table_case3():
put_table([
['Type', 'Content'],
['html', put_html('X2')],
['text', '
'],
['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)
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)
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)
# 表格数据用字典
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)
# 表头使用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('X2')],
['text', '
'],
['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)