index.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. PyWebIO
  2. ==========
  3. PyWebIO是一个用于在浏览器上获取输入和进行输出的工具库。能够将原有的通过终端交互的脚本快速服务化,供其他人在网络上通过浏览器访问使用;
  4. PyWebIO还可以方便地整合进现有的Web服务,让你不需要编写Html和JS代码,就可以构建出具有良好可用性的Web程序。
  5. 特点
  6. ------------
  7. - 使用同步而不是基于回调的方式获取输入,无需在各个步骤之间保存状态,使用更方便
  8. - 代码侵入性小,对于旧脚本代码仅需修改输入输出逻辑
  9. - 支持多用户与并发请求
  10. - 支持整合到现有的Web服务,目前支持与Tornado和Flask的集成
  11. - 同时支持基于线程的执行模型和基于协程的执行模型
  12. Install
  13. ------------
  14. PyPi安装::
  15. pip3 install -U pywebio
  16. 目前PyWebIO处于快速迭代时期,PyPi上的包更新可能滞后,建议使用源码安装::
  17. pip3 install -U https://code.aliyun.com/wang0618/pywebio/repository/archive.zip
  18. **系统要求**: PyWebIO要求 Python 版本在 3.5.2 及以上
  19. .. _hello_word:
  20. Hello, world
  21. --------------
  22. 这是一个使用PyWebIO计算 `BMI指数 <https://en.wikipedia.org/wiki/Body_mass_index>`_ 的脚本::
  23. # A simple script to calculate BMI
  24. from pywebio.input import input, FLOAT
  25. from pywebio.output import put_text, set_output_fixed_height
  26. def bmi():
  27. set_output_fixed_height(True)
  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. 如果没有使用PywWebIO,这只是一个非常简单的脚本,而通过使用PywWebIO提供的输入输出函数,你可以在浏览器中与代码进行交互:
  41. .. image:: /assets/demo.*
  42. :align: center
  43. 将上面代码最后一行对 ``bmi()`` 的直接调用改为使用 `pywebio.start_server(bmi, port=80) <pywebio.platform.start_server>` 便可以在80端口提供 ``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: 3
  50. :caption: 使用手册
  51. guide
  52. input
  53. output
  54. session
  55. platform
  56. misc
  57. demos
  58. .. toctree::
  59. :maxdepth: 2
  60. :caption: 实现文档
  61. spec
  62. Indices and tables
  63. ----------------------
  64. * :ref:`genindex`
  65. * :ref:`modindex`
  66. * :ref:`search`
  67. Discussion and support
  68. ----------------------
  69. * Need help when use PyWebIO? Send me Email ``wang0.618&qq.com`` (replace ``&`` with ``@`` ).
  70. * Report bugs on the `GitHub issue <https://github.com/wang0618/pywebio/issues>`_.