output.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. r"""输出内容到用户浏览器
  2. 本模块提供了一系列函数来输出不同形式的内容到用户浏览器,并支持灵活的输出控制。
  3. 输出控制
  4. --------------
  5. 锚点
  6. ^^^^^^^^^^^^^^^^^
  7. .. autofunction:: set_anchor
  8. .. autofunction:: clear_before
  9. .. autofunction:: clear_after
  10. .. autofunction:: clear_range
  11. .. autofunction:: scroll_to
  12. 环境设置
  13. ^^^^^^^^^^^^^^^^^
  14. .. autofunction:: set_title
  15. .. autofunction:: set_output_fixed_height
  16. .. autofunction:: set_auto_scroll_bottom
  17. 内容输出
  18. --------------
  19. .. autofunction:: put_text
  20. .. autofunction:: put_markdown
  21. .. autofunction:: put_code
  22. .. autofunction:: put_table
  23. .. autofunction:: td_buttons
  24. .. autofunction:: put_buttons
  25. .. autofunction:: put_file
  26. """
  27. from base64 import b64encode
  28. from collections.abc import Mapping
  29. from .framework import Global
  30. from .io_ctrl import output_register_callback, send_msg
  31. def set_title(title):
  32. r"""设置页面标题"""
  33. send_msg('output_ctl', dict(title=title))
  34. def set_output_fixed_height(enabled=True):
  35. r"""开启/关闭页面固高度模式"""
  36. send_msg('output_ctl', dict(output_fixed_height=enabled))
  37. def set_auto_scroll_bottom(enabled=True):
  38. r"""开启/关闭页面自动滚动到底部"""
  39. send_msg('output_ctl', dict(auto_scroll_bottom=enabled))
  40. _AnchorTPL = 'pywebio-anchor-%s'
  41. def set_anchor(name):
  42. """
  43. 在当前输出处标记锚点。 若已经存在 ``name`` 锚点,则先将旧锚点删除
  44. """
  45. inner_ancher_name = _AnchorTPL % name
  46. send_msg('output_ctl', dict(set_anchor=inner_ancher_name))
  47. def clear_before(anchor):
  48. """清除 ``anchor`` 锚点之前输出的内容"""
  49. inner_ancher_name = _AnchorTPL % anchor
  50. send_msg('output_ctl', dict(clear_before=inner_ancher_name))
  51. def clear_after(anchor):
  52. """清除 ``anchor`` 锚点之后输出的内容"""
  53. inner_ancher_name = _AnchorTPL % anchor
  54. send_msg('output_ctl', dict(clear_after=inner_ancher_name))
  55. def clear_range(start_anchor, end_anchor):
  56. """
  57. 清除 ``start_anchor`` - ``end_ancher`` 锚点之间输出的内容.
  58. 若 ``start_anchor`` 或 ``end_ancher`` 不存在,则不进行任何操作
  59. """
  60. inner_start_anchor_name = 'pywebio-anchor-%s' % start_anchor
  61. inner_end_ancher_name = 'pywebio-anchor-%s' % end_anchor
  62. send_msg('output_ctl', dict(clear_range=[inner_start_anchor_name, inner_end_ancher_name]))
  63. def scroll_to(anchor):
  64. """将页面滚动到 ``anchor`` 锚点处"""
  65. inner_ancher_name = 'pywebio-anchor-%s' % anchor
  66. send_msg('output_ctl', dict(scroll_to=inner_ancher_name))
  67. def _put_content(type, ws=None, anchor=None, before=None, after=None, **other_spec):
  68. """
  69. 向用户端发送 ``output`` 指令
  70. :param type: 输出类型
  71. :param content: 输出内容
  72. :param anchor: 为当前的输出内容标记锚点
  73. :param before: 在给定的锚点之前输出内容
  74. :param after: 在给定的锚点之后输出内容。
  75. 注意: ``before`` 和 ``after`` 参数不可以同时使用
  76. :param other_spec: 额外的输出参数
  77. """
  78. assert not (before and after), "Parameter 'before' and 'after' cannot be specified at the same time"
  79. spec = dict(type=type)
  80. spec.update(other_spec)
  81. if anchor:
  82. spec['anchor'] = _AnchorTPL % anchor
  83. if before:
  84. spec['before'] = _AnchorTPL % before
  85. elif after:
  86. spec['after'] = _AnchorTPL % after
  87. msg = dict(command="output", spec=spec)
  88. (ws or Global.active_ws).send_coro_msg(msg)
  89. def put_text(text, inline=False, anchor=None, before=None, after=None):
  90. """
  91. 输出文本内容
  92. :param str text: 文本内容
  93. :param str anchor: 为当前的输出内容标记锚点
  94. :param str before: 在给定的锚点之前输出内容
  95. :param str after: 在给定的锚点之后输出内容
  96. 注意: ``before`` 和 ``after`` 参数不可以同时使用
  97. """
  98. _put_content('text', content=text, inline=inline, anchor=anchor, before=before, after=after)
  99. def put_html(html, anchor=None, before=None, after=None):
  100. """
  101. 输出文本内容
  102. :param str html: html内容
  103. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  104. """
  105. _put_content('html', content=html, anchor=anchor, before=before, after=after)
  106. def put_code(content, langage='', anchor=None, before=None, after=None):
  107. """
  108. 输出代码块
  109. :param str content: 代码内容
  110. :param str langage: 代码语言
  111. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  112. """
  113. code = "```%s\n%s\n```" % (langage, content)
  114. put_markdown(code, anchor=anchor, before=before, after=after)
  115. def put_markdown(mdcontent, strip_indent=0, lstrip=False, anchor=None, before=None, after=None):
  116. """
  117. 输出Markdown内容。
  118. :param str mdcontent: Markdown文本
  119. :param int strip_indent: 对于每一行,若前 ``strip_indent`` 个字符都为空格,则将其去除
  120. :param bool lstrip: 是否去除每一行开始的空白符
  121. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  122. 当在函数中使用Python的三引号语法输出多行内容时,为了排版美观可能会对Markdown文本进行缩进,
  123. 这时候,可以设置 ``strip_indent`` 或 ``lstrip`` 来防止Markdown错误解析(但不要同时使用 ``strip_indent`` 和 ``lstrip`` )::
  124. # 不使用strip_indent或lstrip
  125. def hello():
  126. put_markdown(r\""" # H1
  127. This is content.
  128. \""")
  129. # 使用lstrip
  130. def hello():
  131. put_markdown(r\""" # H1
  132. This is content.
  133. \""", lstrip=True)
  134. # 使用strip_indent
  135. def hello():
  136. put_markdown(r\""" # H1
  137. This is content.
  138. \""", strip_indent=4)
  139. """
  140. if strip_indent:
  141. lines = (
  142. i[strip_indent:] if (i[:strip_indent] == ' ' * strip_indent) else i
  143. for i in mdcontent.splitlines()
  144. )
  145. mdcontent = '\n'.join(lines)
  146. if lstrip:
  147. lines = (i.lstrip() for i in mdcontent.splitlines())
  148. mdcontent = '\n'.join(lines)
  149. _put_content('markdown', content=mdcontent, anchor=anchor, before=before, after=after)
  150. def put_table(tdata, header=None, anchor=None, before=None, after=None):
  151. """
  152. 输出表格
  153. :param list tdata: 表格数据。列表项可以为 ``list`` 或者 ``dict``
  154. :param list header: 当tdata为字典列表时,使用 ``header`` 指定表头顺序
  155. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  156. """
  157. if header:
  158. tdata = [
  159. [row.get(k, '') for k in header]
  160. for row in tdata
  161. ]
  162. def quote(data):
  163. return str(data).replace('|', r'\|')
  164. # 防止当tdata只有一行时,无法显示表格
  165. if len(tdata) == 1:
  166. tdata[0:0] = [' '] * len(tdata[0])
  167. header = "|%s|" % "|".join(map(quote, tdata[0]))
  168. res = [header]
  169. res.append("|%s|" % "|".join(['----'] * len(tdata[0])))
  170. for tr in tdata[1:]:
  171. t = "|%s|" % "|".join(map(quote, tr))
  172. res.append(t)
  173. put_markdown('\n'.join(res), anchor=anchor, before=before, after=after)
  174. def _format_button(buttons):
  175. """
  176. 格式化按钮参数
  177. :param buttons: button列表, button可用形式:
  178. {value:, label:, }
  179. (value, label,)
  180. value 单值,label等于value
  181. :return: [{value:, label:, }, ...]
  182. """
  183. btns = []
  184. for btn in buttons:
  185. if isinstance(btn, Mapping):
  186. assert 'value' in btn and 'label' in btn, 'actions item must have value and label key'
  187. elif isinstance(btn, list):
  188. assert len(btn) == 2, 'actions item format error'
  189. btn = dict(zip(('value', 'label'), btn))
  190. else:
  191. btn = dict(value=btn, label=btn)
  192. btns.append(btn)
  193. return btns
  194. def td_buttons(buttons, onclick, save=None, mutex_mode=False):
  195. """
  196. 在表格中显示一组按钮
  197. :param str buttons, onclick, save: 与 `put_buttons` 函数的同名参数含义一致
  198. """
  199. btns = _format_button(buttons)
  200. callback_id = output_register_callback(onclick, save, mutex_mode)
  201. tpl = '<button type="button" value="{value}" class="btn btn-primary btn-sm" ' \
  202. 'onclick="WebIO.DisplayAreaButtonOnClick(this, \'%s\')">{label}</button>' % callback_id
  203. btns_html = [tpl.format(**b) for b in btns]
  204. return ' '.join(btns_html)
  205. def put_buttons(buttons, onclick, small=False, save=None, mutex_mode=False, anchor=None, before=None, after=None):
  206. """
  207. 输出一组按钮
  208. :param list buttons: 按钮列表。列表项的可用形式有:
  209. * dict: ``{value:选项值, label:选项标签, [disabled:是否禁止点击]}``
  210. * tuple or list: ``(value, label, [disabled])``
  211. * 单值: 此时label和value使用相同的值
  212. :type onclick: Callable or Coroutine
  213. :param onclick: 按钮点击回调函数. ``onclick`` 可以是普通函数或者协程函数.
  214. 函数签名为 ``onclick(btn_value, save)``.
  215. 当按钮组中的按钮被点击时,``onclick`` 被调用,``onclick`` 接收两个参数,``btn_value``为被点击的按钮的 ``value`` 值,
  216. ``save`` 为 `td_buttons` 的 ``save`` 参数值
  217. :param any save: ``save`` 内容将传入 ``onclick`` 回调函数的第二个参数
  218. :param bool mutex_mode: 互斥模式。若为 ``True`` ,则在运行回调函数过程中,无法响应当前按钮组的新点击事件,仅当 `onclick`` 为协程函数时有效
  219. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  220. """
  221. assert not (before and after), "Parameter 'before' and 'after' cannot be specified at the same time"
  222. btns = _format_button(buttons)
  223. callback_id = output_register_callback(onclick, save, mutex_mode)
  224. _put_content('buttons', callback_id=callback_id, buttons=btns, small=small, anchor=anchor, before=before,
  225. after=after)
  226. def put_file(name, content, anchor=None, before=None, after=None):
  227. """输出文件。
  228. 在浏览器上的显示为一个以文件名为名的链接,点击链接后浏览器自动下载文件。
  229. :param str name: 文件名
  230. :param content: 文件内容. 类型为 bytes-like object
  231. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  232. """
  233. assert not (before and after), "Parameter 'before' and 'after' cannot be specified at the same time"
  234. content = b64encode(content).decode('ascii')
  235. _put_content('file', name=name, content=content, anchor=anchor, before=before, after=after)