output.py 29 KB

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