index.rst 3.4 KB

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