-
Notifications
You must be signed in to change notification settings - Fork 382
Why PyWebIO?
When you are writing some toy programs, you may encounter such a dilemma: a function can be quickly implemented through a script (terminal program), but for the convenience of use, you want to make it be a Web application.
There are several difficulties to migrate a terminal script to a Web application:
-
Need to write additional front-end code to create web pages.
Generally, at least two pages should be written, one is the input form and the other is the result page. If the input in the original script cannot be integrated into a form (for example, some input items need to be determined based on the result of some input items), then more form pages need to be written. -
Due to the stateless of the HTTP protocol, the state needs to be transferred between various pages, and additional code is needed to check the validation of the state.
For example, in Web development, the session mechanism of back-end or the<input type="hidden">
mechanism of front-end is generally used to pass the state between pages. -
Web application cannot achieve real-time output in a single HTTP request, which also increases the difficulty of converting script into Web service.
For example, if the console program needs to perform some time-consuming operations, it can be directly blocked in the main thread and only need to periodically print some logs. In web applications, time-consuming tasks usually need to be completed offline, and the front-end page needs to be polled regularly to display real-time state of the task.
By using PyWebIO, the script code can be migrated to a Web application only by modifying the input and output operation. You don't have to suffer from the difficulties described above. Because from the structure of code, the PyWebIO application is still a console programs except that the interactive medium of the application is changed from the terminal to the browser.
Because of that, PyWebIO can output more diverse content (such as pictures, charts, etc.) than terminal programs, so PyWebIO is very suitable for quickly building web applications that require a lot of interaction.
On the other hand, comparing to terminal scripts, accessing services through the Web also eliminates the trouble of installing dependencies, so PyWebIO is also very suitable for writing some online tools.