Ivorforce / Dash-Standalone-Boilerplate

A boilerplate repository for standalone dash apps, using nuitka.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dash Standalone Boilerplate

This package is a template for Dash (and generally Flask) based Python web programs that want to be a standalone desktop app rather than a local website.

screenshot

Setup

First, install poetry. Then:

# set up project
poetry install
# run the project
poetry run python src/dnb/main.py

How does it work?

First, a Flask server is opened through a local port (8050). This server can be accessed through any local web browser. Dash is attached to the Flask server.

To make it a standalone app, a web view (through pywebview) is opened to visit the address. In contrast to something like Electron, the web view is completely native and thus not included in the final app bundle. This makes the app leaner and less RAM intensive.

Finally, the app is bundled into an executable using Nuitka. Nuitka compiles the whole python interpreter, including required packages, into an app that can be run without an existing python installation.

These tricks combine to make an app that behaves just as one would expect from a normal app, avoiding the myriad issues that are normally associated with shipping a python web app.

Building and shipping the app

It is possible to build the app for the prevailing operating system using the following code:

# You need to install with nuitka
poetry install -E nuitka
# compile binary
poetry run python -m nuitka --output-dir=build --onefile --macos-create-app-bundle --include-package-data=dash --include-package-data=dash_core_components --include-package-data=dash_html_components --include-package-data=packaging --include-package=plotly --include-package-data=plotly --include-package-data=dnb src/dnb/main.py
# run binary
./main.bin

This repository also comes with a github action that builds it for macOS, Linux, and Windows. This action takes about 2 hours to execute, using about 360 of your 2000 free minutes offered by GitHub - so use it sparingly. To trigger it, tag a commit as release:

git tag release.0.1
git push --tags

You will be able to download the artifacts of the build from the workflow page. I recommend publishing them locally on GitHub with the Releases feature. Note that the macOS and Linux builds are not executable by default. Before uploading, make sure you chmod +x path/to/executable and re-zip the application.

Alternatives

There are a few alternatives to using Nuitka to create binaries:

  • Some (like PyApp, pyinstaller and PyOxidizer), bundle an automated setup script. Actual python code is run on target machines, which reduces build times and may avoid some issues while introducing others. It will also require the target machine to set up itself.
  • Cython has an embedding option that allows it to create executables. Cython can gain considerable speed over Nuitka, but may be more work to get running.

In the Wild

See how I'm using this template with a more advanced project in ECG Viewer.

About

A boilerplate repository for standalone dash apps, using nuitka.

License:Creative Commons Zero v1.0 Universal


Languages

Language:Python 100.0%