How to build cross platform Desktop GUI based apps with Python and web technologies and frameworks

Have ever wanted to use your Python and web development skills to build cross platform Desktop GUI apps ? if yes then welcome to this tutorial where i'll show you how to use PyWebView to turn your web application built using Python and client side technologies such as HTML ,CSS and JavaScript into a standalone cross platform GUI application that runs under major operating systems such as Linux ,Windows and MAC .

You can also use any web framework based on Python such as the so popular Django framework or also the lightweight web development framework Flask to build business logic of your app and then wrap the whole app ,server and client side ,into a Desktop app .

What is PyWebView ?


Before getting started with PyWebView lets first introduce it .PyWebView is an open source ,cross platform and lightweight wrapper which provides you with a WebKit/MSHTML based webview inside a native GUI window which allows you to render HTML and CSS and execute JavaScript code just like any normal web browser .It's in fact a headless web browser that you can use to run a unique and default web app so the user gets the feeling of having an ordinary GUI application .

PyWebView doens't reinvent the wheel but instead it uses the existing tools and native platform APIs.For providing a native window

for the target system it uses

Win32 API or Win Forms under Windows .

Cocoa on OS X .

GTK 3 ,QT4/5 under Linux .

Getting started with PyWebView


First of all ,make sure you have either Python 2 or 3 installed in your development machine .You also need PIP package manager installed then open up your terminal under Linux/MAC or command prompt under Windows and enter

    pip install pywebview

If your installation fails make sure you have the following dependencies installed depending on your target system and re-execute the install command again .

Under Windows you need to install pywin32, comtypes if you want to use Win32 subsystem to create natives windows or pythonnet if you want to be able to use Windows Forms for native windows .

Under OS X you need to install pyobjc if it's not already installed in your system .

Under Linux GTK3 based distributions you have to install PyGObject before you can use PyWebView .

For QT based systems you need to install PyQt4/PyQt5 .

How to use pywebview ?


You can easilly get started with pywebview .Just execute the following Python code to launch a native window that displays a visited web URL .

import webview

webview.create_window("Techiediaries", "http://www.techiediaries.com",width=800, height=600, resizable=True, fullscreen=False) 

So you first start by importing the webview .Then use the create_window method to create and show a native window hich renders the content of http://www.techiediaries.com with the title "Techiediaries" .

All parameters are self explanatory they just control the width ,hieght and other properties of the window .

You can see a full example of a Flask app wrapped into a GUI app using pywebview under the GitHub repository of pywebview

Conclusion

While there are many Python based frameworks for building Desktop apps which are cross platform since Python itself is a portable and cross platform environment ,you have to learn these frameworks before you can build your apps but with pywebview you can use your existing web development skills and familiar Python web frameworks to create the web apps you have the skills to build and then wrap them into GUI based apps without starting from scratch .

About the Author

Techiediaries is a website dedicated to bring you tutorials for the latest web technologies


comments powered by Disqus
DMCA.com Protection Status