Write interactive web app in script way.
Project description
PyWebIO
Write interactive web app in script way.
[Document] | [Demos] | [Why PyWebIO?]
PyWebIO provides a series of imperative functions to obtain user input and output on the browser, turning the browser into a "rich text terminal", and can be used to build simple web applications or browser-based GUI applications without the need to have knowledge of HTML and JS. PyWebIO can also be easily integrated into existing Web services. PyWebIO is very suitable for quickly building applications that do not require complex UI.
Features:
- Use synchronization instead of a callback-based method to get input
- Non-declarative layout, simple and efficient
- Less intrusive: old script code can be transformed into a Web application only by modifying the input and output operation
- Support integration into existing web services, currently supports Flask, Django, Tornado, aiohttp, FastAPI framework
- Support for
asyncio
and coroutine - Support data visualization with third-party libraries, e.g.,
plotly
,bokeh
,pyecharts
.
Installation
Stable version:
pip3 install -U pywebio
Development version:
pip3 install -U https://code.aliyun.com/wang0618/pywebio/repository/archive.zip
Prerequisites: PyWebIO requires Python 3.5.2 or newer
Quickstart
Hello, world
Here is a simple PyWebIO script to calculate the BMI:
from pywebio.input import input, FLOAT
from pywebio.output import put_text
def bmi():
height = input("Your Height(cm):", type=FLOAT)
weight = input("Your Weight(kg):", type=FLOAT)
BMI = weight / (height / 100) ** 2
top_status = [(14.9, 'Severely underweight'), (18.4, 'Underweight'),
(22.9, 'Normal'), (27.5, 'Overweight'),
(40.0, 'Moderately obese'), (float('inf'), 'Severely obese')]
for top, status in top_status:
if BMI <= top:
put_text('Your BMI: %.1f, category: %s' % (BMI, status))
break
if __name__ == '__main__':
bmi()
This is just a very simple script if you ignore PyWebIO, but using the input and output functions provided by PyWebIO, you can interact with the code in the browser [demo]:
Serve as web service
The above BMI program will exit immediately after the calculation, you can use pywebio.start_server()
to publish the bmi()
function as a web application:
from pywebio import start_server
from pywebio.input import input, FLOAT
from pywebio.output import put_text
def bmi(): # bmi() keep the same
...
if __name__ == '__main__':
start_server(bmi, port=80)
Integration with web framework
To integrate a PyWebIO application into Tornado, all you need is to add a RequestHandler
to the existing Tornado application:
import tornado.ioloop
import tornado.web
from pywebio.platform.tornado import webio_handler
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
if __name__ == "__main__":
application = tornado.web.Application([
(r"/", MainHandler),
(r"/bmi", webio_handler(bmi)), # bmi is the same function as above
])
application.listen(port=80, address='localhost')
tornado.ioloop.IOLoop.current().start()
Now, you can open http://localhost/bmi
for BMI calculation.
For integration with other web frameworks, please refer to document.
Demos
- Basic demo : PyWebIO basic input and output demos and some small applications written using PyWebIO.
- Data visualization demo : Data visualization with the third-party libraries, e.g.,
plotly
,bokeh
,pyecharts
.
Document
Document is on https://pywebio.readthedocs.io
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pywebio-1.4.0.tar.gz
.
File metadata
- Download URL: pywebio-1.4.0.tar.gz
- Upload date:
- Size: 339.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0691ec1949875c8f33b0a53164a4f2cc7ebaa3704f4f41dc1b3b86d9993f1c26 |
|
MD5 | 6fa849661acbe08d939e1fb3da055754 |
|
BLAKE2b-256 | 2121c2a24a0de6036db5f128d683061b3cf5b1e14255e95a4926537ded9da1f0 |