input_usage.py 5.7 KB

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