Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:
webview ,widget ,python ,web dev ,web app

Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}