pythonpyinstallerpywebview

Pywebview - How can one freeze pywebview applications into a smaller executable?


My Goal:

Bundle a simple pywebview application as a standalone executable (one file) on Linux (specifically Arch Linux). The binary executable size must be no greater than 30MB. Code is below.

The Problem:

When trying to compile the below application (which is named foobar.py), and when using the below command with pyinstaller:

# install my application's dependencies
pip install pywebview

# install pyinstaller
pip install pyinstaller

# install pywebview dependencies
pip install PyGObject
pyinstaller --exclude-module PyQt5 --exclude-module sympy --exclude-module numpy foobar.py

The resulting executable size is 3.1 GB.

Current non-working solution

Use a python virtual environment and run the same commands as above. There is no noticeable decrease in executable size.

Current Source Code:

foobar.py:

import webview

html = """

<!DOCTYPE html>
<html>
  <head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
      body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI",
          "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
      }
      div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0, 0, 0, 0.02);
      }
      a:link,
      a:visited {
        color: #38488f;
        text-decoration: none;
      }
      @media (max-width: 700px) {
        div {
          margin: 0 auto;
          width: auto;
        }
      }
    </style>
  </head>

  <body>
    <div>
      <h1>Hello World</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
        <p>
        <a href="https://example.org">More information...</a>
      </p>
    </div>
  </body>
</html>
"""

webview.create_window('Hello world', html=html)
webview.start()

Solution

  • Here's my solution: