output.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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:: remove
  12. .. autofunction:: scroll_to
  13. 环境设置
  14. ^^^^^^^^^^^^^^^^^
  15. .. autofunction:: set_title
  16. .. autofunction:: set_output_fixed_height
  17. .. autofunction:: set_auto_scroll_bottom
  18. 内容输出
  19. --------------
  20. .. autofunction:: put_text
  21. .. autofunction:: put_markdown
  22. .. autofunction:: put_code
  23. .. autofunction:: put_table
  24. .. autofunction:: table_cell_buttons
  25. .. autofunction:: put_buttons
  26. .. autofunction:: put_image
  27. .. autofunction:: put_file
  28. """
  29. import io
  30. from base64 import b64encode
  31. from collections.abc import Mapping
  32. from .io_ctrl import output_register_callback, send_msg
  33. try:
  34. from PIL.Image import Image as PILImage
  35. except ImportError:
  36. PILImage = type('MockPILImage', (), dict(__init__=None))
  37. TOP = 'top'
  38. MIDDLE = 'middle'
  39. BOTTOM = 'bottom'
  40. def set_title(title):
  41. r"""设置页面标题"""
  42. send_msg('output_ctl', dict(title=title))
  43. def set_output_fixed_height(enabled=True):
  44. r"""开启/关闭页面固高度模式"""
  45. send_msg('output_ctl', dict(output_fixed_height=enabled))
  46. def set_auto_scroll_bottom(enabled=True):
  47. r"""开启/关闭页面自动滚动到底部"""
  48. send_msg('output_ctl', dict(auto_scroll_bottom=enabled))
  49. _AnchorTPL = 'pywebio-anchor-%s'
  50. def set_anchor(name):
  51. """
  52. 在当前输出处标记锚点。 若已经存在 ``name`` 锚点,则先将旧锚点删除
  53. """
  54. inner_ancher_name = _AnchorTPL % name
  55. send_msg('output_ctl', dict(set_anchor=inner_ancher_name))
  56. def clear_before(anchor):
  57. """清除 ``anchor`` 锚点之前输出的内容"""
  58. inner_ancher_name = _AnchorTPL % anchor
  59. send_msg('output_ctl', dict(clear_before=inner_ancher_name))
  60. def clear_after(anchor):
  61. """清除 ``anchor`` 锚点之后输出的内容"""
  62. inner_ancher_name = _AnchorTPL % anchor
  63. send_msg('output_ctl', dict(clear_after=inner_ancher_name))
  64. def clear_range(start_anchor, end_anchor):
  65. """
  66. 清除 ``start_anchor`` - ``end_ancher`` 锚点之间输出的内容.
  67. 若 ``start_anchor`` 或 ``end_ancher`` 不存在,则不进行任何操作
  68. """
  69. inner_start_anchor_name = 'pywebio-anchor-%s' % start_anchor
  70. inner_end_ancher_name = 'pywebio-anchor-%s' % end_anchor
  71. send_msg('output_ctl', dict(clear_range=[inner_start_anchor_name, inner_end_ancher_name]))
  72. def remove(anchor):
  73. """将 ``anchor`` 锚点连同锚点处的内容移除"""
  74. inner_ancher_name = _AnchorTPL % anchor
  75. send_msg('output_ctl', dict(remove=inner_ancher_name))
  76. def scroll_to(anchor, position=TOP):
  77. """将页面滚动到 ``anchor`` 锚点处
  78. :param str anchor: 锚点名
  79. :param str position: 将锚点置于屏幕可视区域的位置。可用值:
  80. * ``TOP`` : 滚动页面,让锚点位于屏幕可视区域顶部
  81. * ``MIDDLE`` : 滚动页面,让锚点位于屏幕可视区域中间
  82. * ``BOTTOM`` : 滚动页面,让锚点位于屏幕可视区域底部
  83. """
  84. inner_ancher_name = 'pywebio-anchor-%s' % anchor
  85. send_msg('output_ctl', dict(scroll_to=inner_ancher_name, position=position))
  86. def _put_content(type, anchor=None, before=None, after=None, **other_spec):
  87. """
  88. 向用户端发送 ``output`` 指令
  89. :param str type: 输出类型
  90. :param content: 输出内容
  91. :param str anchor: 为当前的输出内容标记锚点,若锚点已经存在,则将锚点处的内容替换为当前内容。
  92. :param str before: 在给定的锚点之前输出内容。若给定的锚点不存在,则不输出任何内容
  93. :param str after: 在给定的锚点之后输出内容。若给定的锚点不存在,则不输出任何内容。
  94. 注意: ``before`` 和 ``after`` 参数不可以同时使用
  95. :param other_spec: 额外的输出参数
  96. """
  97. assert not (before and after), "Parameter 'before' and 'after' cannot be specified at the same time"
  98. spec = dict(type=type)
  99. spec.update(other_spec)
  100. if anchor:
  101. spec['anchor'] = _AnchorTPL % anchor
  102. if before:
  103. spec['before'] = _AnchorTPL % before
  104. elif after:
  105. spec['after'] = _AnchorTPL % after
  106. send_msg("output", spec)
  107. def put_text(text, inline=False, anchor=None, before=None, after=None):
  108. """
  109. 输出文本内容
  110. :param str text: 文本内容
  111. :param bool inline: 文本行末不换行。默认换行
  112. :param str anchor: 为当前的输出内容标记锚点,若锚点已经存在,则将锚点处的内容替换为当前内容。
  113. :param str before: 在给定的锚点之前输出内容。若给定的锚点不存在,则不输出任何内容
  114. :param str after: 在给定的锚点之后输出内容。若给定的锚点不存在,则不输出任何内容。
  115. 注意: ``before`` 和 ``after`` 参数不可以同时使用。
  116. 当 ``anchor`` 指定的锚点已经在页面上存在时,``before`` 和 ``after`` 参数将被忽略。
  117. """
  118. _put_content('text', content=str(text), inline=inline, anchor=anchor, before=before, after=after)
  119. def put_html(html, anchor=None, before=None, after=None):
  120. """
  121. 输出文本内容
  122. :param str html: html内容
  123. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  124. """
  125. _put_content('html', content=html, anchor=anchor, before=before, after=after)
  126. def put_code(content, langage='', anchor=None, before=None, after=None):
  127. """
  128. 输出代码块
  129. :param str content: 代码内容
  130. :param str langage: 代码语言
  131. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  132. """
  133. code = "```%s\n%s\n```" % (langage, content)
  134. put_markdown(code, anchor=anchor, before=before, after=after)
  135. def put_markdown(mdcontent, strip_indent=0, lstrip=False, anchor=None, before=None, after=None):
  136. """
  137. 输出Markdown内容。
  138. :param str mdcontent: Markdown文本
  139. :param int strip_indent: 对于每一行,若前 ``strip_indent`` 个字符都为空格,则将其去除
  140. :param bool lstrip: 是否去除每一行开始的空白符
  141. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  142. 当在函数中使用Python的三引号语法输出多行内容时,为了排版美观可能会对Markdown文本进行缩进,
  143. 这时候,可以设置 ``strip_indent`` 或 ``lstrip`` 来防止Markdown错误解析(但不要同时使用 ``strip_indent`` 和 ``lstrip`` )::
  144. # 不使用strip_indent或lstrip
  145. def hello():
  146. put_markdown(r\""" # H1
  147. This is content.
  148. \""")
  149. # 使用lstrip
  150. def hello():
  151. put_markdown(r\""" # H1
  152. This is content.
  153. \""", lstrip=True)
  154. # 使用strip_indent
  155. def hello():
  156. put_markdown(r\""" # H1
  157. This is content.
  158. \""", strip_indent=4)
  159. """
  160. if strip_indent:
  161. lines = (
  162. i[strip_indent:] if (i[:strip_indent] == ' ' * strip_indent) else i
  163. for i in mdcontent.splitlines()
  164. )
  165. mdcontent = '\n'.join(lines)
  166. if lstrip:
  167. lines = (i.lstrip() for i in mdcontent.splitlines())
  168. mdcontent = '\n'.join(lines)
  169. _put_content('markdown', content=mdcontent, anchor=anchor, before=before, after=after)
  170. def put_table(tdata, header=None, anchor=None, before=None, after=None):
  171. """
  172. 输出表格
  173. :param list tdata: 表格数据。列表项可以为 ``list`` 或者 ``dict``
  174. :param list header: 设定表头。
  175. 当 ``tdata`` 的列表项为 ``list`` 类型时,若省略 ``header`` 参数,则使用 ``tdata`` 的第一项作为表头。
  176. 当 ``tdata`` 为字典列表时,使用 ``header`` 指定表头顺序,不可省略。
  177. 此时, ``header`` 格式可以为 <字典键>列表 或者 ``(<显示文本>, <字典键>)`` 列表。
  178. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  179. 使用示例::
  180. put_table([
  181. ['Name', 'Gender', 'Address'],
  182. ['Wang', 'M', 'China'],
  183. ['Liu', 'W', 'America'],
  184. ])
  185. put_table([
  186. ['Wang', 'M', 'China'],
  187. ['Liu', 'W', 'America'],
  188. ], header=['Name', 'Gender', 'Address'])
  189. put_table([
  190. {"Course":"OS", "Score": "80"},
  191. {"Course":"DB", "Score": "93"},
  192. ], header=["Course", "Score"]) # or header=[("课程", "Course"), ("得分" ,"Score")]
  193. """
  194. # Change ``dict`` row table to list row table
  195. if tdata and isinstance(tdata[0], dict):
  196. if isinstance(header[0], (list, tuple)):
  197. header_ = [h[0] for h in header]
  198. order = [h[-1] for h in header]
  199. else:
  200. header_ = order = header
  201. tdata = [
  202. [row.get(k, '') for k in order]
  203. for row in tdata
  204. ]
  205. header = header_
  206. if not header:
  207. header = tdata[0]
  208. tdata = tdata[1:]
  209. # 防止当tdata只有一行时,无法显示表格
  210. if len(tdata) == 0:
  211. raise ValueError("No data in table")
  212. def quote(data):
  213. return str(data).replace('|', r'\|')
  214. header_row = "|%s|" % "|".join(map(quote, header))
  215. rows = [header_row]
  216. rows.append("|%s|" % "|".join(['----'] * len(header)))
  217. for tr in tdata:
  218. t = "|%s|" % "|".join(map(quote, tr))
  219. rows.append(t)
  220. put_markdown('\n'.join(rows), anchor=anchor, before=before, after=after)
  221. def _format_button(buttons):
  222. """
  223. 格式化按钮参数
  224. :param buttons: button列表, button可用形式:
  225. {label:, value:, }
  226. (label, value, )
  227. value 单值,label等于value
  228. :return: [{value:, label:, }, ...]
  229. """
  230. btns = []
  231. for btn in buttons:
  232. if isinstance(btn, Mapping):
  233. assert 'value' in btn and 'label' in btn, 'actions item must have value and label key'
  234. elif isinstance(btn, (list, tuple)):
  235. assert len(btn) == 2, 'actions item format error'
  236. btn = dict(zip(('label', 'value'), btn))
  237. else:
  238. btn = dict(value=btn, label=btn)
  239. btns.append(btn)
  240. return btns
  241. def table_cell_buttons(buttons, onclick, **callback_options):
  242. """
  243. 在表格中显示一组按钮
  244. :param str buttons, onclick, save: 与 `put_buttons` 函数的同名参数含义一致
  245. .. _table_cell_buttons-code-sample:
  246. 使用示例::
  247. from functools import partial
  248. def edit_row(choice, row):
  249. put_text("You click %s button ar row %s" % (choice, row))
  250. put_table([
  251. ['Idx', 'Actions'],
  252. ['1', table_cell_buttons(['edit', 'delete'], onclick=partial(edit_row, row=1))],
  253. ['2', table_cell_buttons(['edit', 'delete'], onclick=partial(edit_row, row=2))],
  254. ['3', table_cell_buttons(['edit', 'delete'], onclick=partial(edit_row, row=3))],
  255. ])
  256. """
  257. btns = _format_button(buttons)
  258. callback_id = output_register_callback(onclick, **callback_options)
  259. tpl = '<button type="button" value="{value}" class="btn btn-primary btn-sm" ' \
  260. 'onclick="WebIO.DisplayAreaButtonOnClick(this, \'%s\')">{label}</button>' % callback_id
  261. btns_html = [tpl.format(**b) for b in btns]
  262. return ' '.join(btns_html)
  263. def put_buttons(buttons, onclick, small=False, anchor=None, before=None, after=None, **callback_options):
  264. """
  265. 输出一组按钮
  266. :param list buttons: 按钮列表。列表项的可用形式有:
  267. * dict: ``{label:选项标签, value:选项值}``
  268. * tuple or list: ``(label, value)``
  269. * 单值: 此时label和value使用相同的值
  270. :type onclick: Callable or Coroutine
  271. :param onclick: 按钮点击回调函数. ``onclick`` 可以是普通函数或者协程函数.
  272. 函数签名为 ``onclick(btn_value)``.
  273. 当按钮组中的按钮被点击时,``onclick`` 被调用,并传入被点击的按钮的 ``value`` 值。
  274. 可以使用 ``functools.partial`` 来在 ``onclick`` 中保存更多上下文信息,见 `table_cell_buttons` :ref:`代码示例 <table_cell_buttons-code-sample>` 。
  275. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  276. :param callback_options: 回调函数的其他参数。根据选用的 session 实现有不同参数
  277. CoroutineBasedSession 实现
  278. * mutex_mode: 互斥模式。默认为 ``False`` 。若为 ``True`` ,则在运行回调函数过程中,无法响应当前按钮组的新点击事件,仅当 ``onclick`` 为协程函数时有效
  279. ThreadBasedSession 实现
  280. * serial_mode: 串行模式模式。默认为 ``False`` 。若为 ``True`` ,则运行当前点击事件时,其他所有新的点击事件都将被排队等待当前点击事件时运行完成。
  281. 不开启 ``serial_mode`` 时,ThreadBasedSession 在新线程中执行回调函数。所以如果回调函数运行时间很短,
  282. 可以关闭 ``serial_mode`` 来提高性能。
  283. """
  284. btns = _format_button(buttons)
  285. callback_id = output_register_callback(onclick, **callback_options)
  286. _put_content('buttons', callback_id=callback_id, buttons=btns, small=small, anchor=anchor, before=before,
  287. after=after)
  288. def put_image(content, format=None, title='', anchor=None, before=None, after=None):
  289. """输出图片。
  290. :param content: 文件内容. 类型为 bytes-like object 或者为 ``PIL.Image.Image`` 实例
  291. :param str title: 图片描述
  292. :param str format: 图片格式。如 ``png`` , ``jpeg`` , ``gif`` 等
  293. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  294. """
  295. if isinstance(content, PILImage):
  296. format = content.format
  297. imgByteArr = io.BytesIO()
  298. content.save(imgByteArr, format=format)
  299. content = imgByteArr.getvalue()
  300. format = '' if format is None else ('image/%s' % format)
  301. b64content = b64encode(content).decode('ascii')
  302. html = r'<img src="data:{format};base64, {b64content}" alt="{title}" />'.format(format=format,
  303. b64content=b64content,
  304. title=title)
  305. put_html(html, anchor=anchor, before=before, after=after)
  306. def put_file(name, content, anchor=None, before=None, after=None):
  307. """输出文件。
  308. 在浏览器上的显示为一个以文件名为名的链接,点击链接后浏览器自动下载文件。
  309. :param str name: 文件名
  310. :param content: 文件内容. 类型为 bytes-like object
  311. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  312. """
  313. content = b64encode(content).decode('ascii')
  314. _put_content('file', name=name, content=content, anchor=anchor, before=before, after=after)