PyScript PWA

Creating installable PyScript applications that cache assets and runs offline offer enormous potential for Python. In this article, I will show you how to create a Python application that installs on the desktop and on mobile devices. This application will continue to function with no Internet connection. The PyScript and Pyodide files are cached by the application significantly reducing startup time. In my opinion, this will be a killer feature that will allow Python to be a major web development language.

AI, ML, and Data Science often require distributed serverless technologies and/or large backend servers to support large customer bases. By moving the CPU processing to the user’s browser, many new abilities will be created. PyScript will be able to access the user’s device (with permission of course), such as the camera, speaker, and microphone, as input devices. Image recognition, speech transcribing and translation, and many more features can be performed on the device without requiring an Internet connection.

Some will argue that JavaScript offers those features today. JavaScript offers none of those features. Those features are provided by the browser framework. Python can interface with those features just as easily as JavaScript. I am having a very difficult time finding something JavaScript does that I cannot easily do in Python. That includes integrating with JavaScript libraries. On the other hand, there are numerous Python libraries that are years ahead of JavaScript. The argument that JavaScript is king is only valid for those who know nothing but JavaScript. If JavaScript was the king, we would be writing all applications in JavaScript today.

Application Appearance

This is a screenshot of the installed application. Notice how it appears just like a normal application.

Demo Link

I created a domain and copied this application here.

GitHub Repository

The source code for this application is available here.

Important Concepts

The following items must be implemented to create an installable application that runs offline:

  • HTML page
  • Icons
  • manifest.json
  • Installation script
  • Service Worker

That is the entire list. No complicated code, no extensive integration, nothing more is required.

HTML Page

This can be anything you want that is supported by HTML and PyScript. For this application, I will use the Mathplot Example for the PyScript GitHub repository [link].

Three changes are required:

  • Specify that application manifest file: manifest.json
  • Specify a button so the user can install the application
  • Include a JavaScript file to declare the Service Worker and manage the install event

Required Changes:

  • Insert the manifest specification in the HTML head section.
  • Insert the button at the top of the body section.
  • Insert the script tag at the bottom of the HTML body section.

Complete index.html:

ICONS

You will need some icons to display for the application. The specification is not clear, but you need at least one icon. My example includes several that I had already created. The icons are located in the img directory. Use those icons until you create your own.

More details on the Web Application Manifest: [link].

I deliberately left out the manifest.json iOS icons so that this becomes a task for the next article. I wanted this article to be as simple as possible and still work on the desktop.

MANIFEST.JSON

The manifest specifies the characteristics of your application:

  • Application name
  • Starting page URL
  • Display type (fullscreen, browser, standalone, minimal-ui)
  • Orientation
  • Colors
  • Icons

This link has a good description of the various options.

Complete manifest.json:

Installation Script

This is located in js/main.js. This file processes three events:

  • DOM load – Once the document body is loaded inform the browser to register the service worker
  • Before Install Prompt (beforeinstallprompt)- This event is called if the user has not already installed the application. The code enables the hidden install button.
  • Install event – This event is called if the user clicks the install button. A browser confirmation dialog is displayed. The actual application installation and setup are handled entirely by the browser

Complete js/main.js:

Service Worker

The services worker plus the manifest.json are the magic that makes everything possible.

The service worker does several important things:

  • Defines the assets to be cached. I included only a partial list because I also included fetch event caching so that the PyScript and Pyodide assets are also cached.
  • Listens for the install event. Once fired, create the caches and pre-fetches the listed assets.
  • Listen for the activate event. Nothing performed here.
  • Listen for the Fetch API event. This code hooks the Fetch API and caches requests and responses. This is the code that makes offline access possible.

Complete sw.js:

Application Analysis

Google provides an excellent tool to analyze an application for offline access. This is called Lighthouse. My example passes the Lighthouse tests, but just barely. There are a number of improvements that can be made. I am planning a second part for this article that takes a Lighthouse report and then solves or improves upon each issue.

Summary

In this article, I demonstrated example code so that you can make any PyScript application installable. By listening for the Fetch API event, you can cache the PyScript and Pyodide files so that your application does not need to download them every time the application starts, which improves start time, but also allows for the application to run without the Internet.

More Information

Photography Credit

I write free articles about technology. Recently, I learned about Pexels.com which provides free images. The image in this article is courtesy of Magda Ehlers at Pexels.