input_usage.py 5.9 KB

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