PyWebIO

PyWebIO provides a diverse set of imperative functions to obtain user input and output content 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. Using PyWebIO, developers can write applications just like writing terminal scripts (interaction based on input and print function), without the need to have knowledge of HTML and JS. PyWebIO is ideal for quickly building interactive applications that don’t require a complicated user interface.

Features

  • Use synchronization instead of callback-based method to get input

  • Non-declarative layout, simple and efficient

  • Less intrusive: old script code can be transformed into a Web service only by modifying the input and output operation

  • Support integration into existing web services, currently supports Flask, Django, Tornado, aiohttp and FastAPI(Starlette) framework

  • Support for asyncio and coroutine

  • Support data visualization with third-party libraries

Installation

Stable version:

pip3 install -U pywebio

Development version:

pip3 install -U https://github.com/pywebio/PyWebIO/archive/dev-release.zip

Prerequisites: PyWebIO requires Python 3.5.2 or newer

Hello, world

Here is a simple PyWebIO script to calculate the BMI

# A simple script to calculate BMI
from pywebio.input import input, FLOAT
from pywebio.output import put_text

def bmi():
    height = input("Input your height(cm):", type=FLOAT)
    weight = input("Input your weight(kg):", type=FLOAT)

    BMI = weight / (height / 100) ** 2

    top_status = [(16, 'Severely underweight'), (18.5, 'Underweight'),
                  (25, 'Normal'), (30, 'Overweight'),
                  (35, '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 after using the input and output functions provided by PyWebIO, you can interact with the code in the browser:

_images/demo.gif

In the last line of the above code, changing the function call bmi() to pywebio.start_server(bmi, port=80) will start a bmi web service on port 80 ( online Demo ).

If you want to integrate the bmi() service into an existing web framework, you can visit Integration with a web framework section of this document.

Indices and tables

Discussion and support