Getting Started with pywebview
Need to display some web content in your Python application? We take a look at the pywebview project to help you accomplish exactly that.
Join the DZone community and get the full member experience.
Join For Freei 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 the display of 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 with the ability to load a website in a desktop application, kind of like electron .
while pywebview claims it “has no dependencies on an external gui framework,” on windows it requires pythonnet, pywin32 and comtypes to be 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 this is 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')
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:
- https://github.com/r0x0r/pywebview#api
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.
wrapping up
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.
Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments