1
0

output.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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_html
  23. .. autofunction:: put_code
  24. .. autofunction:: put_table
  25. .. autofunction:: table_cell_buttons
  26. .. autofunction:: put_buttons
  27. .. autofunction:: put_image
  28. .. autofunction:: put_file
  29. """
  30. import io
  31. import logging
  32. from base64 import b64encode
  33. from collections.abc import Mapping
  34. from .io_ctrl import output_register_callback, send_msg, OutputReturn, safely_destruct_output_when_exp
  35. try:
  36. from PIL.Image import Image as PILImage
  37. except ImportError:
  38. PILImage = type('MockPILImage', (), dict(__init__=None))
  39. logger = logging.getLogger(__name__)
  40. __all__ = ['Position', 'set_title', 'set_output_fixed_height', 'set_auto_scroll_bottom', 'set_anchor', 'clear_before',
  41. 'clear_after', 'clear_range', 'remove', 'scroll_to', 'put_text', 'put_html', 'put_code', 'put_markdown',
  42. 'put_table', 'table_cell_buttons', 'put_buttons', 'put_image', 'put_file', 'PopupSize', 'popup',
  43. 'close_popup']
  44. # popup尺寸
  45. class PopupSize:
  46. LARGE = 'large'
  47. NORMAL = 'normal'
  48. SMALL = 'small'
  49. class Position:
  50. TOP = 'top'
  51. MIDDLE = 'middle'
  52. BOTTOM = 'bottom'
  53. def set_title(title):
  54. r"""设置页面标题"""
  55. send_msg('output_ctl', dict(title=title))
  56. def set_output_fixed_height(enabled=True):
  57. r"""开启/关闭页面固高度模式"""
  58. send_msg('output_ctl', dict(output_fixed_height=enabled))
  59. def set_auto_scroll_bottom(enabled=True):
  60. r"""开启/关闭页面自动滚动到底部"""
  61. send_msg('output_ctl', dict(auto_scroll_bottom=enabled))
  62. def _get_anchor_id(name):
  63. """获取实际用于前端html页面中的id属性"""
  64. name = name.replace(' ', '-')
  65. return 'pywebio-anchor-%s' % name
  66. def set_anchor(name):
  67. """
  68. 在当前输出处标记锚点。 若已经存在 ``name`` 锚点,则先将旧锚点删除
  69. """
  70. inner_ancher_name = _get_anchor_id(name)
  71. send_msg('output_ctl', dict(set_anchor=inner_ancher_name))
  72. def clear_before(anchor):
  73. """清除 ``anchor`` 锚点之前输出的内容。
  74. ⚠️注意: 位于 ``anchor`` 锚点之前设置的锚点也会被清除
  75. """
  76. inner_ancher_name = _get_anchor_id(anchor)
  77. send_msg('output_ctl', dict(clear_before=inner_ancher_name))
  78. def clear_after(anchor):
  79. """清除 ``anchor`` 锚点之后输出的内容。
  80. ⚠️注意: 位于 ``anchor`` 锚点之后设置的锚点也会被清除
  81. """
  82. inner_ancher_name = _get_anchor_id(anchor)
  83. send_msg('output_ctl', dict(clear_after=inner_ancher_name))
  84. def clear_range(start_anchor, end_anchor):
  85. """
  86. 清除 ``start_anchor`` - ``end_ancher`` 锚点之间输出的内容.
  87. 若 ``start_anchor`` 或 ``end_ancher`` 不存在,则不进行任何操作。
  88. ⚠️注意: 在 ``start_anchor`` - ``end_ancher`` 之间设置的锚点也会被清除
  89. """
  90. inner_start_anchor_name = 'pywebio-anchor-%s' % start_anchor
  91. inner_end_ancher_name = 'pywebio-anchor-%s' % end_anchor
  92. send_msg('output_ctl', dict(clear_range=[inner_start_anchor_name, inner_end_ancher_name]))
  93. def remove(anchor):
  94. """将 ``anchor`` 锚点连同锚点处的内容移除"""
  95. inner_ancher_name = _get_anchor_id(anchor)
  96. send_msg('output_ctl', dict(remove=inner_ancher_name))
  97. def scroll_to(anchor, position=Position.TOP):
  98. """scroll_to(anchor, position=Position.TOP)
  99. 将页面滚动到 ``anchor`` 锚点处
  100. :param str anchor: 锚点名
  101. :param str position: 将锚点置于屏幕可视区域的位置。可用值:
  102. * ``Position.TOP`` : 滚动页面,让锚点位于屏幕可视区域顶部
  103. * ``Position.MIDDLE`` : 滚动页面,让锚点位于屏幕可视区域中间
  104. * ``Position.BOTTOM`` : 滚动页面,让锚点位于屏幕可视区域底部
  105. """
  106. inner_ancher_name = 'pywebio-anchor-%s' % anchor
  107. send_msg('output_ctl', dict(scroll_to=inner_ancher_name, position=position))
  108. def _get_output_spec(type, anchor=None, before=None, after=None, **other_spec):
  109. """
  110. 获取 ``output`` 指令的spec字段
  111. :param str type: 输出类型
  112. :param str anchor: 为当前的输出内容标记锚点,若锚点已经存在,则将锚点处的内容替换为当前内容。
  113. :param str before: 在给定的锚点之前输出内容。若给定的锚点不存在,则不输出任何内容
  114. :param str after: 在给定的锚点之后输出内容。若给定的锚点不存在,则不输出任何内容。
  115. 注意: ``before`` 和 ``after`` 参数不可以同时使用
  116. :param other_spec: 额外的输出参数,值为None的参数不会包含到返回值中
  117. :return dict: ``output`` 指令的spec字段
  118. """
  119. assert not (before and after), "Parameter 'before' and 'after' cannot be specified at the same time"
  120. spec = dict(type=type)
  121. spec.update({k: v for k, v in other_spec.items() if v is not None})
  122. if anchor:
  123. spec['anchor'] = _get_anchor_id(anchor)
  124. if before:
  125. spec['before'] = _get_anchor_id(before)
  126. elif after:
  127. spec['after'] = _get_anchor_id(after)
  128. return spec
  129. def put_text(text, inline=False, anchor=None, before=None, after=None) -> OutputReturn:
  130. """
  131. 输出文本内容
  132. :param str text: 文本内容
  133. :param bool inline: 文本行末不换行。默认换行
  134. :param str anchor: 为当前的输出内容标记锚点,若锚点已经存在,则将锚点处的内容替换为当前内容。
  135. :param str before: 在给定的锚点之前输出内容。若给定的锚点不存在,则不输出任何内容
  136. :param str after: 在给定的锚点之后输出内容。若给定的锚点不存在,则不输出任何内容。
  137. 注意: ``before`` 和 ``after`` 参数不可以同时使用。
  138. 当 ``anchor`` 指定的锚点已经在页面上存在时,``before`` 和 ``after`` 参数将被忽略。
  139. """
  140. spec = _get_output_spec('text', content=str(text), inline=inline, anchor=anchor, before=before, after=after)
  141. return OutputReturn(spec)
  142. def put_html(html, anchor=None, before=None, after=None) -> OutputReturn:
  143. """
  144. 输出Html内容。
  145. 与支持通过Html输出内容到 `Jupyter Notebook <https://nbviewer.jupyter.org/github/ipython/ipython/blob/master/examples/IPython%20Kernel/Rich%20Output.ipynb#HTML>`_ 的库兼容。
  146. :param html: html字符串或 实现了 `IPython.display.HTML` 接口的类的实例
  147. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  148. """
  149. if hasattr(html, '__html__'):
  150. html = html.__html__()
  151. spec = _get_output_spec('html', content=html, anchor=anchor, before=before, after=after)
  152. return OutputReturn(spec)
  153. def put_code(content, langage='', anchor=None, before=None, after=None) -> OutputReturn:
  154. """
  155. 输出代码块
  156. :param str content: 代码内容
  157. :param str langage: 代码语言
  158. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  159. """
  160. code = "```%s\n%s\n```" % (langage, content)
  161. return put_markdown(code, anchor=anchor, before=before, after=after)
  162. def put_markdown(mdcontent, strip_indent=0, lstrip=False, anchor=None, before=None, after=None) -> OutputReturn:
  163. """
  164. 输出Markdown内容。
  165. :param str mdcontent: Markdown文本
  166. :param int strip_indent: 对于每一行,若前 ``strip_indent`` 个字符都为空格,则将其去除
  167. :param bool lstrip: 是否去除每一行开始的空白符
  168. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  169. 当在函数中使用Python的三引号语法输出多行内容时,为了排版美观可能会对Markdown文本进行缩进,
  170. 这时候,可以设置 ``strip_indent`` 或 ``lstrip`` 来防止Markdown错误解析(但不要同时使用 ``strip_indent`` 和 ``lstrip`` )::
  171. # 不使用strip_indent或lstrip
  172. def hello():
  173. put_markdown(r\""" # H1
  174. This is content.
  175. \""")
  176. # 使用lstrip
  177. def hello():
  178. put_markdown(r\""" # H1
  179. This is content.
  180. \""", lstrip=True)
  181. # 使用strip_indent
  182. def hello():
  183. put_markdown(r\""" # H1
  184. This is content.
  185. \""", strip_indent=4)
  186. """
  187. if strip_indent:
  188. lines = (
  189. i[strip_indent:] if (i[:strip_indent] == ' ' * strip_indent) else i
  190. for i in mdcontent.splitlines()
  191. )
  192. mdcontent = '\n'.join(lines)
  193. if lstrip:
  194. lines = (i.lstrip() for i in mdcontent.splitlines())
  195. mdcontent = '\n'.join(lines)
  196. spec = _get_output_spec('markdown', content=mdcontent, anchor=anchor, before=before, after=after)
  197. return OutputReturn(spec)
  198. @safely_destruct_output_when_exp('tdata')
  199. def put_table(tdata, header=None, span=None, anchor=None, before=None, after=None) -> OutputReturn:
  200. """
  201. 输出表格
  202. :param list tdata: 表格数据。列表项可以为 ``list`` 或者 ``dict`` , 单元格的内容可以为字符串或 ``put_xxx`` 类型的输出函数,字符串内容的单元格显示时会被当作html。
  203. :param list header: 设定表头。
  204. 当 ``tdata`` 的列表项为 ``list`` 类型时,若省略 ``header`` 参数,则使用 ``tdata`` 的第一项作为表头。
  205. 当 ``tdata`` 为字典列表时,使用 ``header`` 指定表头顺序,不可省略。
  206. 此时, ``header`` 格式可以为 <字典键>列表 或者 ``(<显示文本>, <字典键>)`` 列表。
  207. :param dict span: 表格的跨行/跨列信息,格式为 ``{ (行id,列id):{"col": 跨列数, "row": 跨行数} }``
  208. 其中 ``行id`` 和 ``列id`` 为将表格转为二维数组后的需要跨行/列的单元格,二维数据包含表头,``id`` 从 0 开始记数。
  209. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  210. 使用示例::
  211. # 'Name'单元格跨2行、'Address'单元格跨2列
  212. put_table([
  213. ['Name', 'Address'],
  214. ['City', 'Country'],
  215. ['Wang', 'Beijing', 'China'],
  216. ['Liu', 'New York', 'America'],
  217. ], span={(0,0):{"row":2}, (0,1):{"col":2}})
  218. # 单元格为 ``put_xxx`` 类型的输出函数
  219. put_table([
  220. ['Type', 'Content'],
  221. ['html', 'X<sup>2</sup>'],
  222. ['text', put_text('<hr/>')],
  223. ['buttons', put_buttons(['A', 'B'], onclick=...)],
  224. ['markdown', put_markdown('`Awesome PyWebIO!`')],
  225. ['file', put_file('hello.text', b'')],
  226. ['table', put_table([['A', 'B'], ['C', 'D']])]
  227. ])
  228. # 设置表头
  229. put_table([
  230. ['Wang', 'M', 'China'],
  231. ['Liu', 'W', 'America'],
  232. ], header=['Name', 'Gender', 'Address'])
  233. # dict类型的表格行
  234. put_table([
  235. {"Course":"OS", "Score": "80"},
  236. {"Course":"DB", "Score": "93"},
  237. ], header=["Course", "Score"]) # or header=[("课程", "Course"), ("得分" ,"Score")]
  238. .. versionadded:: 0.3
  239. 单元格的内容支持 ``put_xxx`` 类型的输出函数
  240. """
  241. # Change ``dict`` row table to list row table
  242. if tdata and isinstance(tdata[0], dict):
  243. if isinstance(header[0], (list, tuple)):
  244. header_ = [h[0] for h in header]
  245. order = [h[-1] for h in header]
  246. else:
  247. header_ = order = header
  248. tdata = [
  249. [row.get(k, '') for k in order]
  250. for row in tdata
  251. ]
  252. header = header_
  253. if header:
  254. tdata = [header, *tdata]
  255. span = span or {}
  256. span = {('%s,%s' % row_col): val for row_col, val in span.items()}
  257. spec = _get_output_spec('table', data=tdata, span=span, anchor=anchor, before=before, after=after)
  258. return OutputReturn(spec)
  259. def _format_button(buttons):
  260. """
  261. 格式化按钮参数
  262. :param buttons: button列表, button可用形式:
  263. {label:, value:, }
  264. (label, value, )
  265. value 单值,label等于value
  266. :return: [{value:, label:, }, ...]
  267. """
  268. btns = []
  269. for btn in buttons:
  270. if isinstance(btn, Mapping):
  271. assert 'value' in btn and 'label' in btn, 'actions item must have value and label key'
  272. elif isinstance(btn, (list, tuple)):
  273. assert len(btn) == 2, 'actions item format error'
  274. btn = dict(zip(('label', 'value'), btn))
  275. else:
  276. btn = dict(value=btn, label=btn)
  277. btns.append(btn)
  278. return btns
  279. def table_cell_buttons(buttons, onclick, **callback_options) -> str:
  280. """
  281. 在表格中显示一组按钮
  282. :param str buttons, onclick, save: 与 `put_buttons` 函数的同名参数含义一致
  283. .. _table_cell_buttons-code-sample:
  284. 使用示例::
  285. from functools import partial
  286. def edit_row(choice, row):
  287. put_text("You click %s button at row %s" % (choice, row))
  288. put_table([
  289. ['Idx', 'Actions'],
  290. ['1', table_cell_buttons(['edit', 'delete'], onclick=partial(edit_row, row=1))],
  291. ['2', table_cell_buttons(['edit', 'delete'], onclick=partial(edit_row, row=2))],
  292. ['3', table_cell_buttons(['edit', 'delete'], onclick=partial(edit_row, row=3))],
  293. ])
  294. .. deprecated:: 0.3
  295. Use :func:`put_buttons()` instead
  296. """
  297. logger.warning("pywebio.output.table_cell_buttons() is deprecated in version 0.3 and will be removed in 1.0, "
  298. "use pywebio.output.put_buttons() instead.")
  299. btns = _format_button(buttons)
  300. callback_id = output_register_callback(onclick, **callback_options)
  301. tpl = '<button type="button" value="{value}" class="btn btn-primary btn-sm" ' \
  302. 'onclick="WebIO.DisplayAreaButtonOnClick(this, \'%s\')">{label}</button>' % callback_id
  303. btns_html = [tpl.format(**b) for b in btns]
  304. return ' '.join(btns_html)
  305. def put_buttons(buttons, onclick, small=None, anchor=None, before=None, after=None,
  306. **callback_options) -> OutputReturn:
  307. """
  308. 输出一组按钮
  309. :param list buttons: 按钮列表。列表项的可用形式有:
  310. * dict: ``{label:选项标签, value:选项值}``
  311. * tuple or list: ``(label, value)``
  312. * 单值: 此时label和value使用相同的值
  313. :type onclick: Callable or Coroutine
  314. :param onclick: 按钮点击回调函数. ``onclick`` 可以是普通函数或者协程函数.
  315. 函数签名为 ``onclick(btn_value)``.
  316. 当按钮组中的按钮被点击时,``onclick`` 被调用,并传入被点击的按钮的 ``value`` 值。
  317. 可以使用 ``functools.partial`` 来在 ``onclick`` 中保存更多上下文信息 。
  318. :param bool small: 是否显示小号按钮,默认为False
  319. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  320. :param callback_options: 回调函数的其他参数。根据选用的 session 实现有不同参数
  321. CoroutineBasedSession 实现
  322. * mutex_mode: 互斥模式。默认为 ``False`` 。若为 ``True`` ,则在运行回调函数过程中,无法响应当前按钮组的新点击事件,仅当 ``onclick`` 为协程函数时有效
  323. ThreadBasedSession 实现
  324. * serial_mode: 串行模式模式。默认为 ``False`` 。若为 ``True`` ,则运行当前点击事件时,其他所有新的点击事件都将被排队等待当前点击事件时运行完成。
  325. 不开启 ``serial_mode`` 时,ThreadBasedSession 在新线程中执行回调函数。所以如果回调函数运行时间很短,
  326. 可以关闭 ``serial_mode`` 来提高性能。
  327. 使用示例::
  328. from functools import partial
  329. def edit_row(choice, id):
  330. put_text("You click %s button with id: %s" % (choice, id))
  331. put_buttons(['edit', 'delete'], onclick=partial(edit_row, id=1))
  332. """
  333. btns = _format_button(buttons)
  334. callback_id = output_register_callback(onclick, **callback_options)
  335. spec = _get_output_spec('buttons', callback_id=callback_id, buttons=btns, small=small, anchor=anchor, before=before,
  336. after=after)
  337. return OutputReturn(spec)
  338. def put_image(content, format=None, title='', width=None, height=None, anchor=None, before=None,
  339. after=None) -> OutputReturn:
  340. """输出图片。
  341. :param content: 文件内容. 类型为 bytes-like object 或者为 ``PIL.Image.Image`` 实例
  342. :param str title: 图片描述
  343. :param str width: 图像的宽度,单位是CSS像素(数字px)或者百分比(数字%)。
  344. :param str height: 图像的高度,单位是CSS像素(数字px)或者百分比(数字%)。可以只指定 width 和 height 中的一个值,浏览器会根据原始图像进行缩放。
  345. :param str format: 图片格式。如 ``png`` , ``jpeg`` , ``gif`` 等
  346. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  347. """
  348. if isinstance(content, PILImage):
  349. format = content.format
  350. imgByteArr = io.BytesIO()
  351. content.save(imgByteArr, format=format)
  352. content = imgByteArr.getvalue()
  353. format = '' if format is None else ('image/%s' % format)
  354. width = 'width="%s"' % width if width is not None else ''
  355. height = 'height="%s"' % height if height is not None else ''
  356. b64content = b64encode(content).decode('ascii')
  357. html = r'<img src="data:{format};base64, {b64content}" ' \
  358. r'alt="{title}" {width} {height}/>'.format(format=format, b64content=b64content,
  359. title=title, height=height, width=width)
  360. return put_html(html, anchor=anchor, before=before, after=after)
  361. def put_file(name, content, anchor=None, before=None, after=None) -> OutputReturn:
  362. """输出文件。
  363. 在浏览器上的显示为一个以文件名为名的链接,点击链接后浏览器自动下载文件。
  364. :param str name: 文件名
  365. :param content: 文件内容. 类型为 bytes-like object
  366. :param str anchor, before, after: 与 `put_text` 函数的同名参数含义一致
  367. """
  368. content = b64encode(content).decode('ascii')
  369. spec = _get_output_spec('file', name=name, content=content, anchor=anchor, before=before, after=after)
  370. return OutputReturn(spec)
  371. @safely_destruct_output_when_exp('content')
  372. def popup(title, content, size=PopupSize.NORMAL, implicit_close=True, closable=True):
  373. """popup(title, content, size=PopupSize.NORMAL, implicit_close=True, closable=True)
  374. 显示弹窗
  375. :param str title: 弹窗标题
  376. :type content: list/str/put_xxx()
  377. :param content: 弹窗内容. 可以为字符串或 ``put_xxx`` 类输出函数的返回值,或者为它们组成的列表。字符串内容会被看作html
  378. :param str size: 弹窗窗口大小,可选值:
  379. * ``LARGE`` : 大尺寸
  380. * ``NORMAL`` : 普通尺寸
  381. * ``SMALL`` : 小尺寸
  382. :param bool implicit_close: 是否可以通过点击弹窗外的内容或按下 ``Esc`` 键来关闭弹窗
  383. :param bool closable: 是否可由用户关闭弹窗. 默认情况下,用户可以通过点击弹窗右上角的关闭按钮来关闭弹窗,
  384. 设置为 ``False`` 时弹窗仅能通过 :func:`popup_close()` 关闭, ``implicit_close`` 参数被忽略.
  385. Example::
  386. popup('popup title', 'popup html content', size=PopupSize.SMALL)
  387. popup('Popup title', [
  388. '<h3>Popup Content</h3>',
  389. put_text('html: <br/>'),
  390. put_table([['A', 'B'], ['C', 'D']]),
  391. put_buttons(['close_popup()'], onclick=lambda _: close_popup())
  392. ])
  393. """
  394. if not isinstance(content, (list, tuple)):
  395. content = [content]
  396. for item in content:
  397. assert isinstance(item, (str, OutputReturn)), "popup() content must be list of str/put_xxx()"
  398. send_msg(cmd='popup', spec=dict(content=OutputReturn.jsonify(content), title=title, size=size,
  399. implicit_close=implicit_close, closable=closable))
  400. def close_popup():
  401. """关闭弹窗"""
  402. send_msg(cmd='close_popup')