I'm back from a hiatus and I've got a cracking Python project for you all!
So What Is It?
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.
So Why Should I Use This?
The humble web browser is everywhere and that means users can access your content anywhere. No custom tkinter interface, and no messy frameworks. Just generate some HTML and you have a basic user interface. The problem is that mixing HTML and Python can prove tricky for some. You can use Anvil to generate HTML user interfaces (I love Anvil, but it can be a little tricky to get going. Anvil is immensely powerful and well worth learning. Flask is also tricky and I've never truly got the hang of it.
PyWebIO is much simpler and starts a server directly on our machine.
A quick demo where I use PyWebIO on @Raspberry_Pi to create a Web interface to scroll messages across @pimoroni Scroll pHAT HD. I'll put the code up for people to play with. #tuesdaytooling https://t.co/hS4L5bOklB pic.twitter.com/wM40yDYRO8— biglesp (@biglesp) June 14, 2022
I can see PyWebIO being a very useful alternative for educators and makers who want to get accessible data from their Python projects.
I'm thinking of Raspberry Pi projects such as...
- Custom user interfaces for robots
- Weather station
- Citizen science data collection
- Raspberry Pi camera controller
PyWebIO is Python, which means we can mix this code with our existing code to add a web app to our projects.
So How Do I Install PyWebIO?
PyWebIO is available via pip
Linux / Mac
pip3 install pywebio
pip install pywebio
So How Do I Use It?
- Open your favourite text editor and we'll start with a quick "Hello World"
- Start a server for the browser session.
from pywebio import start_server
- Import the input and output classes. We'll need these to capture user input and display it in the browser.
from pywebio.input import * from pywebio.output import *
- Import the session for our web app.
from pywebio.session import *
- Import a persistent input to keep the user input. We can refresh the page and reuse the input field.
from pywebio.pin import *
- Create a function called main().
- Inside the function create a variable called name and use input to capture the user's name.
name = input("What is your name?")
- Create a new variable, greetings which will store a personalised message to the user.
greetings = 'Greetings',name,'!'
- Create a popup message to greet the user. Popup is a command in PyWebIO to generate those annoying website popups. But ours is actually nice!
- Create a notification that will visually alert the user. We can use emojiis in the alert, which is nice.
toast('New message 🔔')
- Come out of the function to finally start the server and tell it to run our function and start on port 8080.
start_server(main, port=8080, debug=True)
- Save the code as webform.py and then run it from the terminal / IDE.
- Open a browser to 127.0.0.1:8080 and the form will appear!
Complete Demo Code Listing
from pywebio import start_server from pywebio.input import * from pywebio.output import * from pywebio.session import * from pywebio.pin import * def main(): name = input("What is your name?") greetings = 'Greetings',name,'!' popup('Greetings', greetings) toast('New message 🔔') start_server(main, port=8080, debug=True)
Taking It Further
We're not limited to just simple tasks like this, we have access to inputs such as
- Drop down menus
- Password fields
- Single choice
- Multi-line text input
- File uploads
- Write to files