__init__.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. r"""
  2. The ``platform`` module provides support for deploying PyWebIO applications in different ways.
  3. .. contents::
  4. :local:
  5. .. seealso::
  6. * :ref:`Use Guide: Server mode and Script mode <server_and_script_mode>`
  7. * :ref:`Advanced Topic: Integration with Web Framework <integration_web_framework>`
  8. .. _dir_deploy:
  9. Directory Deploy
  10. -----------------
  11. You can use ``path_deploy()`` or ``path_deploy_http()`` to deploy the PyWebIO applications from a directory.
  12. The python file under this directory need contain the ``main`` function to be seen as the PyWebIO application.
  13. You can access the application by using the file path as the URL.
  14. Note that users can't view and access files or folders whose name begin with the underscore in this directory.
  15. For example, given the following folder structure::
  16. .
  17. ├── A
  18. │ └── a.py
  19. ├── B
  20. │ └── b.py
  21. └── c.py
  22. All three python files contain ``main`` PyWebIO application function.
  23. If you use this directory in `path_deploy() <pywebio.platform.path_deploy>`, you can access the PyWebIO application in
  24. ``b.py`` by using URL ``http://<host>:<port>/A/b``. And if the files have been modified after run
  25. `path_deploy() <pywebio.platform.path_deploy>`, you can use ``reload`` URL parameter to reload application in the file:
  26. ``http://<host>:<port>/A/b?reload``
  27. You can also use the command ``pywebio-path-deploy`` to start a server just like using
  28. `path_deploy() <pywebio.platform.path_deploy>`. For more information, refer ``pywebio-path-deploy --help``
  29. .. autofunction:: pywebio.platform.path_deploy
  30. .. autofunction:: pywebio.platform.path_deploy_http
  31. .. _app_deploy:
  32. Application Deploy
  33. --------------------
  34. The ``start_server()`` functions can start a Python Web server and serve given PyWebIO applications on it.
  35. The ``webio_handler()`` and ``webio_view()`` functions can be used to integrate PyWebIO applications into existing Python Web project.
  36. The ``wsgi_app()`` and ``asgi_app()`` is used to get the WSGI or ASGI app for running PyWebIO applications.
  37. This is helpful when you don't want to start server with the Web framework built-in's.
  38. For example, you want to use other WSGI server, or you are deploying app in a cloud environment.
  39. Note that only Flask, Django and FastApi backend support it.
  40. .. versionchanged:: 1.1
  41. Added the ``cdn`` parameter in ``start_server()``, ``webio_handler()`` and ``webio_view()``.
  42. .. versionchanged:: 1.2
  43. Added the ``static_dir`` parameter in ``start_server()``.
  44. .. versionchanged:: 1.3
  45. Added the ``wsgi_app()`` and ``asgi_app()``.
  46. Tornado support
  47. ^^^^^^^^^^^^^^^^^^^^
  48. There are two protocols (WebSocket and HTTP) can be used to communicates with the browser:
  49. WebSocket
  50. '''''''''''''
  51. .. autofunction:: pywebio.platform.tornado.start_server
  52. .. autofunction:: pywebio.platform.tornado.webio_handler
  53. HTTP
  54. '''''''''''''
  55. .. autofunction:: pywebio.platform.tornado_http.start_server
  56. .. autofunction:: pywebio.platform.tornado_http.webio_handler
  57. Flask support
  58. ^^^^^^^^^^^^^^^^^^^^
  59. When using the Flask as PyWebIO backend server, you need to install Flask by yourself and make sure the version is not less than ``0.10``.
  60. You can install it with the following command::
  61. pip3 install -U flask>=0.10
  62. .. autofunction:: pywebio.platform.flask.webio_view
  63. .. autofunction:: pywebio.platform.flask.wsgi_app
  64. .. autofunction:: pywebio.platform.flask.start_server
  65. Django support
  66. ^^^^^^^^^^^^^^^^^^^^
  67. When using the Django as PyWebIO backend server, you need to install Django by yourself and make sure the version is not less than ``2.2``.
  68. You can install it with the following command::
  69. pip3 install -U django>=2.2
  70. .. autofunction:: pywebio.platform.django.webio_view
  71. .. autofunction:: pywebio.platform.django.wsgi_app
  72. .. autofunction:: pywebio.platform.django.start_server
  73. aiohttp support
  74. ^^^^^^^^^^^^^^^^^^^^
  75. When using the aiohttp as PyWebIO backend server, you need to install aiohttp by yourself and make sure the version is not less than ``3.1``.
  76. You can install it with the following command::
  77. pip3 install -U aiohttp>=3.1
  78. .. autofunction:: pywebio.platform.aiohttp.webio_handler
  79. .. autofunction:: pywebio.platform.aiohttp.start_server
  80. FastAPI/Starlette support
  81. ^^^^^^^^^^^^^^^^^^^^^^^^^
  82. When using the FastAPI/Starlette as PyWebIO backend server, you need to install ``fastapi`` or ``starlette`` by yourself.
  83. Also other dependency packages are required. You can install them with the following command::
  84. pip3 install -U fastapi starlette uvicorn aiofiles websockets
  85. .. autofunction:: pywebio.platform.fastapi.webio_routes
  86. .. autofunction:: pywebio.platform.fastapi.asgi_app
  87. .. autofunction:: pywebio.platform.fastapi.start_server
  88. Other
  89. --------------
  90. .. autofunction:: pywebio.config
  91. .. autofunction:: pywebio.platform.run_event_loop
  92. """
  93. from .adaptor.http import run_event_loop
  94. from .page import config
  95. from .page import seo
  96. from .path_deploy import path_deploy_http, path_deploy
  97. from .tornado import start_server
  98. from . import tornado
  99. try:
  100. from . import flask # enable `pywebio.platform.flask.xxx` expression without `import pywebio.platform.flask`
  101. except Exception:
  102. pass
  103. try:
  104. from . import django
  105. except Exception:
  106. pass
  107. try:
  108. from . import fastapi
  109. except Exception:
  110. pass
  111. try:
  112. from . import tornado_http
  113. except Exception:
  114. pass
  115. try:
  116. from . import aiohttp
  117. except Exception:
  118. pass