output.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. r"""输出内容到用户浏览器
  2. 本模块提供了一系列函数来输出不同形式的内容到用户浏览器,并支持灵活的输出控制。
  3. 输出控制
  4. --------------
  5. 输出域Scope
  6. ^^^^^^^^^^^^^^^^^
  7. .. autofunction:: set_scope
  8. .. autofunction:: clear
  9. .. autofunction:: remove
  10. .. autofunction:: scroll_to
  11. .. autofunction:: use_scope
  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_html
  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. .. autofunction:: put_collapse
  29. .. autofunction:: put_link
  30. .. autofunction:: put_widget
  31. """
  32. import io
  33. import logging
  34. from base64 import b64encode
  35. from collections.abc import Mapping, Sequence
  36. from functools import wraps
  37. from .io_ctrl import output_register_callback, send_msg, OutputReturn, safely_destruct_output_when_exp
  38. from .session import get_current_session
  39. from .utils import random_str, iscoroutinefunction
  40. try:
  41. from PIL.Image import Image as PILImage
  42. except ImportError:
  43. PILImage = type('MockPILImage', (), dict(__init__=None))
  44. logger = logging.getLogger(__name__)
  45. __all__ = ['Position', 'set_title', 'set_output_fixed_height', 'set_auto_scroll_bottom', 'remove', 'scroll_to',
  46. 'put_text', 'put_html', 'put_code', 'put_markdown', 'use_scope', 'set_scope', 'clear', 'remove',
  47. 'put_table', 'table_cell_buttons', 'put_buttons', 'put_image', 'put_file', 'PopupSize', 'popup',
  48. 'close_popup', 'put_widget', 'put_collapse', 'put_link']
  49. # popup尺寸
  50. class PopupSize:
  51. LARGE = 'large'
  52. NORMAL = 'normal'
  53. SMALL = 'small'
  54. class Position:
  55. TOP = 'top'
  56. MIDDLE = 'middle'
  57. BOTTOM = 'bottom'
  58. # put_xxx()中的position值
  59. class OutputPosition:
  60. TOP = 0
  61. BOTTOM = -1
  62. class Scope:
  63. Current = -1
  64. Root = 0
  65. Parent = -2
  66. def set_title(title):
  67. r"""设置页面标题"""
  68. send_msg('output_ctl', dict(title=title))
  69. def set_output_fixed_height(enabled=True):
  70. r"""开启/关闭页面固高度模式"""
  71. send_msg('output_ctl', dict(output_fixed_height=enabled))
  72. def set_auto_scroll_bottom(enabled=True):
  73. r"""开启/关闭页面自动滚动到底部"""
  74. send_msg('output_ctl', dict(auto_scroll_bottom=enabled))
  75. def _parse_scope(name):
  76. """获取实际用于前端html页面中的id属性
  77. :param str name:
  78. """
  79. name = name.replace(' ', '-')
  80. return 'pywebio-scope-%s' % name
  81. def set_scope(name, container_scope=Scope.Current, position=OutputPosition.BOTTOM, if_exist='none'):
  82. """创建一个新的scope.
  83. :param str name: scope名
  84. :param int/str container_scope: 此scope的父scope. 可以直接指定父scope名或使用 `Scope` 常量. scope不存在时,不进行任何操作.
  85. :param int position: 在父scope中创建此scope的位置.
  86. `OutputPosition.TOP` : 在父scope的顶部创建, `OutputPosition.BOTTOM` : 在父scope的尾部创建
  87. :param str if_exist: 已经存在 ``name`` scope 时如何操作:
  88. - `'none'` 表示不进行任何操作
  89. - `'remove'` 表示先移除旧scope再创建新scope
  90. - `'clear'` 表示将旧scope的内容清除,不创建新scope
  91. 默认为 `'none'`
  92. """
  93. if isinstance(container_scope, int):
  94. container_scope = get_current_session().get_scope_name(container_scope)
  95. send_msg('output_ctl', dict(set_scope=_parse_scope(name),
  96. container=_parse_scope(container_scope),
  97. position=position, if_exist=if_exist))
  98. def clear(scope=Scope.Current):
  99. """清空scope内容
  100. :param int/str scope: 可以直接指定scope名或使用 `Scope` 常量
  101. """
  102. scope_name = _parse_scope(scope)
  103. send_msg('output_ctl', dict(clear=scope_name))
  104. def remove(scope):
  105. """移除Scope"""
  106. send_msg('output_ctl', dict(remove=_parse_scope(scope)))
  107. def scroll_to(scope, position=Position.TOP):
  108. """scroll_to(scope, position=Position.TOP)
  109. 将页面滚动到 ``scope`` Scope处
  110. :param str scope: Scope名
  111. :param str position: 将Scope置于屏幕可视区域的位置。可用值:
  112. * ``Position.TOP`` : 滚动页面,让Scope位于屏幕可视区域顶部
  113. * ``Position.MIDDLE`` : 滚动页面,让Scope位于屏幕可视区域中间
  114. * ``Position.BOTTOM`` : 滚动页面,让Scope位于屏幕可视区域底部
  115. """
  116. send_msg('output_ctl', dict(scroll_to=_parse_scope(scope), position=position))
  117. def _get_output_spec(type, scope, position, **other_spec):
  118. """
  119. 获取 ``output`` 指令的spec字段
  120. :param str type: 输出类型
  121. :param int/str scope: 输出到的scope
  122. :param int position: 在scope输出的位置, `OutputPosition.TOP` : 输出到scope的顶部, `OutputPosition.BOTTOM` : 输出到scope的尾部
  123. :param other_spec: 额外的输出参数,值为None的参数不会包含到返回值中
  124. :return dict: ``output`` 指令的spec字段
  125. """
  126. spec = dict(type=type)
  127. spec.update({k: v for k, v in other_spec.items() if v is not None})
  128. if isinstance(scope, int):
  129. scope_name = get_current_session().get_scope_name(scope)
  130. else:
  131. scope_name = scope
  132. spec['scope'] = _parse_scope(scope_name)
  133. spec['position'] = position
  134. return spec
  135. def put_text(text, inline=False, scope=Scope.Current, position=OutputPosition.BOTTOM) -> OutputReturn:
  136. """
  137. 输出文本内容
  138. :param any text: 文本内容
  139. :param bool inline: 文本行末不换行。默认换行
  140. :param int/str scope: 内容输出的目标scope, 若scope不存在,则不进行任何输出操作。
  141. `scope` 可以直接指定目标Scope名,或者使用int通过索引Scope栈来确定Scope:0表示最顶层也就是ROOT Scope,-1表示当前Scope,-2表示当前Scope的父Scope,...
  142. :param int position: 在scope中输出的位置。
  143. position为非负数时表示输出到scope的第position个(从0计数)子元素的前面;position为负数时表示输出到scope的倒数第position个(从-1计数)元素之后。
  144. 参数 `scope` 和 `position` 的更多使用说明参见 :ref:`用户手册 <scope_param>`
  145. """
  146. spec = _get_output_spec('text', content=str(text), inline=inline, scope=scope, position=position)
  147. return OutputReturn(spec)
  148. def put_html(html, scope=Scope.Current, position=OutputPosition.BOTTOM) -> OutputReturn:
  149. """
  150. 输出Html内容。
  151. 与支持通过Html输出内容到 `Jupyter Notebook <https://nbviewer.jupyter.org/github/ipython/ipython/blob/master/examples/IPython%20Kernel/Rich%20Output.ipynb#HTML>`_ 的库兼容。
  152. :param html: html字符串或 实现了 `IPython.display.HTML` 接口的类的实例
  153. :param int scope, position: 与 `put_text` 函数的同名参数含义一致
  154. """
  155. if hasattr(html, '__html__'):
  156. html = html.__html__()
  157. spec = _get_output_spec('html', content=html, scope=scope, position=position)
  158. return OutputReturn(spec)
  159. def put_code(content, langage='', scope=Scope.Current, position=OutputPosition.BOTTOM) -> OutputReturn:
  160. """
  161. 输出代码块
  162. :param str content: 代码内容
  163. :param str langage: 代码语言
  164. :param int scope, position: 与 `put_text` 函数的同名参数含义一致
  165. """
  166. code = "```%s\n%s\n```" % (langage, content)
  167. return put_markdown(code, scope=scope, position=position)
  168. def put_markdown(mdcontent, strip_indent=0, lstrip=False, scope=Scope.Current,
  169. position=OutputPosition.BOTTOM) -> OutputReturn:
  170. """
  171. 输出Markdown内容。
  172. :param str mdcontent: Markdown文本
  173. :param int strip_indent: 对于每一行,若前 ``strip_indent`` 个字符都为空格,则将其去除
  174. :param bool lstrip: 是否去除每一行开始的空白符
  175. :param int scope, position: 与 `put_text` 函数的同名参数含义一致
  176. 当在函数中使用Python的三引号语法输出多行内容时,为了排版美观可能会对Markdown文本进行缩进,
  177. 这时候,可以设置 ``strip_indent`` 或 ``lstrip`` 来防止Markdown错误解析(但不要同时使用 ``strip_indent`` 和 ``lstrip`` )::
  178. # 不使用strip_indent或lstrip
  179. def hello():
  180. put_markdown(r\""" # H1
  181. This is content.
  182. \""")
  183. # 使用lstrip
  184. def hello():
  185. put_markdown(r\""" # H1
  186. This is content.
  187. \""", lstrip=True)
  188. # 使用strip_indent
  189. def hello():
  190. put_markdown(r\""" # H1
  191. This is content.
  192. \""", strip_indent=4)
  193. """
  194. if strip_indent:
  195. lines = (
  196. i[strip_indent:] if (i[:strip_indent] == ' ' * strip_indent) else i
  197. for i in mdcontent.splitlines()
  198. )
  199. mdcontent = '\n'.join(lines)
  200. if lstrip:
  201. lines = (i.lstrip() for i in mdcontent.splitlines())
  202. mdcontent = '\n'.join(lines)
  203. spec = _get_output_spec('markdown', content=mdcontent, scope=scope, position=position)
  204. return OutputReturn(spec)
  205. @safely_destruct_output_when_exp('tdata')
  206. def put_table(tdata, header=None, span=None, scope=Scope.Current, position=OutputPosition.BOTTOM) -> OutputReturn:
  207. """
  208. 输出表格
  209. :param list tdata: 表格数据。列表项可以为 ``list`` 或者 ``dict`` , 单元格的内容可以为字符串或 ``put_xxx`` 类型的输出函数,字符串内容的单元格显示时会被当作html。
  210. :param list header: 设定表头。
  211. 当 ``tdata`` 的列表项为 ``list`` 类型时,若省略 ``header`` 参数,则使用 ``tdata`` 的第一项作为表头。
  212. 当 ``tdata`` 为字典列表时,使用 ``header`` 指定表头顺序,不可省略。
  213. 此时, ``header`` 格式可以为 <字典键>列表 或者 ``(<显示文本>, <字典键>)`` 列表。
  214. :param dict span: 表格的跨行/跨列信息,格式为 ``{ (行id,列id):{"col": 跨列数, "row": 跨行数} }``
  215. 其中 ``行id`` 和 ``列id`` 为将表格转为二维数组后的需要跨行/列的单元格,二维数据包含表头,``id`` 从 0 开始记数。
  216. :param int scope, position: 与 `put_text` 函数的同名参数含义一致
  217. 使用示例::
  218. # 'Name'单元格跨2行、'Address'单元格跨2列
  219. put_table([
  220. ['Name', 'Address'],
  221. ['City', 'Country'],
  222. ['Wang', 'Beijing', 'China'],
  223. ['Liu', 'New York', 'America'],
  224. ], span={(0,0):{"row":2}, (0,1):{"col":2}})
  225. # 单元格为 ``put_xxx`` 类型的输出函数
  226. put_table([
  227. ['Type', 'Content'],
  228. ['html', 'X<sup>2</sup>'],
  229. ['text', put_text('<hr/>')],
  230. ['buttons', put_buttons(['A', 'B'], onclick=...)],
  231. ['markdown', put_markdown('`Awesome PyWebIO!`')],
  232. ['file', put_file('hello.text', b'')],
  233. ['table', put_table([['A', 'B'], ['C', 'D']])]
  234. ])
  235. # 设置表头
  236. put_table([
  237. ['Wang', 'M', 'China'],
  238. ['Liu', 'W', 'America'],
  239. ], header=['Name', 'Gender', 'Address'])
  240. # dict类型的表格行
  241. put_table([
  242. {"Course":"OS", "Score": "80"},
  243. {"Course":"DB", "Score": "93"},
  244. ], header=["Course", "Score"]) # or header=[("课程", "Course"), ("得分" ,"Score")]
  245. .. versionadded:: 0.3
  246. 单元格的内容支持 ``put_xxx`` 类型的输出函数
  247. """
  248. # Change ``dict`` row table to list row table
  249. if tdata and isinstance(tdata[0], dict):
  250. if isinstance(header[0], (list, tuple)):
  251. header_ = [h[0] for h in header]
  252. order = [h[-1] for h in header]
  253. else:
  254. header_ = order = header
  255. tdata = [
  256. [row.get(k, '') for k in order]
  257. for row in tdata
  258. ]
  259. header = header_
  260. if header:
  261. tdata = [header, *tdata]
  262. span = span or {}
  263. span = {('%s,%s' % row_col): val for row_col, val in span.items()}
  264. spec = _get_output_spec('table', data=tdata, span=span, scope=scope, position=position)
  265. return OutputReturn(spec)
  266. def _format_button(buttons):
  267. """
  268. 格式化按钮参数
  269. :param buttons: button列表, button可用形式:
  270. {label:, value:, }
  271. (label, value, )
  272. value 单值,label等于value
  273. :return: [{value:, label:, }, ...]
  274. """
  275. btns = []
  276. for btn in buttons:
  277. if isinstance(btn, Mapping):
  278. assert 'value' in btn and 'label' in btn, 'actions item must have value and label key'
  279. elif isinstance(btn, (list, tuple)):
  280. assert len(btn) == 2, 'actions item format error'
  281. btn = dict(zip(('label', 'value'), btn))
  282. else:
  283. btn = dict(value=btn, label=btn)
  284. btns.append(btn)
  285. return btns
  286. def table_cell_buttons(buttons, onclick, **callback_options) -> str:
  287. """
  288. 在表格中显示一组按钮
  289. :param str buttons, onclick, save: 与 `put_buttons` 函数的同名参数含义一致
  290. .. _table_cell_buttons-code-sample:
  291. 使用示例::
  292. from functools import partial
  293. def edit_row(choice, row):
  294. put_text("You click %s button at row %s" % (choice, row))
  295. put_table([
  296. ['Idx', 'Actions'],
  297. ['1', table_cell_buttons(['edit', 'delete'], onclick=partial(edit_row, row=1))],
  298. ['2', table_cell_buttons(['edit', 'delete'], onclick=partial(edit_row, row=2))],
  299. ['3', table_cell_buttons(['edit', 'delete'], onclick=partial(edit_row, row=3))],
  300. ])
  301. .. deprecated:: 0.3
  302. Use :func:`put_buttons()` instead
  303. """
  304. logger.warning("pywebio.output.table_cell_buttons() is deprecated in version 0.3 and will be removed in 1.0, "
  305. "use pywebio.output.put_buttons() instead.")
  306. btns = _format_button(buttons)
  307. callback_id = output_register_callback(onclick, **callback_options)
  308. tpl = '<button type="button" value="{value}" class="btn btn-primary btn-sm" ' \
  309. 'onclick="WebIO.DisplayAreaButtonOnClick(this, \'%s\')">{label}</button>' % callback_id
  310. btns_html = [tpl.format(**b) for b in btns]
  311. return ' '.join(btns_html)
  312. def put_buttons(buttons, onclick, small=None, scope=Scope.Current, position=OutputPosition.BOTTOM,
  313. **callback_options) -> OutputReturn:
  314. """
  315. 输出一组按钮
  316. :param list buttons: 按钮列表。列表项的可用形式有:
  317. * dict: ``{label:选项标签, value:选项值}``
  318. * tuple or list: ``(label, value)``
  319. * 单值: 此时label和value使用相同的值
  320. :type onclick: Callable / list
  321. :param onclick: 按钮点击回调函数. ``onclick`` 可以是函数或者函数组成的列表.
  322. ``onclick`` 为函数时, 签名为 ``onclick(btn_value)``. ``btn_value`` 为被点击的按钮的 ``value`` 值
  323. ``onclick`` 为列表时,列表内函数的签名为 ``func()``. 此时,回调函数与 ``buttons`` 一一对应
  324. | Tip: 可以使用 ``functools.partial`` 来在 ``onclick`` 中保存更多上下文信息.
  325. | Note: 当使用基于协程的会话实现时,回调函数可以使用协程函数.
  326. :param bool small: 是否显示小号按钮,默认为False
  327. :param int scope, position: 与 `put_text` 函数的同名参数含义一致
  328. :param callback_options: 回调函数的其他参数。根据选用的 session 实现有不同参数
  329. CoroutineBasedSession 实现
  330. * mutex_mode: 互斥模式。默认为 ``False`` 。若为 ``True`` ,则在运行回调函数过程中,无法响应当前按钮组的新点击事件,仅当 ``onclick`` 为协程函数时有效
  331. ThreadBasedSession 实现
  332. * serial_mode: 串行模式模式。默认为 ``False`` 。若为 ``True`` ,则运行当前点击事件时,其他所有新的点击事件都将被排队等待当前点击事件时运行完成。
  333. 不开启 ``serial_mode`` 时,ThreadBasedSession 在新线程中执行回调函数。所以如果回调函数运行时间很短,
  334. 可以关闭 ``serial_mode`` 来提高性能。
  335. 使用示例::
  336. from functools import partial
  337. def row_action(choice, id):
  338. put_text("You click %s button with id: %s" % (choice, id))
  339. put_buttons(['edit', 'delete'], onclick=partial(row_action, id=1))
  340. def edit():
  341. ...
  342. def delete():
  343. ...
  344. put_buttons(['edit', 'delete'], onclick=[edit, delete])
  345. """
  346. btns = _format_button(buttons)
  347. if isinstance(onclick, Sequence):
  348. assert len(btns) == len(onclick), "`onclick` and `buttons` must be same length."
  349. onclick = {btn['value']: callback for btn, callback in zip(btns, onclick)}
  350. def click_callback(btn_val):
  351. if isinstance(onclick, dict):
  352. func = onclick.get(btn_val, lambda: None)
  353. return func()
  354. else:
  355. return onclick(btn_val)
  356. callback_id = output_register_callback(click_callback, **callback_options)
  357. spec = _get_output_spec('buttons', callback_id=callback_id, buttons=btns, small=small,
  358. scope=scope, position=position)
  359. return OutputReturn(spec)
  360. def put_image(src, format=None, title='', width=None, height=None,
  361. scope=Scope.Current, position=OutputPosition.BOTTOM) -> OutputReturn:
  362. """输出图片。
  363. :param src: 图片内容. 类型可以为字符串类型的URL或者是 bytes-like object 或者为 ``PIL.Image.Image`` 实例
  364. :param str title: 图片描述
  365. :param str width: 图像的宽度,单位是CSS像素(数字px)或者百分比(数字%)。
  366. :param str height: 图像的高度,单位是CSS像素(数字px)或者百分比(数字%)。可以只指定 width 和 height 中的一个值,浏览器会根据原始图像进行缩放。
  367. :param str format: 图片格式。如 ``png`` , ``jpeg`` , ``gif`` 等, 仅在 `src` 为非URL时有效
  368. :param int scope, position: 与 `put_text` 函数的同名参数含义一致
  369. """
  370. if isinstance(src, PILImage):
  371. format = src.format
  372. imgByteArr = io.BytesIO()
  373. src.save(imgByteArr, format=format)
  374. src = imgByteArr.getvalue()
  375. if isinstance(src, (bytes, bytearray)):
  376. b64content = b64encode(src).decode('ascii')
  377. format = '' if format is None else ('image/%s' % format)
  378. src = "data:{format};base64, {b64content}".format(format=format, b64content=b64content)
  379. width = 'width="%s"' % width if width is not None else ''
  380. height = 'height="%s"' % height if height is not None else ''
  381. html = r'<img src="{src}" alt="{title}" {width} {height}/>'.format(src=src, title=title, height=height, width=width)
  382. return put_html(html, scope=scope, position=position)
  383. def put_file(name, content, scope=Scope.Current, position=OutputPosition.BOTTOM) -> OutputReturn:
  384. """输出文件。
  385. 在浏览器上的显示为一个以文件名为名的链接,点击链接后浏览器自动下载文件。
  386. :param str name: 文件名
  387. :param content: 文件内容. 类型为 bytes-like object
  388. :param int scope, position: 与 `put_text` 函数的同名参数含义一致
  389. """
  390. content = b64encode(content).decode('ascii')
  391. spec = _get_output_spec('file', name=name, content=content, scope=scope, position=position)
  392. return OutputReturn(spec)
  393. def put_link(name, url=None, app=None, new_window=False, scope=Scope.Current,
  394. position=OutputPosition.BOTTOM) -> OutputReturn:
  395. """输出链接到其他页面或PyWebIO App的超链接
  396. :param str name: 链接名称
  397. :param str url: 链接到的页面地址
  398. :param str app: 链接到的PyWebIO应用名
  399. :param bool new_window: 是否在新窗口打开链接
  400. :param int scope, position: 与 `put_text` 函数的同名参数含义一致
  401. ``url`` 和 ``app`` 参数必须指定一个但不可以同时指定
  402. """
  403. assert bool(url is None) != bool(app is None), "Must set `url` or `app` parameter but not both"
  404. href = 'javascript:WebIO.openApp(%r, %d)' % (app, new_window) if app is not None else url
  405. target = '_blank' if (new_window and url) else '_self'
  406. html = '<a href="{href}" target="{target}">{name}</a>'.format(href=href, target=target, name=name)
  407. return put_html(html, scope=scope, position=position)
  408. @safely_destruct_output_when_exp('content')
  409. def put_collapse(title, content, open=False, scope=Scope.Current, position=OutputPosition.BOTTOM) -> OutputReturn:
  410. """输出可折叠的内容
  411. :param str title: 内容标题
  412. :type content: list/str/put_xxx()
  413. :param content: 内容可以为字符串或 ``put_xxx`` 类输出函数的返回值,或者为它们组成的列表。字符串内容会被看作html
  414. :param bool open: 是否默认展开折叠内容。默认不展开内容
  415. :param int scope, position: 与 `put_text` 函数的同名参数含义一致
  416. """
  417. if not isinstance(content, (list, tuple)):
  418. content = [content]
  419. for item in content:
  420. assert isinstance(item, (str, OutputReturn)), "put_collapse() content must be list of str/put_xxx()"
  421. tpl = """
  422. <details {{#open}}open{{/open}}>
  423. <summary>{{title}}</summary>
  424. {{#contents}}
  425. {{& pywebio_output_parse}}
  426. {{/contents}}
  427. </details>
  428. """
  429. return put_widget(tpl, dict(title=title, contents=content, open=open), scope=scope, position=position)
  430. @safely_destruct_output_when_exp('data')
  431. def put_widget(template, data, scope=Scope.Current, position=OutputPosition.BOTTOM) -> OutputReturn:
  432. """输出自定义的控件
  433. :param template: html模版,使用 `mustache.js <https://github.com/janl/mustache.js>`_ 语法
  434. :param dict data: 渲染模版使用的数据.
  435. 数据可以包含输出函数( ``put_xxx()`` )的返回值, 可以使用 ``pywebio_output_parse`` 函数来解析 ``put_xxx()`` 内容.
  436. ⚠️:使用 ``pywebio_output_parse`` 函数时,需要关闭mustache的html转义: ``{{& pywebio_output_parse}}`` , 参见下文示例.
  437. :param int scope, position: 与 `put_text` 函数的同名参数含义一致
  438. :Example:
  439. ::
  440. tpl = '''
  441. <details>
  442. <summary>{{title}}</summary>
  443. {{#contents}}
  444. {{& pywebio_output_parse}}
  445. {{/contents}}
  446. </details>
  447. '''
  448. put_widget(tpl, {
  449. "title": 'More content',
  450. "contents": [
  451. put_text('text'),
  452. put_markdown('~~删除线~~'),
  453. put_table([
  454. ['商品', '价格'],
  455. ['苹果', '5.5'],
  456. ['香蕉', '7'],
  457. ])
  458. ]
  459. })
  460. """
  461. spec = _get_output_spec('custom_widget', template=template, data=data, scope=scope, position=position)
  462. return OutputReturn(spec)
  463. @safely_destruct_output_when_exp('content')
  464. def popup(title, content, size=PopupSize.NORMAL, implicit_close=True, closable=True):
  465. """popup(title, content, size=PopupSize.NORMAL, implicit_close=True, closable=True)
  466. 显示弹窗
  467. :param str title: 弹窗标题
  468. :type content: list/str/put_xxx()
  469. :param content: 弹窗内容. 可以为字符串或 ``put_xxx`` 类输出函数的返回值,或者为它们组成的列表。字符串内容会被看作html
  470. :param str size: 弹窗窗口大小,可选值:
  471. * ``LARGE`` : 大尺寸
  472. * ``NORMAL`` : 普通尺寸
  473. * ``SMALL`` : 小尺寸
  474. :param bool implicit_close: 是否可以通过点击弹窗外的内容或按下 ``Esc`` 键来关闭弹窗
  475. :param bool closable: 是否可由用户关闭弹窗. 默认情况下,用户可以通过点击弹窗右上角的关闭按钮来关闭弹窗,
  476. 设置为 ``False`` 时弹窗仅能通过 :func:`popup_close()` 关闭, ``implicit_close`` 参数被忽略.
  477. Example::
  478. popup('popup title', 'popup html content', size=PopupSize.SMALL)
  479. popup('Popup title', [
  480. '<h3>Popup Content</h3>',
  481. put_text('html: <br/>'),
  482. put_table([['A', 'B'], ['C', 'D']]),
  483. put_buttons(['close_popup()'], onclick=lambda _: close_popup())
  484. ])
  485. """
  486. if not isinstance(content, (list, tuple)):
  487. content = [content]
  488. for item in content:
  489. assert isinstance(item, (str, OutputReturn)), "popup() content must be list of str/put_xxx()"
  490. send_msg(cmd='popup', spec=dict(content=OutputReturn.jsonify(content), title=title, size=size,
  491. implicit_close=implicit_close, closable=closable))
  492. def close_popup():
  493. """关闭弹窗"""
  494. send_msg(cmd='close_popup')
  495. clear_scope = clear
  496. def use_scope(name=None, clear=False, create_scope=True, **scope_params):
  497. """scope的上下文管理器和装饰器
  498. :param name: scope名. 若为None则生成一个全局唯一的scope名
  499. :param bool clear: 是否要清除scope内容
  500. :param bool create_scope: scope不存在时是否创建scope
  501. :param scope_params: 创建scope时传入set_scope()的参数. 仅在 `create_scope=True` 时有效.
  502. :Usage:
  503. ::
  504. with use_scope(...):
  505. put_xxx()
  506. @use_scope(...)
  507. def app():
  508. put_xxx()
  509. """
  510. if name is None:
  511. name = random_str(10)
  512. class use_scope_:
  513. def __enter__(self):
  514. if create_scope:
  515. set_scope(name, **scope_params)
  516. if clear:
  517. clear_scope(name)
  518. get_current_session().push_scope(name)
  519. return name
  520. def __exit__(self, exc_type, exc_val, exc_tb):
  521. """该方法如果返回True ,说明上下文管理器可以处理异常,使得 with 语句终止异常传播"""
  522. get_current_session().pop_scope()
  523. return False # Propagate Exception
  524. def __call__(self, func):
  525. """装饰器"""
  526. @wraps(func)
  527. def wrapper(*args, **kwargs):
  528. self.__enter__()
  529. try:
  530. return func(*args, **kwargs)
  531. finally:
  532. self.__exit__(None, None, None)
  533. @wraps(func)
  534. async def coro_wrapper(*args, **kwargs):
  535. self.__enter__()
  536. try:
  537. return await func(*args, **kwargs)
  538. finally:
  539. self.__exit__(None, None, None)
  540. if iscoroutinefunction(func):
  541. return coro_wrapper
  542. else:
  543. return wrapper
  544. return use_scope_()