input_usage.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. """
  2. Demo of input
  3. ^^^^^^^^^^^^^^^
  4. To demonstrate various input forms supported by PyWebIO
  5. :demo_host:`Demo </?pywebio_api=input_usage>` `Source code <https://github.com/wang0618/PyWebIO/blob/dev/demos/input_usage.py>`_
  6. """
  7. from pywebio import start_server
  8. from pywebio.input import *
  9. from pywebio.output import *
  10. from pywebio.session import set_env
  11. def main():
  12. """PyWebIO输入演示
  13. 演示PyWebIO支持的各种输入形式
  14. """
  15. set_env(auto_scroll_bottom=True)
  16. put_markdown("""# PyWebIO 输入演示
  17. 在[这里](https://github.com/wang0618/PyWebIO/blob/dev/demos/input_usage.py)可以获取本Demo的源码。
  18. 本Demo仅提供了PyWebIO输入模块的部分功能的演示,完整特性请参阅[用户指南](https://pywebio.readthedocs.io/zh_CN/latest/guide.html)。
  19. PyWebIO的输入函数都定义在 `pywebio.input` 模块中,可以使用 `from pywebio.input import *` 引入。
  20. ### 基本输入
  21. 首先是一些基本类型的输入
  22. #### 文本输入
  23. ```python
  24. name = input("What's your name?")
  25. ```
  26. """, lstrip=True)
  27. put_text("这样一行代码的效果如下:",)
  28. name = input("What's your name?")
  29. put_markdown("`name = %r`" % name)
  30. # 其他类型的输入
  31. put_markdown("""PyWebIO的输入函数是同步的,在表单被提交之前,输入函数不会返回。
  32. #### 其他类型的输入:
  33. ```python
  34. # 密码输入
  35. password = input("Input password", type=PASSWORD)
  36. # 下拉选择框
  37. gift = select('Which gift you want?', ['keyboard', 'ipad'])
  38. # CheckBox
  39. agree = checkbox("用户协议", options=['I agree to terms and conditions'])
  40. # Text Area
  41. text = textarea('Text Area', rows=3, placeholder='Some text')
  42. # 文件上传
  43. img = file_upload("Select a image:", accept="image/*")
  44. ```
  45. """, lstrip=True)
  46. password = input("Input password", type=PASSWORD)
  47. put_markdown("`password = %r`" % password)
  48. gift = select('Which gift you want?', ['keyboard', 'ipad'])
  49. put_markdown("`gift = %r`" % gift)
  50. agree = checkbox("用户协议", options=['I agree to terms and conditions'])
  51. put_markdown("`agree = %r`" % agree)
  52. text = textarea('Text Area', rows=3, placeholder='Some text')
  53. put_markdown("`text = %r`" % text)
  54. img = file_upload("Select a image:", accept="image/*", help_text='可以直接选择"提交"')
  55. put_markdown("`img = %r`" % img)
  56. # 输入选项
  57. put_markdown("""#### 输入选项
  58. 输入函数可指定的参数非常丰富:
  59. ```python
  60. input('This is label', type=TEXT, placeholder='This is placeholder',
  61. help_text='This is help text', required=True,
  62. datalist=['candidate1', 'candidate2', 'candidate2'])
  63. ```
  64. """, strip_indent=4)
  65. input('This is label', type=TEXT, placeholder='This is placeholder',
  66. help_text='This is help text', required=True,
  67. datalist=['candidate1', 'candidate2', 'candidate2'])
  68. # 校验函数
  69. put_markdown("""我们可以为输入指定校验函数,校验函数校验通过时返回None,否则返回错误消息:
  70. ```python
  71. def check_age(p): # 检验函数校验通过时返回None,否则返回错误消息
  72. if p < 10:
  73. return 'Too young!!'
  74. if p > 60:
  75. return 'Too old!!'
  76. age = input("How old are you?", type=NUMBER, validate=check_age)
  77. ```
  78. """, strip_indent=4)
  79. def check_age(p): # 检验函数校验通过时返回None,否则返回错误消息
  80. if p < 10:
  81. return 'Too young!!'
  82. if p > 60:
  83. return 'Too old!!'
  84. age = input("How old are you?", type=NUMBER, validate=check_age, help_text='尝试输入一些非法值,比如"8"、"65"')
  85. put_markdown('`age = %r`' % age)
  86. # Codemirror
  87. put_markdown(r"""PyWebIO 的 `textarea()` 输入函数还支持使用 [Codemirror](https://codemirror.net/) 实现代码风格的编辑区,只需使用 `code` 参数传入Codemirror支持的选项即可(最简单的情况是直接传入` code={}` 或 `code=True`):
  88. ```python
  89. code = textarea('Code Edit', code={
  90. 'mode': "python", # 编辑区代码语言
  91. 'theme': 'darcula', # 编辑区darcula主题
  92. }, value='import something\n# Write your python code')
  93. ```
  94. """, strip_indent=4)
  95. code = textarea('Code Edit', code={
  96. 'mode': "python", # 编辑区代码语言
  97. 'theme': 'darcula', # 编辑区darcula主题, Visit https://codemirror.net/demo/theme.html#cobalt to get more themes
  98. }, value='import something\n# Write your python code')
  99. put_markdown("Your code:\n```python\n%s\n```" % code)
  100. # 输入组
  101. put_markdown(r"""### 输入组
  102. `input_group()` 接受单项输入组成的列表作为参数,输入组中需要在每一项输入函数中提供 `name` 参数来用于在结果中标识不同输入项。输入组中同样支持设置校验函数,其接受整个表单数据作为参数。
  103. ```python
  104. def check_form(data): # 检验函数校验通过时返回None,否则返回 (input name,错误消息)
  105. if len(data['name']) > 6:
  106. return ('name', '名字太长!')
  107. if data['age'] <= 0:
  108. return ('age', '年龄不能为负数!')
  109. data = input_group("Basic info", [
  110. input('Input your name', name='name'),
  111. input('Input your age', name='age', type=NUMBER, validate=check_age)
  112. ], validate=check_form)
  113. ```
  114. """, strip_indent=4)
  115. def check_form(data): # 检验函数校验通过时返回None,否则返回 (input name,错误消息)
  116. if len(data['name']) > 6:
  117. return ('name', '名字太长!')
  118. if data['age'] <= 0:
  119. return ('age', '年龄不能为负数!')
  120. data = input_group("Basic info", [
  121. input('Input your name', name='name'),
  122. input('Input your age', name='age', type=NUMBER, validate=check_age)
  123. ], validate=check_form)
  124. put_markdown("`data = %r`" % data)
  125. put_markdown("""----
  126. PyWebIO的输入演示到这里就结束了,更多内容请访问PyWebIO[用户指南](https://pywebio.readthedocs.io/zh_CN/latest/guide.html)和[input模块文档](https://pywebio.readthedocs.io/zh_CN/latest/input.html)。
  127. """, lstrip=True)
  128. if __name__ == '__main__':
  129. start_server(main, debug=True, port=8080, cdn=False)