1
0

output.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import json
  2. import logging
  3. from collections.abc import Mapping
  4. from base64 import b64encode
  5. from .framework import Global, Task
  6. from .input_ctrl import send_msg, single_input, input_control, next_event, run_async
  7. from .output_ctl import register_callback
  8. import asyncio
  9. import inspect
  10. def set_title(title):
  11. send_msg('output_ctl', dict(title=title))
  12. def set_output_fixed_height(enabled=True):
  13. send_msg('output_ctl', dict(output_fixed_height=enabled))
  14. def set_auto_scroll_bottom(enabled=True):
  15. send_msg('output_ctl', dict(auto_scroll_bottom=enabled))
  16. def text_print(text, *, ws=None):
  17. if text is None:
  18. text = ''
  19. msg = dict(command="output", spec=dict(content=text, type='text'))
  20. (ws or Global.active_ws).write_message(json.dumps(msg))
  21. def json_print(obj):
  22. text = "```\n%s\n```" % json.dumps(obj, indent=4, ensure_ascii=False)
  23. text_print(text)
  24. def put_markdown(mdcontent, strip_indent=0, lstrip=False):
  25. """
  26. 输出Markdown内容。当在函数中使用Python的三引号语法输出多行内容时,为了排版美观可能会对Markdown文本进行缩进,
  27. 这时候,可以设置strip_indent或lstrip来防止Markdown错误解析
  28. :param mdcontent: Markdown文本
  29. :param strip_indent: 去除行开始的缩进空白数。
  30. :param lstrip: 是否去除行开始的空白。
  31. :return:
  32. """
  33. if strip_indent:
  34. lines = (
  35. i[:strip_indent] if (i[:strip_indent] == ' ' * strip_indent) else i
  36. for i in mdcontent.splitlines()
  37. )
  38. mdcontent = '\n'.join(lines)
  39. if lstrip:
  40. lines = (i.lstrip() for i in mdcontent.splitlines())
  41. mdcontent = '\n'.join(lines)
  42. text_print(mdcontent)
  43. def put_table(tdata, header=None):
  44. """
  45. 输出表格
  46. :param tdata: list of list|dict
  47. :param header: 列表,当tdata为字典列表时,header指定表头顺序
  48. :return:
  49. """
  50. if header:
  51. tdata = [
  52. [row.get(k, '') for k in header]
  53. for row in tdata
  54. ]
  55. def quote(data):
  56. return str(data).replace('|', r'\|')
  57. # 防止当tdata只有一行时,无法显示表格
  58. if len(tdata) == 1:
  59. tdata[0:0] = [' '] * len(tdata[0])
  60. header = "|%s|" % "|".join(map(quote, tdata[0]))
  61. res = [header]
  62. res.append("|%s|" % "|".join(['----'] * len(tdata[0])))
  63. for tr in tdata[1:]:
  64. t = "|%s|" % "|".join(map(quote, tr))
  65. res.append(t)
  66. text_print('\n'.join(res))
  67. def _format_button(buttons):
  68. """
  69. 格式化按钮参数
  70. :param buttons: button列表, button可用形式:
  71. {value:, label:, }
  72. (value, label,)
  73. value 单值,label等于value
  74. :return: [{value:, label:, }, ...]
  75. """
  76. btns = []
  77. for btn in buttons:
  78. if isinstance(btn, Mapping):
  79. assert 'value' in btn and 'label' in btn, 'actions item must have value and label key'
  80. elif isinstance(btn, list):
  81. assert len(btn) == 2, 'actions item format error'
  82. btn = dict(zip(('value', 'label'), btn))
  83. else:
  84. btn = dict(value=btn, label=btn)
  85. btns.append(btn)
  86. return btns
  87. def td_buttons(buttons, onclick, save=None, mutex_mode=False):
  88. """
  89. 在表格中显示一组按钮
  90. 参数含义同 buttons 函数
  91. :return:
  92. """
  93. btns = _format_button(buttons)
  94. callback_id = register_callback(onclick, save, mutex_mode)
  95. tpl = '<button type="button" value="{value}" class="btn btn-primary btn-sm" ' \
  96. 'onclick="WebIO.DisplayAreaButtonOnClick(this, \'%s\')">{label}</button>' % callback_id
  97. btns_html = [tpl.format(**b) for b in btns]
  98. return ' '.join(btns_html)
  99. def buttons(buttons, onclick, small=False, save=None, mutex_mode=False):
  100. """
  101. 显示一组按钮
  102. :param buttons: button列表, button可用形式: value 只能为字符串
  103. {value:, label:, }
  104. (value, label,)
  105. value 单值,label等于value
  106. :param onclick: CallBack(btn_value, save) CallBack can be generator function or coroutine function
  107. :param save:
  108. :param mutex_mode: 互斥模式,回调在运行过程中,无法响应同一回调,仅当onclick为协程函数时有效
  109. :return:
  110. """
  111. btns = _format_button(buttons)
  112. callback_id = register_callback(onclick, save, mutex_mode)
  113. send_msg('output', dict(type='buttons', callback_id=callback_id, buttons=btns, small=small))
  114. def put_file(name, content):
  115. """
  116. :param name: file name
  117. :param content: bytes-like object
  118. :return:
  119. """
  120. content = b64encode(content).decode('ascii')
  121. send_msg('output', dict(type='file', name=name, content=content))