I stumbled across the pywebview project a couple of weeks ago. The pywebview package “is a lightweight cross-platform wrapper around a webview component that allows to display HTML content in its own native GUI window.” It uses WebKit on OSX and Linux and Trident (MSHTML) on Windows, which is actually what wxPython’s webview widget also does. The idea behind pywebview is that it provides you the ability to load a website in a desktop application, kind of Electron.
While pywebview claims it “has no dependencies on an external GUI framework”, on Windows it requires pythonnet, PyWin32 and comtypes installed. OSX requires “pyobjc”, although that is included with the default Python installed in OSX. For Linux, it’s a bit more complicated. On GTK3 based systems you will need PyGObject whereas on Debian based systems, you’ll need to install PyGObject + gir1.2-webkit-3.0. Finally, you can also use PyQt 4 or 5.
You can use Python micro-web frameworks, such as Flask or bottle, with pywebview to create cool applications using HTML5 instead of Python.
To install pywebview itself, just use pip:
pip install pywebview
Once installed and assuming you also have the prerequisites, you can do something like this:
import webview webview.create_window('My Web App', 'http://www.mousevspython.com') webview.start()
This will load the specified URL in a window with the specified title (i.e. the first argument). Your new application should end up looking something like this:
The API for pywebview is quite short and sweet and can be found here:
There are only a handful of methods that you can use, which makes them easy to remember. But since you can’t create any other controls for your pywebview application, you will need to do all your user interface logic in your web application.
The pywebview package supports being frozen using PyInstaller for Windows and py2app for OSX. It also works with virtualenv, although there are known issues that you will want to read about before using virtualenv.
The pywebview package is actually pretty neat and I personally think it’s worth a look. If you want something that’s a bit more integrated to your desktop, then you might want to give wxPython or PyQt a try. But if all you need to do is distribute an HTML5-based web app, then this package might be just the one for you.
2 thoughts on “Getting Started with pywebview”
How about CEF Python? It has no dependencies and embeds latest Chromium on all platforms. Although it is not lightweight.. https://github.com/cztomczak/cefpython#cef-python
I was actually thinking about checking CEF Python out and doing a separate article on it since I’m not very familiar with it at the moment.
Comments are closed.