Over a million developers have joined DZone.

Getting Started with pywebview

DZone's Guide to

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.

· Web Dev Zone ·
Free Resource

Have you seen our HERE Twitch channel to livestream our Developer Waypoints series?

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 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:

Image title

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.

Developer Waypoints is a live coding series from HERE, which will teach you how to build with maps and location data.

webview ,widget ,python ,web dev ,web app

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}