output.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. _AnchorTPL = 'pywebio-anchor-%s'
  17. def set_anchor(name):
  18. """
  19. 在当前输出处标记锚点。 若已经存在name锚点,则先将旧锚点删除
  20. """
  21. inner_ancher_name = _AnchorTPL % name
  22. send_msg('output_ctl', dict(set_anchor=inner_ancher_name))
  23. def clear_before(anchor):
  24. """清除anchor锚点之前输出的内容"""
  25. inner_ancher_name = _AnchorTPL % anchor
  26. send_msg('output_ctl', dict(clear_before=inner_ancher_name))
  27. def clear_after(anchor):
  28. """清除anchor锚点之后输出的内容"""
  29. inner_ancher_name = _AnchorTPL % anchor
  30. send_msg('output_ctl', dict(clear_after=inner_ancher_name))
  31. def clear_range(start_anchor, end_ancher):
  32. """
  33. 清除start_anchor-end_ancher锚点之间输出的内容.
  34. 若 start_anchor 或 end_ancher 不存在,则不进行任何操作
  35. """
  36. inner_start_anchor_name = 'pywebio-anchor-%s' % start_anchor
  37. inner_end_ancher_name = 'pywebio-anchor-%s' % end_ancher
  38. send_msg('output_ctl', dict(clear_range=[inner_start_anchor_name, inner_end_ancher_name]))
  39. def scroll_to(anchor):
  40. """将页面滚动到anchor锚点处"""
  41. inner_ancher_name = 'pywebio-anchor-%s' % anchor
  42. send_msg('output_ctl', dict(scroll_to=inner_ancher_name))
  43. def _put_content(type, ws=None, anchor=None, before=None, after=None, **other_spec):
  44. """
  45. 向浏览器输出内容
  46. :param type:
  47. :param content:
  48. :param ws:
  49. :param before:
  50. :param after:
  51. :return:
  52. """
  53. assert not (before and after), "Parameter 'before' and 'after' cannot be specified at the same time"
  54. spec = dict(type=type)
  55. spec.update(other_spec)
  56. if anchor:
  57. spec['anchor'] = _AnchorTPL % anchor
  58. if before:
  59. spec['before'] = _AnchorTPL % before
  60. elif after:
  61. spec['after'] = _AnchorTPL % after
  62. msg = dict(command="output", spec=spec)
  63. (ws or Global.active_ws).write_message(json.dumps(msg))
  64. def text_print(text, inline=False, ws=None, anchor=None, before=None, after=None):
  65. """
  66. 输出文本内容
  67. :param text:
  68. :param ws:
  69. :param before:
  70. :param after:
  71. :return:
  72. """
  73. _put_content('text', content=text, inline=inline, ws=ws, anchor=anchor, before=before, after=after)
  74. def put_html(html, anchor=None, before=None, after=None):
  75. _put_content('html', content=html, anchor=anchor, before=before, after=after)
  76. def put_code(content, langage='', anchor=None, before=None, after=None):
  77. code = "```%s\n%s\n```" % (langage, content)
  78. put_markdown(code, anchor=anchor, before=before, after=after)
  79. def put_markdown(mdcontent, strip_indent=0, lstrip=False, anchor=None, before=None, after=None):
  80. """
  81. 输出Markdown内容。当在函数中使用Python的三引号语法输出多行内容时,为了排版美观可能会对Markdown文本进行缩进,
  82. 这时候,可以设置strip_indent或lstrip来防止Markdown错误解析
  83. :param mdcontent: Markdown文本
  84. :param strip_indent: 去除行开始的缩进空白数。
  85. :param lstrip: 是否去除行开始的空白。
  86. :return:
  87. """
  88. if strip_indent:
  89. lines = (
  90. i[strip_indent:] if (i[:strip_indent] == ' ' * strip_indent) else i
  91. for i in mdcontent.splitlines()
  92. )
  93. mdcontent = '\n'.join(lines)
  94. if lstrip:
  95. lines = (i.lstrip() for i in mdcontent.splitlines())
  96. mdcontent = '\n'.join(lines)
  97. _put_content('markdown', content=mdcontent, anchor=anchor, before=before, after=after)
  98. def put_table(tdata, header=None, anchor=None, before=None, after=None):
  99. """
  100. 输出表格
  101. :param tdata: list of list|dict
  102. :param header: 列表,当tdata为字典列表时,header指定表头顺序
  103. :return:
  104. """
  105. if header:
  106. tdata = [
  107. [row.get(k, '') for k in header]
  108. for row in tdata
  109. ]
  110. def quote(data):
  111. return str(data).replace('|', r'\|')
  112. # 防止当tdata只有一行时,无法显示表格
  113. if len(tdata) == 1:
  114. tdata[0:0] = [' '] * len(tdata[0])
  115. header = "|%s|" % "|".join(map(quote, tdata[0]))
  116. res = [header]
  117. res.append("|%s|" % "|".join(['----'] * len(tdata[0])))
  118. for tr in tdata[1:]:
  119. t = "|%s|" % "|".join(map(quote, tr))
  120. res.append(t)
  121. put_markdown('\n'.join(res), anchor=anchor, before=before, after=after)
  122. def _format_button(buttons):
  123. """
  124. 格式化按钮参数
  125. :param buttons: button列表, button可用形式:
  126. {value:, label:, }
  127. (value, label,)
  128. value 单值,label等于value
  129. :return: [{value:, label:, }, ...]
  130. """
  131. btns = []
  132. for btn in buttons:
  133. if isinstance(btn, Mapping):
  134. assert 'value' in btn and 'label' in btn, 'actions item must have value and label key'
  135. elif isinstance(btn, list):
  136. assert len(btn) == 2, 'actions item format error'
  137. btn = dict(zip(('value', 'label'), btn))
  138. else:
  139. btn = dict(value=btn, label=btn)
  140. btns.append(btn)
  141. return btns
  142. def td_buttons(buttons, onclick, save=None, mutex_mode=False):
  143. """
  144. 在表格中显示一组按钮
  145. 参数含义同 buttons 函数
  146. :return:
  147. """
  148. btns = _format_button(buttons)
  149. callback_id = register_callback(onclick, save, mutex_mode)
  150. tpl = '<button type="button" value="{value}" class="btn btn-primary btn-sm" ' \
  151. 'onclick="WebIO.DisplayAreaButtonOnClick(this, \'%s\')">{label}</button>' % callback_id
  152. btns_html = [tpl.format(**b) for b in btns]
  153. return ' '.join(btns_html)
  154. def buttons(buttons, onclick, small=False, save=None, mutex_mode=False, anchor=None, before=None, after=None):
  155. """
  156. 显示一组按钮
  157. :param buttons: button列表, button可用形式: value 只能为字符串
  158. {value:, label:, }
  159. (value, label,)
  160. value 单值,label等于value
  161. :param onclick: CallBack(btn_value, save) CallBack can be generator function or coroutine function
  162. :param save:
  163. :param mutex_mode: 互斥模式,回调在运行过程中,无法响应同一回调,仅当onclick为协程函数时有效
  164. :return:
  165. """
  166. assert not (before and after), "Parameter 'before' and 'after' cannot be specified at the same time"
  167. btns = _format_button(buttons)
  168. callback_id = register_callback(onclick, save, mutex_mode)
  169. _put_content('buttons', callback_id=callback_id, buttons=btns, small=small, anchor=anchor, before=before,
  170. after=after)
  171. def put_file(name, content, anchor=None, before=None, after=None):
  172. """
  173. :param name: file name
  174. :param content: bytes-like object
  175. :return:
  176. """
  177. assert not (before and after), "Parameter 'before' and 'after' cannot be specified at the same time"
  178. content = b64encode(content).decode('ascii')
  179. _put_content('file', name=name, content=content, anchor=anchor, before=before, after=after)