index.rst 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. PyWebIO
  2. ==========
  3. PyWebIO提供了一系列命令式的交互函数来在浏览器上获取用户输入和进行输出,可以用于构建简单的Web应用或基于浏览器的GUI应用。
  4. 使用PyWebIO,开发者能像编写终端脚本一样(基于input和print进行交互)来编写应用,无需具备HTML和JS的相关知识。
  5. PyWebIO还可以方便地整合进现有的Web框架,非常适合在短时间内快速构建对UI要求不高的应用。
  6. 特性
  7. ------------
  8. - 使用同步而不是基于回调的方式获取输入,无需在各个步骤之间保存状态,使用更方便
  9. - 代码侵入性小,对于旧脚本代码仅需修改输入输出逻辑
  10. - 支持多用户与并发请求
  11. - 支持整合到现有的Web服务,目前支持与Flask、Django、Tornado、aiohttp框架集成
  12. - 同时支持基于线程的执行模型和基于协程的执行模型
  13. Install
  14. ------------
  15. PyPi安装::
  16. pip3 install -U pywebio
  17. 目前PyWebIO处于快速迭代时期,PyPi上的包更新可能滞后,建议使用源码安装::
  18. pip3 install -U https://code.aliyun.com/wang0618/pywebio/repository/archive.zip
  19. **系统要求**: PyWebIO要求 Python 版本在 3.5.2 及以上
  20. .. _hello_word:
  21. Hello, world
  22. --------------
  23. 这是一个使用PyWebIO计算 `BMI指数 <https://en.wikipedia.org/wiki/Body_mass_index>`_ 的脚本::
  24. # A simple script to calculate BMI
  25. from pywebio.input import input, FLOAT
  26. from pywebio.output import put_text
  27. def bmi():
  28. height = input("请输入你的身高(cm):", type=FLOAT)
  29. weight = input("请输入你的体重(kg):", type=FLOAT)
  30. BMI = weight / (height / 100) ** 2
  31. top_status = [(14.9, '极瘦'), (18.4, '偏瘦'),
  32. (22.9, '正常'), (27.5, '过重'),
  33. (40.0, '肥胖'), (float('inf'), '非常肥胖')]
  34. for top, status in top_status:
  35. if BMI <= top:
  36. put_text('你的 BMI 值: %.1f,身体状态:%s' % (BMI, status))
  37. break
  38. if __name__ == '__main__':
  39. bmi()
  40. 如果没有使用PyWebIO,这只是一个非常简单的脚本,而通过使用PyWebIO提供的输入输出函数,你可以在浏览器中与代码进行交互:
  41. .. image:: /assets/demo.*
  42. :align: center
  43. 将上面代码最后一行对 ``bmi()`` 的直接调用改为使用 `pywebio.start_server(bmi, port=80) <pywebio.platform.start_server>` 便可以在80端口提供 ``bmi()`` 服务( :demo_host:`在线Demo </?pywebio_api=bmi>` )。
  44. 将 ``bmi()`` 服务整合到现有的Web框架请参考 :ref:`与Web框架集成 <integration_web_framework>`
  45. Documentation
  46. -------------
  47. 这个文档同时也提供 `PDF 和 Epub 格式 <https://readthedocs.org/projects/pywebio/downloads/>`_.
  48. .. toctree::
  49. :maxdepth: 2
  50. :caption: 使用手册
  51. guide
  52. input
  53. output
  54. session
  55. platform
  56. libraries_support
  57. demos
  58. misc
  59. .. toctree::
  60. :titlesonly:
  61. releases
  62. .. toctree::
  63. :maxdepth: 2
  64. :caption: 实现文档
  65. spec
  66. Indices and tables
  67. ----------------------
  68. * :ref:`genindex`
  69. * :ref:`modindex`
  70. * :ref:`search`
  71. Discussion and support
  72. ----------------------
  73. * Need help when use PyWebIO? Make a new discussion on `Github Discussions <https://github.com/wang0618/PyWebIO/discussions>`_.
  74. * Report bugs on the `GitHub issue <https://github.com/wang0618/pywebio/issues>`_.