spec.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. Server-Client communication protocol
  2. ========================================
  3. PyWebIO uses a server-client architecture, the server executes task code, and interacts with the client (that is, the user browser) through the network. This section introduce the protocol specification for the communication between PyWebIO server and client.
  4. There are two communication methods between server and client: WebSocket and Http.
  5. When using Tornado or aiohttp backend, the server and client communicate through WebSocket, when using Flask or Django backend, the server and client communicate through Http.
  6. **WebSocket communication**
  7. The server and the client send json-serialized message through WebSocket connection
  8. **Http communication**
  9. * The client polls the backend through Http GET requests, and the backend returns a list of PyWebIO messages serialized in json
  10. * When the user submits the form or clicks the button, the client submits data to the backend through Http POST request
  11. In the following, the data sent by the server to the client is called command, and the data sent by the client to the server is called event.
  12. The following describes the format of command and event
  13. Command
  14. ------------
  15. Command is sent by the server to the client. The basic format of command is::
  16. {
  17. "command": ""
  18. "task_id": ""
  19. "spec": {}
  20. }
  21. Each fields are described as follows:
  22. * ``command`` : command name
  23. * ``task_id`` : Id of the task that send the command
  24. * ``spec`` : the data of the command, which is different depending on the command name
  25. Note that: the arguments shown above are merely the same with the parameters of corresponding PyWebIO functions,
  26. but there are some differences.
  27. The following describes the ``spec`` fields of different commands:
  28. input_group
  29. ^^^^^^^^^^^^^^^
  30. Show a form in user's browser.
  31. .. list-table:: fields of ``spec``
  32. :header-rows: 1
  33. * - Field
  34. - Required
  35. - Type
  36. - Description
  37. * - label
  38. - False
  39. - str
  40. - Title of the form
  41. * - inputs
  42. - True
  43. - list
  44. - Input items
  45. * - cancelable
  46. - False
  47. - bool
  48. - | Whether the form can be cancelled。
  49. | If cancelable=True, a “Cancel” button will be displayed at the bottom of the form.
  50. | A ``from_cancel`` event is triggered after the user clicks the cancel button.
  51. The ``inputs`` field is a list of input items, each input item is a ``dict``, the fields of the item are as follows:
  52. * label: Label of input field, required.
  53. * type: Input type, required.
  54. * name: Identifier of the input field, required.
  55. * onchange: bool, whether to push input value when input change
  56. * onbulr: bool, whether to push input value when input field `onblur`
  57. * auto_focus: Set focus automatically. At most one item of ``auto_focus`` can be true in the input item list
  58. * help_text: Help text for the input
  59. * Additional HTML attribute of the input element
  60. * Other attributes of different input types
  61. Currently supported ``type`` are:
  62. * text: Plain text input
  63. * number: Number input
  64. * password: Password input
  65. * checkbox: Checkbox
  66. * radio: Radio
  67. * select: Drop-down selection
  68. * textarea: Multi-line text input
  69. * file: File uploading
  70. * actions: Actions selection.
  71. Correspondence between different input types and html input elements:
  72. * text: input[type=text]
  73. * number: input[type=number]
  74. * float: input[type=text], and transform input value to float
  75. * password: input[type=password]
  76. * checkbox: input[type=checkbox]
  77. * radio: input[type=radio]
  78. * select: select https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/select
  79. * textarea: textarea https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/textarea
  80. * file: input[type=file]
  81. * actions: button[type=submit] https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/button
  82. Unique attributes of different input types:
  83. * text,number,password:
  84. * action: Display a button on the right of the input field.
  85. The format of ``action`` is ``{label: button label, callback_id: button click callback id}``
  86. * textarea:
  87. * code: Codemirror options or boolean, same as ``code`` parameter of :func:`pywebio.input.textarea`
  88. * select:
  89. * options: ``{label:, value: , [selected:,] [disabled:]}``
  90. * checkbox:
  91. * options: ``{label:, value: , [selected:,] [disabled:]}``
  92. * inline
  93. * radio:
  94. * options: ``{label:, value: , [selected:,] [disabled:]}``
  95. * inline
  96. * actions
  97. * buttons: ``{label:, value:, [type: 'submit'/'reset'/'cancel'], [disabled:], [color:]}`` .
  98. * file:
  99. * multiple: Whether to allow upload multiple files.
  100. * max_size: The maximum size of a single file, in bytes.
  101. * max_total_size: The maximum size of all files, in bytes.
  102. * slider
  103. * min_value: The minimum permitted value.
  104. * max_value: The maximum permitted value.
  105. * step: The stepping interval.
  106. * float: If need return a float value
  107. update_input
  108. ^^^^^^^^^^^^^^^
  109. Update the input item, you can update the ``spec`` of the input item of the currently displayed form
  110. The ``spec`` fields of ``update_input`` commands:
  111. * target_name: str The name of the target input item.
  112. * target_value: str, optional. Used to filter item in checkbox, radio
  113. * attributes: dist, fields need to be updated
  114. * valid_status: When it is bool, it means setting the state of the input value, pass/fail; when it is 0, it means clear the valid_status flag
  115. * value: Set the value of the item
  116. * label
  117. * placeholder
  118. * invalid_feedback
  119. * valid_feedback
  120. * help_text
  121. * options: only available in checkbox, radio and select type
  122. * other fields of item's ``spec`` // not support the ``inline`` field
  123. close_session
  124. ^^^^^^^^^^^^^^^
  125. Indicates that the server has closed the connection. ``spec`` of the command is empty.
  126. set_session_id
  127. ^^^^^^^^^^^^^^^
  128. Send current session id to client, used to reconnect to server (Only available in websocket connection).
  129. ``spec`` of the command is session id.
  130. destroy_form
  131. ^^^^^^^^^^^^^^^
  132. Destroy the current form. ``spec`` of the command is empty.
  133. Note: The form will not be automatically destroyed after it is submitted, it needs to be explicitly destroyed using this command
  134. output
  135. ^^^^^^^^^^^^^^^
  136. Output content
  137. The ``spec`` fields of ``output`` commands:
  138. * type: content type
  139. * style: str, Additional css style
  140. * container_selector: The css selector of output widget's content slot. If empty(default), use widget self as container
  141. * container_dom_id: The dom id need to be set to output widget's content slot.
  142. * scope: str, CSS selector of the output container. If multiple containers are matched, the content will be output to every matched container
  143. * position: int, see :ref:`scope - User manual <scope_param>`
  144. * click_callback_id:
  145. * Other attributes of different types
  146. ``container_selector`` and ``container_dom_id`` is used to implement output context manager.
  147. Unique attributes of different types:
  148. * type: markdown
  149. * content: str
  150. * options: dict, `marked.js <https://github.com/markedjs/marked>`_ options
  151. * sanitize: bool, Whether to enable a XSS sanitizer for HTML
  152. * type: html
  153. * content: str
  154. * sanitize: bool, Whether to enable a XSS sanitizer for HTML
  155. * type: text
  156. * content: str
  157. * inline: bool, Use text as an inline element (no line break at the end of the text)
  158. * type: buttons
  159. * callback_id:
  160. * buttons:[ {value:, label:, [color:], [disabled:]},...]
  161. * small: bool, Whether to enable small button
  162. * group: bool, Whether to group the buttons together
  163. * link: bool, Whether to make button seem as link.
  164. * outline: bool, Whether enable outline style.
  165. * type: file
  166. * name: File name when downloading
  167. * content: File content with base64 encoded
  168. * type: table
  169. * data: Table data, which is a two-dimensional list, the first row is table header.
  170. * span: cell span info. Format: {"[row id],[col id]": {"row":row span, "col":col span }}
  171. * type: pin
  172. * input: input spec, same as the item of ``input_group.inputs``
  173. * type: scope
  174. * dom_id: the DOM id need to be set to this widget
  175. * contents list: list of output spec
  176. * type: scrollable
  177. * contents:
  178. * min_height:
  179. * max_height:
  180. * keep_bottom:
  181. * border:
  182. * type: tabs
  183. * tabs:
  184. * type: custom_widget
  185. * template:
  186. * data:
  187. pin_values
  188. ^^^^^^^^^^^^^^^
  189. Get the value of the pin widgets. The ``spec`` fields of ``pin_values`` commands:
  190. * names: list, The names of the pin widget
  191. The client will respond with a ``js_yield`` event, with the data field containing ``{ pin_name: pin_value, ... }``
  192. pin_update
  193. ^^^^^^^^^^^^^^^
  194. The ``spec`` fields of ``pin_update`` commands:
  195. * name
  196. * attributes: dist, fields need to be updated
  197. pin_wait
  198. ^^^^^^^^^^^^^^^
  199. The ``spec`` fields of ``pin_wait`` commands:
  200. * names: list,
  201. * timeout: int,
  202. pin_onchange
  203. ^^^^^^^^^^^^^^^
  204. set a callback which is invoked when the value of pin widget is changed
  205. The ``spec`` fields of ``pin_onchange`` commands:
  206. * name: string
  207. * callback_id: string, if ``None``, not set callback
  208. * clear: bool
  209. popup
  210. ^^^^^^^^^^^^^^^
  211. Show popup
  212. The ``spec`` fields of ``popup`` commands:
  213. * title
  214. * content
  215. * size: ``large``, ``normal``, ``small``
  216. * implicit_close
  217. * closable
  218. * dom_id: DOM id of popup container element
  219. toast
  220. ^^^^^^^^^^^^^^^
  221. Show a notification message
  222. The ``spec`` fields of ``popup`` commands:
  223. * content
  224. * duration
  225. * position: `'left'` / `'center'` / `'right'`
  226. * color: hexadecimal color value starting with '#'
  227. * callback_id
  228. close_popup
  229. ^^^^^^^^^^^^^^^
  230. Close the current popup window.
  231. ``spec`` of the command is empty.
  232. set_env
  233. ^^^^^^^^^^^^^^^
  234. Config the environment of current session.
  235. The ``spec`` fields of ``set_env`` commands:
  236. * title (str)
  237. * output_animation (bool)
  238. * auto_scroll_bottom (bool)
  239. * http_pull_interval (int)
  240. * input_panel_fixed (bool)
  241. * input_panel_min_height (int)
  242. * input_panel_init_height (int)
  243. * input_auto_focus (bool)
  244. output_ctl
  245. ^^^^^^^^^^^^^^^
  246. Output control
  247. The ``spec`` fields of ``output_ctl`` commands:
  248. * set_scope: scope name
  249. * container: Specify css selector to the parent scope of target scope.
  250. * position: int, The index where this scope is created in the parent scope.
  251. * if_exist: What to do when the specified scope already exists:
  252. - null: Do nothing
  253. - `'remove'`: Remove the old scope first and then create a new one
  254. - `'clear'`: Just clear the contents of the old scope, but don’t create a new scope
  255. - `'blank'`: Clear the contents of the old scope and keep the height, don’t create a new scope
  256. * loose: css selector of the scope, set the scope not to keep the height (i.e., revoke the effect of ``set_scope(if_exist='blank')``)
  257. * clear: css selector of the scope need to clear
  258. * clear_before
  259. * clear_after
  260. * clear_range:[,]
  261. * scroll_to
  262. * position: top/middle/bottom, Where to place the scope in the visible area of the page
  263. * remove: Remove the specified scope
  264. run_script
  265. ^^^^^^^^^^^^^^^
  266. run javascript code in user's browser
  267. The ``spec`` fields of ``run_script`` commands:
  268. * code: str, code
  269. * args: dict, Local variables passed to js code
  270. * eval: bool, whether to submit the return value of javascript code
  271. download
  272. ^^^^^^^^^^^^^^^
  273. Send file to user
  274. The ``spec`` fields of ``download`` commands:
  275. * name: str, File name when downloading
  276. * content: str, File content in base64 encoding.
  277. Event
  278. ------------
  279. Event is sent by the client to the server. The basic format of event is::
  280. {
  281. event: event name
  282. task_id: ""
  283. data: object/str
  284. }
  285. The ``data`` field is the data carried by the event, and its content varies according to the event.
  286. The ``data`` field of different events is as follows:
  287. input_event
  288. ^^^^^^^^^^^^^^^
  289. Triggered when the form changes
  290. * event_name: Current available value is ``'blur'``, which indicates that the input item loses focus
  291. * name: name of input item
  292. * value: value of input item
  293. note: checkbox and radio do not generate blur events
  294. .. _callback_event:
  295. callback
  296. ^^^^^^^^^^^^^^^
  297. Triggered when the user clicks the button in the page
  298. In the ``callback`` event, ``task_id`` is the ``callback_id`` field of the ``button``;
  299. The ``data`` of the event is the ``value`` of the button that was clicked
  300. from_submit
  301. ^^^^^^^^^^^^^^^
  302. Triggered when the user submits the form
  303. The ``data`` of the event is a dict, whose key is the name of the input item, and whose value is the value of the input item.
  304. from_cancel
  305. ^^^^^^^^^^^^^^^
  306. Cancel input form
  307. The ``data`` of the event is ``None``
  308. js_yield
  309. ^^^^^^^^^^^^^^^
  310. submit data from js. It's a common event to submit data to backend.
  311. The ``data`` of the event is the data need to submit