index.rst 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. PyWebIO
  2. ==========
  3. PyWebIO provides a diverse set of imperative functions to obtain user input and output content on the browser,
  4. turning the browser into a "rich text terminal", and can be used to build simple web applications or browser-based
  5. GUI applications. Using PyWebIO, developers can write applications just like writing terminal scripts
  6. (interaction based on input and print function), without the need to have knowledge of HTML and JS.
  7. PyWebIO is ideal for quickly building interactive applications that don't require a complicated user interface.
  8. Features
  9. ------------
  10. - Use synchronization instead of callback-based method to get input
  11. - Non-declarative layout, simple and efficient
  12. - Less intrusive: old script code can be transformed into a Web service only by modifying the input and output operation
  13. - Support integration into existing web services, currently supports Flask, Django, Tornado, aiohttp and FastAPI(Starlette) framework
  14. - Support for ``asyncio`` and coroutine
  15. - Support data visualization with third-party libraries
  16. Installation
  17. --------------
  18. Stable version::
  19. pip3 install -U pywebio
  20. Development version::
  21. pip3 install -U https://code.aliyun.com/wang0618/pywebio/repository/archive.zip
  22. **Prerequisites**: PyWebIO requires Python 3.5.2 or newer
  23. .. _hello_word:
  24. Hello, world
  25. --------------
  26. Here is a simple PyWebIO script to calculate the `BMI <https://en.wikipedia.org/wiki/Body_mass_index>`_ ::
  27. # A simple script to calculate BMI
  28. from pywebio.input import input, FLOAT
  29. from pywebio.output import put_text
  30. def bmi():
  31. height = input("Input your height(cm):", type=FLOAT)
  32. weight = input("Input your weight(kg):", type=FLOAT)
  33. BMI = weight / (height / 100) ** 2
  34. top_status = [(16, 'Severely underweight'), (18.5, 'Underweight'),
  35. (25, 'Normal'), (30, 'Overweight'),
  36. (35, 'Moderately obese'), (float('inf'), 'Severely obese')]
  37. for top, status in top_status:
  38. if BMI <= top:
  39. put_text('Your BMI: %.1f. Category: %s' % (BMI, status))
  40. break
  41. if __name__ == '__main__':
  42. bmi()
  43. This is just a very simple script if you ignore PyWebIO, but after using the input and output functions provided by PyWebIO,
  44. you can interact with the code in the browser:
  45. .. image:: /assets/demo.*
  46. :width: 450px
  47. :align: center
  48. In the last line of the above code, changing the function call ``bmi()`` to
  49. `pywebio.start_server(bmi, port=80) <pywebio.platform.tornado.start_server>` will start a bmi web service on port 80
  50. ( :demo_host:`online Demo </bmi>` ).
  51. If you want to integrate the ``bmi()`` service into an existing web framework, you can visit
  52. :ref:`Integration with a web framework <integration_web_framework>` section of this document.
  53. Documentation
  54. -------------
  55. This documentation is also available in `PDF and Epub formats <https://readthedocs.org/projects/pywebio/downloads/>`_.
  56. .. toctree::
  57. :maxdepth: 2
  58. :caption: Manual
  59. guide
  60. input
  61. output
  62. session
  63. platform
  64. pin
  65. advanced
  66. libraries_support
  67. .. toctree::
  68. :titlesonly:
  69. cookbook
  70. releases
  71. .. toctree::
  72. :maxdepth: 2
  73. :caption: Battery
  74. battery
  75. .. toctree::
  76. :maxdepth: 2
  77. :caption: Implement Doc
  78. spec
  79. Indices and tables
  80. ----------------------
  81. * :ref:`genindex`
  82. * :ref:`modindex`
  83. * :ref:`search`
  84. Discussion and support
  85. ----------------------
  86. * Need help when use PyWebIO? Make a new discussion on `Github Discussions <https://github.com/wang0618/PyWebIO/discussions>`_.
  87. * Report bugs on the `GitHub issue <https://github.com/wang0618/pywebio/issues>`_.