Dash is an open-source framework for building data visualization web applications using Python, without requiring any frontend development experience. It is commonly used by data scientists and engineers to create interactive dashboards. In this tutorial, we’ll cover different approaches to converting a Dash application into a standalone executable (.exe) file that can be run on Windows, Linux, and Mac platforms without needing a Python installation.

The main motivation behind this is to package the Dash application along with its dependencies into a single file that users can run by double-clicking, even if they don’t have Python or Dash installed.

Let’s first create a simple Dash application. Create a file dash_app.py with the below code:

import dash
from dash import dcc, html
from dash.dependencies import Input, Output

app = dash.Dash(__name__)

app.layout = html.Div([
    dcc.Input(id='input-box', type='text'),
    html.Button('Submit', id='button'),
    html.Div(id='output-div')
])

@app.callback(
    Output('output-div', 'children'),
    [Input('button', 'n_clicks')],
    [dash.dependencies.State('input-box', 'value')]
)
def update_output(n_clicks, value):
    return f'You entered: {value}'

if __name__ == '__main__':
    app.run_server(debug=True)

This application can be run from the terminal using the below command:

python dash_app.py

Let’s explore three different methods to turn your Dash application to an executable:

PyInstaller

PyInstaller is a Python package that bundles a Python application and all its dependencies into a single package. The user can run the packaged app without installing a Python interpreter or any modules. Unlike Nativefier, PyInstaller packages the actual Python code directly into an executable, so no external hosting is needed. This method works entirely offline.

Let’s see how you can bundle a Dash application using PyInstaller.

Install nativefier by running the below command:

pip install nativefier

Now bundle the Dash app with its dependencies using PyInstaller:

pyinstaller --onefile --additional-hooks-dir=./hooks dash_app.py --clean

This will generate build and dist folders and a dash_app.spec file. Edit the dash_app.spec file to ensure paths and dependencies are set properly as below:

a = Analysis(
    ["dash_app.py"],
    pathex=["."],
    binaries=[],
    datas=datas,
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(...)
exe = EXE(...)
coll = COLLECT(...)

Once this command runs, the exe can be found in the path dist/dash_app.exe.

Execute the PyInstaller command one more time to incorporate the above changes:

pyinstaller dash_app.spec --clean

Package versions

Dash 2.18.1

PyInstaller 6.10.0

cx_Freeze

cx_Freeze is a Python module that allows you to convert Python scripts into standalone executables, bundling all necessary dependencies and the Python interpreter. It works across platforms (Windows, macOS, and Linux), making it also ideal for turning your Dash app into a single executable file.

Let’s dive into how you can use cx_Freeze to turn your Dash application to an executable file!

Install cx_Freeze by running the below command:

pip install cx_Freeze

In the dash_app.py, add the following line:

server = app.server

Next, create a server script server.py:

from waitress import serve
from dash_app import server

serve(server, host='0.0.0.0', port=8080)

Then you need to create a setup script setup.py:

from setuptools import find_packages
from cx_Freeze import setup, Executable

# Define build options
options = {
    'build_exe': {
        'includes': [
            # Include any libraries you need
        ],
        'packages': [
            'dash', 'flask', 'waitress'  # Include necessary libraries for Dash
        ],
        'excludes': ['tkinter'],  # Exclude tkinter if not needed
        'include_files': [],  # Add static files or images here if necessary
    }
}

# Define executable details
executables = [
    Executable('server.py',  # The entry point of your Dash app
               base='Console',  # Use 'Console' for cross-platform compatibility
               target_name='dash_app.exe')  # Name of the output executable
]

# Setup configuration
setup(
    name='dash_app',
    version='0.0.1',
    description='Dash App',
    packages=find_packages(),
    executables=executables,
    options=options
)

Also create a text file requirements.txt to include all necessary packages and dependencies:

dash
cx_Freeze
waitress

Install all necessary packages and dependencies by running the following command:

pip install -r requirements.txt

Now you can build your Dash app as below:

python setup.py build

Package versions

Dash 2.18.1

cx_Freeze 7.2.2

Nuitka

Nuitka is a Python-to-C compiler that transforms Python code into optimized C code, which can then be compiled into standalone executables. Unlike PyInstaller or cx_Freeze, Nuitka actually compiles Python code into machine-level code, which can improve performance.

Let’s see how you can bundle a Dash application using Nuitka.

Install nuitka by running the below command:

pip install nuitka

If you’re using Windows and don’t have a compiler installed, you’ll need to install one. You can use the Microsoft Visual C++ Build Tools, or install MinGW.

Alternatively, you can run the following command:

nuitka --version

It will display the current version of Nuitka installed and detect if you need to install a compiler.

Now bundle the Dash app with its dependencies using Nuitka:

nuitka --onefile --standalone --follow-imports --assume-yes-for-download dash_app.py

Once the compilation is complete, you’ll find the executable dash_app.exe inside the dist folder.

Package versions

Dash 2.18.1

Nuitka 2.4.8

Nativefier

Let’s now explore a framework for converting an application to a desktop application: Nativefier. This package is based on Electron, which is a platform that easily enables writing cross-platform desktop applications using JavaScript, HTML, and CSS.

Nativefier is a command-line tool that converts any web application into a desktop application. It uses Electron to package the web app into an executable format for Windows, macOS, and Linux.

However, to use Nativefier for a Dash app, you first need to host the Dash application on a web server like Ploomber Cloud.

Deploy your application on a hosting platform and copy the URL where your app is hosted.

Install nativefier by running the below command:

npm install -g nativefier

Now convert your Dash app to an executable:

nativefier --name '<app.exe name>' '<app_url>' --platform <'windows' or 'mac' or 'linux'>

After the command is executed, an executable file will be created in the current directory!

Package versions

Dash 2.18.1

Nativefier 50.0.1

Limitations

Each method above comes with its own limitation:

PyInstaller, cx_Freeze, and Nuitka works properly offline, but they require different levels of manually editing python scripts for the Dash app.

Nativefier requires us to deploy our Dash application as a web application first.

In conclusion, converting a Dash app into an executable offers several advantages, including ease of distribution, offline functionality, and an enhanced user experience. Whether for internal business use, fieldwork, or general applications, packaging your app as an executable provides a seamless, portable, and efficient solution for users, without requiring them to install Python or manage dependencies.