1
0

output_usage.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. from pywebio import start_server
  2. from pywebio.output import *
  3. from pywebio.session import info as session_info
  4. from functools import partial
  5. def t(eng, chinese):
  6. """return English or Chinese text according to the user's browser language"""
  7. return chinese if 'zh' in session_info.user_language else eng
  8. def code_block(code, strip_indent=4):
  9. if strip_indent:
  10. lines = (
  11. i[strip_indent:] if (i[:strip_indent] == ' ' * strip_indent) else i
  12. for i in code.splitlines()
  13. )
  14. code = '\n'.join(lines)
  15. code = code.strip('\n')
  16. def run_code(code, scope):
  17. with use_scope(scope):
  18. exec(code, globals())
  19. with use_scope() as scope:
  20. put_code(code, 'python')
  21. put_buttons([{'label': t('Run', '运行'), 'value': '', 'color': 'success'}],
  22. onclick=[partial(run_code, code=code, scope=scope)], small=True)
  23. async def main():
  24. """PyWebIO Output Usage
  25. Demonstrate various output usage supported by PyWebIO.
  26. 演示PyWebIO输出模块的使用
  27. """
  28. put_markdown(t("""# PyWebIO Output demo
  29. You can get the source code of this demo in [here](https://github.com/wang0618/PyWebIO/blob/dev/demos/output_usage.py)
  30. This demo only introduces part of the functions of the PyWebIO output module. For the complete features, please refer to the [User Guide](https://pywebio.readthedocs.io/zh_CN/latest/guide.html).
  31. The output functions are all defined in the `pywebio.output` module and can be imported using `from pywebio.output import *`.
  32. """, """# PyWebIO 输出演示
  33. 在[这里](https://github.com/wang0618/PyWebIO/blob/dev/demos/output_usage.py)可以获取本Demo的源码。
  34. 本Demo仅提供了PyWebIO输出模块的部分功能的演示,完整特性请参阅[用户指南](https://pywebio.readthedocs.io/zh_CN/latest/guide.html)。
  35. PyWebIO的输出函数都定义在 `pywebio.output` 模块中,可以使用 `from pywebio.output import *` 引入。
  36. ### 基本输出
  37. PyWebIO提供了一些便捷函数来输出表格、链接等格式:
  38. """))
  39. code_block(t(r"""
  40. # Text Output
  41. put_text("Hello world!")
  42. # Table Output
  43. put_table([
  44. ['Commodity', 'Price'],
  45. ['Apple', '5.5'],
  46. ['Banana', '7'],
  47. ])
  48. # Markdown Output
  49. put_markdown('~~Strikethrough~~')
  50. # File Output
  51. put_file('hello_word.txt', b'hello word!')
  52. """, r"""
  53. # 文本输出
  54. put_text("Hello world!")
  55. # 表格输出
  56. put_table([
  57. ['商品', '价格'],
  58. ['苹果', '5.5'],
  59. ['香蕉', '7'],
  60. ])
  61. # Markdown输出
  62. put_markdown('~~删除线~~')
  63. # 文件输出
  64. put_file('hello_word.txt', b'hello word!')
  65. """))
  66. put_markdown(t(r"""For all output functions provided by PyWebIO, please refer to the [document](https://pywebio.readthedocs.io/en/latest/output.html#output-func-list).
  67. ### Combined Output
  68. The output functions whose name starts with `put_` can be combined with some output functions as part of the final output:
  69. You can pass `put_xxx()` calls to `put_table()` as cell content:
  70. """, r"""PyWebIO提供的全部输出函数请参考[PyWebIO文档](https://pywebio.readthedocs.io/zh_CN/latest/output.html#output-func-list)
  71. ### 组合输出
  72. 函数名以 `put_` 开始的输出函数,可以与一些输出函数组合使用,作为最终输出的一部分。
  73. 比如`put_table()`支持以`put_xxx()`调用作为单元格内容:
  74. """))
  75. code_block(r"""
  76. put_table([
  77. ['Type', 'Content'],
  78. ['html', put_html('X<sup>2</sup>')],
  79. ['text', '<hr/>'], # equal to ['text', put_text('<hr/>')]
  80. ['buttons', put_buttons(['A', 'B'], onclick=toast)],
  81. ['markdown', put_markdown('`Awesome PyWebIO!`')],
  82. ['file', put_file('hello.text', b'hello world')],
  83. ['table', put_table([['A', 'B'], ['C', 'D']])]
  84. ])
  85. """)
  86. put_markdown(t(r"Similarly, you can pass `put_xxx()` calls to `popup()` as the popup content:",
  87. r"类似地,`popup()`也可以将`put_xxx()`调用作为弹窗内容:"))
  88. code_block(r"""
  89. popup('Popup title', [
  90. put_html('<h3>Popup Content</h3>'),
  91. 'plain html: <br/>', # equal to put_text('plain html: <br/>')
  92. put_table([['A', 'B'], ['C', 'D']]),
  93. put_buttons(['close_popup()'], onclick=lambda _: close_popup())
  94. ])
  95. """)
  96. put_markdown(t(r"For more output functions that accept `put_xxx()` calls as parameters, please refer to the [document](https://pywebio.readthedocs.io/en/latest/output.html#output-func-list).",
  97. r"更多接受`put_xxx()`作为参数的输出函数请参考[函数文档](https://pywebio.readthedocs.io/zh_CN/latest/output.html#output-func-list)。"))
  98. put_markdown(t(r"""### Callback
  99. PyWebIO allows you to output some buttons and bind callbacks to them. The provided callback function will be executed when the button is clicked.
  100. This is an example:%s
  101. The call to `put_table()` will not block. When user clicks a button, the corresponding callback function will be invoked:
  102. """, r"""### 事件回调
  103. PyWebIO允许你输出一些控件,当控件被点击时执行提供的回调函数,就像编写GUI程序一样。
  104. 下面是一个例子:%s
  105. `put_table()`的调用不会阻塞。当用户点击了某行中的按钮时,PyWebIO会自动调用相应的回调函数:
  106. """) % """
  107. ```python
  108. from functools import partial
  109. def edit_row(choice, row):
  110. put_markdown("> You click`%s` button ar row `%s`" % (choice, row))
  111. put_table([
  112. ['Idx', 'Actions'],
  113. [1, put_buttons(['edit', 'delete'], onclick=partial(edit_row, row=1))],
  114. [2, put_buttons(['edit', 'delete'], onclick=partial(edit_row, row=2))],
  115. [3, put_buttons(['edit', 'delete'], onclick=partial(edit_row, row=3))],
  116. ])
  117. ```
  118. """)
  119. from functools import partial
  120. @use_scope('table-callback')
  121. def edit_row(choice, row):
  122. put_markdown("> You click `%s` button ar row `%s`" % (choice, row))
  123. put_table([
  124. ['Idx', 'Actions'],
  125. [1, put_buttons(['edit', 'delete'], onclick=partial(edit_row, row=1))],
  126. [2, put_buttons(['edit', 'delete'], onclick=partial(edit_row, row=2))],
  127. [3, put_buttons(['edit', 'delete'], onclick=partial(edit_row, row=3))],
  128. ])
  129. set_scope('table-callback')
  130. put_markdown(t("Of course, PyWebIO also supports outputting individual buttons:", "当然,PyWebIO还支持单独的按钮控件:")+r"""
  131. ```python
  132. def btn_click(btn_val):
  133. put_markdown("> You click `%s` button" % btn_val)
  134. put_buttons(['A', 'B', 'C'], onclick=btn_click) # a group of buttons
  135. put_button("Click me", onclick=lambda: toast("Clicked")) # single button
  136. ```
  137. """)
  138. @use_scope('button-callback')
  139. def btn_click(btn_val):
  140. put_markdown("> You click `%s` button" % btn_val)
  141. put_buttons(['A', 'B', 'C'], onclick=btn_click)
  142. put_button("Click me", onclick=lambda: toast("Clicked"))
  143. set_scope('button-callback')
  144. put_markdown(t("In fact, all output can be bound to click events, not just buttons. You can call `onclick()` method after the output function (function name like `put_xxx()`) call:", "事实上,不仅是按钮,所有的输出都可以绑定点击事件。你可以在输出函数之后调用 `onclick()` 方法来绑定点击事件:")+r"""
  145. ```python
  146. put_image('some-image.png').onclick(lambda: toast('You click the image'))
  147. # set onclick in combined output
  148. put_table([
  149. ['Commodity', 'Price'],
  150. ['Apple', put_text('5.5').onclick(lambda: toast('You click the text'))],
  151. ])
  152. ```
  153. """)
  154. put_image('https://www.python.org/static/img/python-logo.png').onclick(lambda: toast('You click the image'))
  155. # set onclick in combined output
  156. put_table([
  157. ['Commodity', 'Price'],
  158. ['Apple', put_text('5.5').onclick(lambda: toast('You click the text'))],
  159. ])
  160. put_markdown(t("The return value of `onclick()` method is the object itself so it can be used in combined output.",
  161. "`onclick()` 方法的返回值为对象本身,所以可以继续用于组合输出中。"))
  162. put_markdown(t(r"""### Output Scope
  163. PyWebIO uses the scope model to give more control to the location of content output. The output area of PyWebIO can be divided into different output domains. The output domain is called Scope in PyWebIO.
  164. The output domain is a container of output content, and each output domain is arranged vertically, and the output domains can also be nested.
  165. Each output function (function name like `put_xxx()`) will output its content to a scope, the default is "current scope". "current scope" is determined by the runtime context. The output function can also manually specify the scope to output. The scope name is unique within the session.
  166. You can use `use_scope()` to open and enter a new output scope, or enter an existing output scope: %s
  167. The above code will generate the following Scope layout:
  168. """, r"""### 输出域Scope
  169. PyWebIO使用Scope模型来对内容输出的位置进行灵活地控制,PyWebIO的内容输出区可以划分出不同的输出域,PyWebIO将输出域称作`Scope`。
  170. 输出域为输出内容的容器,各个输出域之间上下排列,输出域也可以进行嵌套。
  171. 每个输出函数(函数名形如 `put_xxx()` )都会将内容输出到一个Scope,默认为”当前Scope”,”当前Scope”由运行时上下文确定,输出函数也可以手动指定输出到的Scope。Scope名在会话内唯一。
  172. 可以使用 `use_scope()` 开启并进入一个新的输出域,或进入一个已经存在的输出域: %s
  173. 以上代码将会产生如下Scope布局:
  174. """) % """
  175. ```python
  176. with use_scope('A'):
  177. put_text('Text in scope A')
  178. with use_scope('B'):
  179. put_text('Text in scope B')
  180. with use_scope('C'):
  181. put_text('Text in scope C')
  182. ```
  183. """)
  184. with use_scope('A'):
  185. put_text('Text in scope A')
  186. with use_scope('B'):
  187. put_text('Text in scope B')
  188. with use_scope('C'):
  189. put_text('Text in scope C')
  190. put_html("""<style>
  191. #pywebio-scope-A {border: 1px solid red;}
  192. #pywebio-scope-B {border: 1px solid blue;margin:2px}
  193. #pywebio-scope-C {border: 1px solid green;margin-top:2px}
  194. </style><br/>""")
  195. put_markdown(t(r"""The output function (function name like `put_xxx()`) will output the content to the "current scope" by default, and the "current scope" of the runtime context can be set by `use_scope()`.
  196. In addition, you can use the `scope` parameter of the output function to specify the destination scope to output:
  197. """, r"""
  198. 输出函数(函数名形如 `put_xxx()` )在默认情况下,会将内容输出到”当前Scope”,可以通过 `use_scope()` 设置运行时上下文的”当前Scope”。
  199. 此外,也可以通过输出函数的 scope 参数指定输出的目的Scope:
  200. """))
  201. put_grid([
  202. [put_code("put_text('A', scope='A')", 'python'), None, put_buttons([t('Run', '运行')], [lambda: put_text('A', scope='A')])],
  203. [put_code("put_text('B', scope='B')", 'python'), None, put_buttons([t('Run', '运行')], [lambda: put_text('B', scope='B')])],
  204. [put_code("put_text('C', scope='C')", 'python'), None, put_buttons([t('Run', '运行')], [lambda: put_text('C', scope='C')])],
  205. ], cell_widths='1fr 10px auto')
  206. put_markdown(t("The output content can be inserted into any positions of the target scope by using the `position` parameter of the output function.", "输出函数可以使用`position`参数指定内容在Scope中输出的位置") + """
  207. ```python
  208. put_text(now(), scope='A', position=...)
  209. ```
  210. """)
  211. import datetime
  212. put_buttons([('position=%s' % i, i) for i in [1, 2, 3, -1, -2, -3]],
  213. lambda i: put_text(datetime.datetime.now(), position=i, scope='A'), small=True)
  214. put_markdown(t(r"In addition to `use_scope()`, PyWebIO also provides the following scope control functions:",
  215. r"除了 `use_scope()` , PyWebIO同样提供了以下scope控制函数: "))
  216. put_grid([
  217. [put_code("clear('B') # Clear content of Scope B", 'python'), None, put_buttons(['运行'], [lambda: clear('B')])],
  218. [put_code("remove('C') # Remove Scope C", 'python'), None, put_buttons(['运行'], [lambda: remove('C')])],
  219. [put_code("scroll_to('A') # Scroll the page to position of Scope A", 'python'), None, put_buttons(['运行'], [lambda: scroll_to('A')])],
  220. ], cell_widths='1fr 10px auto')
  221. put_markdown(t(r"""### Layout
  222. In general, using the various output functions introduced above is enough to output what you want, but these outputs are arranged vertically. If you want to make a more complex layout (such as displaying a code block on the left side of the page and an image on the right), you need to use layout functions.
  223. The `pywebio.output` module provides 3 layout functions, and you can create complex layouts by combining them:
  224. - `put_row()` : Use row layout to output content. The content is arranged horizontally
  225. - `put_column()` : Use column layout to output content. The content is arranged vertically
  226. - `put_grid()` : Output content using grid layout
  227. Here is an example by combining `put_row()` and `put_column()`:
  228. """, r"""### 布局
  229. 一般情况下,使用上文介绍的各种输出函数足以完成各种内容的展示,但直接调用输出函数产生的输出之间都是竖直排列的,如果想实现更复杂的布局(比如在页 面左侧显示一个代码块,在右侧显示一个图像),就需要借助布局函数。
  230. `pywebio.output` 模块提供了3个布局函数,通过对他们进行组合可以完成各种复杂的布局:
  231. - `put_row()` : 使用行布局输出内容. 内容在水平方向上排列
  232. - `put_column()` : 使用列布局输出内容. 内容在竖直方向上排列
  233. - `put_grid()` : 使用网格布局输出内容
  234. 比如,通过通过组合 `put_row()` 和 `put_column()` 实现的布局:
  235. """))
  236. code_block(r"""
  237. put_row([
  238. put_column([
  239. put_code('A'),
  240. put_row([
  241. put_code('B1'), None, # %s
  242. put_code('B2'), None,
  243. put_code('B3'),
  244. ]),
  245. put_code('C'),
  246. ]), None,
  247. put_code('D'), None,
  248. put_code('E')
  249. ])
  250. """ % t('None represents the space between the output', 'None 表示输出之间的空白'))
  251. put_markdown(t(r"""### Style
  252. If you are familiar with CSS styles, you can use the `style()` method to set a custom style for the output.
  253. You can set the CSS style for a single `put_xxx()` output:
  254. """, r"""### 样式
  255. 如果你熟悉 CSS样式 ,你还可以使用 `style()` 方法给输出设定自定义样式。
  256. 可以给单个的 `put_xxx()` 输出设定CSS样式,也可以配合组合输出使用:
  257. """))
  258. code_block(r"""
  259. put_text('Red').style('color: red')
  260. put_table([
  261. ['A', 'B'],
  262. ['C', put_text('Red').style('color: red')],
  263. ])
  264. """, strip_indent=4)
  265. put_markdown(t("""----
  266. For more information about output of PyWebIO, please visit PyWebIO [User Guide](https://pywebio.readthedocs.io/zh_CN/latest/guide.html) and [output module documentation](https://pywebio.readthedocs.io/zh_CN/latest/output.html).
  267. """, """----
  268. PyWebIO的输出演示到这里就结束了,更多内容请访问PyWebIO[用户指南](https://pywebio.readthedocs.io/zh_CN/latest/guide.html)和[output模块文档](https://pywebio.readthedocs.io/zh_CN/latest/output.html)。
  269. """))
  270. if __name__ == '__main__':
  271. start_server(main, debug=True, port=8080)