Convert Dash to executable file (.exe)

Hi there,
I have created a Dash app and I need to let others use it as I can only run on my localhost. The problem is that I dont want to let them see my code the way that I think to convert my Dash into executable file instead of running on command prompt so I can give them the .exe file and they can start using my Dashboard on their computer after installing python.
Any ideia?
Thanks

I made a gist last week to cx_Freeze a dash app served with waitress. It can build .msi or .exe look at the doc of cx_Freeze for more info.

2 Likes

Hi @Philippe, thanks for prompt reply but I am not undestanding where to start with your code.
Please, explain step-by-step.
Thanks

  • Copy server.py and setup.py to your project directory, assuming your dash app lies in app.py.
  • Add the requirements that are not in your requirements.txt
  • pip install -r requirements.txt
  • Change to setup.py.
    • setup(name=’’) -> put the name of your project, change version
    • executables targetName -> change to desired exe name.
  • python setup bdist_msi or python setup bdist_exe, I recommend msi.’
  • Install the msi on the desired computer.
  • Open the exe in a terminal: c:\program_files\dash_app> dash_app.exe
  • Open browser to localhost:8000, (you can change the port in server.py serve parameters.)
1 Like

Thanks @Philippe, just to confirm if I undestood, see my code below if i am right.

  • Add the requirements that are not in your requirements.txt
  • pip install -r cx_Freeze
  • pip install -r cx_Logging
  • pip install -r waitress

Change to setup.py.

  • setup(name=’MyDash’)
  • executables targetName=‘MyDash.exe’.

python setup bdist_msi

  • Install the msi on the desired computer. => Was the last step I think.

  • Open the exe in a terminal: c:\program_files\dash_app> MyDash.exe Will there be available a shortcut icon on my computer to allow double click instead of going to terminal???

  • Open browser to localhost:8000, (you can change the port in server.py serve parameters.)

Am I right on those steps?

Yes, it should work like that. For the shortcut icon, you can either create one manually or look in the cx_Freeze docs if there’s an option for that.

You can set the base of the executable to be Win32Gui instead of console and it will tell the messages in a gui instead. I found it cumbersome so I just did console instead.

Also be sure that plotly>=3.3.0 since it contains a fix for cx_Freeze exe generation.

2 Likes

Thanks, will let you know of my results after testing.

@Philippe
pip install -r cx_Freeze
Could not open requirements file: [Errno 2] No such file or directory: ‘cx_Freez
e’
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the ‘python -m pip install --upgrade pip’ comm
and.

Changed pip install -r cx_Freeze to conda install cx_Freeze and got this:
Solving environment: failed

PackagesNotFoundError: The following packages ar
nnels:

  • cx_freeze

Current channels:

To search for alternate channels that may provid
looking for, navigate to

https://anaconda.org

and use the search bar at the top of the page.

You need to remove the -r: pip install cx_Freeze. The -r argument is for reading from a file.

python setup bdist_msi
python: can’t open file ‘setup’: [Errno 2] No such file or directory

python setup.py bdist_msi, my mistake sorry.

Wow, thanks for prompt reply. :slight_smile:

By the way, I dont see any link of server.py and setup.py with my app named study.py. How to link them? I am afraid that MyDash.exe wount run.

You need to import your app in server.py, if your app file is named study.py, you replace:

from app import server
to

# import the dash app
from study import app

# set the server variable to app.server (the flask instance of dash)
server = app.server

Understood but my app has these codes:

app = dash.Dash()

server = Flask(‘my app’)

if name == ‘main’:
app.server.run(debug=True)

What I need to change there inside my code? My Dash connects through an API Server and decodes dataset and save it to my computer and only after that I read .csv and do my stuffs.

And to change localhost port I change this line of study.py to this and hope I am correct:
if name == ‘main’:
app.server.run(
debug=True,
host=‘0.0.0.0’, port=8049)

Thanks a lot.

You should instantiate the app like this:

app = dash.Dash(__name__)
server = app.server

Then in server.py you import it:

from study import server

To change the port of the .exe you need to add port=8049 to serve in server.py

serve(server, port=8049)

C:\Users\H\Desktop\PANDAS\build\exe.win-amd64-3.6>MyDash.exe
Traceback (most recent call last):
File “C:\Users\H\Miniconda3\lib\site-packages\cx_Freeze\initscripts_startup
.py", line 14, in run
module.run()
File "C:\Users\H\Miniconda3\lib\site-packages\cx_Freeze\initscripts\Console.py
", line 26, in run
exec(code, m.dict)
File “server.py”, line 3, in
File “C:\Users\H\Desktop\PANDAS\study.py”, line 5, in
import dash
File "C:\Users\H\Miniconda3\lib\site-packages\dash_init
.py”, line 1, in
from .dash import Dash # noqa: F401
File “C:\Users\H\Miniconda3\lib\site-packages\dash\dash.py”, line 8, in
import plotly
File “C:\Users\H\Miniconda3\lib\site-packages\plotly_init_.py”, line 31, in

from plotly import (plotly, dashboard_objs, graph_objs, grid_objs, tools,
File “C:\Users\H\Miniconda3\lib\site-packages\plotly\plotly_init_.py”, line
10, in
from . plotly import (
File “C:\Users\H\Miniconda3\lib\site-packages\plotly\plotly\plotly.py”, line 2
8, in
from requests.compat import json as json
File "C:\Users\H\Miniconda3\lib\site-packages\requests_init
.py", line 98,
in
from . import packages
File “C:\Users\H\Miniconda3\lib\site-packages\requests\packages.py”, line 7, i
n
locals()[package] = import(package)
File “C:\Users\H\Miniconda3\lib\site-packages\idna_init_.py”, line 2, in
from .core import *
File “C:\Users\H\Miniconda3\lib\site-packages\idna\core.py”, line 1, in
from . import idnadata
ImportError: cannot import name ‘idnadata’

1 Like

Sometimes cx_Freeze has difficulties importing subpackages.

Run first:
pip install idna --upgrade

Change your setup.py options

options = {
    'build_exe': {
        'includes': [
            'cx_Logging', 'idna', 'idna.idnadata'
        ],
        'packages': [
            'asyncio', 'flask', 'jinja2', 'dash', 'plotly', 'waitress'
        ],
        'excludes': ['tkinter']
    }
}

C:\Users\H\Desktop\PANDAS\build\exe.win-amd64-3.6>MyDash
Traceback (most recent call last):
File “C:\Users\H\Miniconda3\lib\site-packages\cx_Freeze\initscripts_startup
.py", line 14, in run
module.run()
File "C:\Users\H\Miniconda3\lib\site-packages\cx_Freeze\initscripts\Console.py
", line 26, in run
exec(code, m.dict)
File “server.py”, line 3, in
File “C:\Users\H\Desktop\PANDAS\study.py”, line 5, in
import dash
File "C:\Users\H\Miniconda3\lib\site-packages\dash_init
.py”, line 1, in
from .dash import Dash # noqa: F401
File “C:\Users\H\Miniconda3\lib\site-packages\dash\dash.py”, line 8, in
import plotly
File “C:\Users\H\Miniconda3\lib\site-packages\plotly_init_.py”, line 31, in

from plotly import (plotly, dashboard_objs, graph_objs, grid_objs, tools,
File “C:\Users\H\Miniconda3\lib\site-packages\plotly\graph_objs_init_.py”,
line 14, in
from plotly.graph_objs.graph_objs import * # this is protected with all

File “C:\Users\H\Miniconda3\lib\site-packages\plotly\graph_objs\graph_objs.py”
, line 34, in
from plotly import exceptions, graph_reference
File “C:\Users\H\Miniconda3\lib\site-packages\plotly\graph_reference.py”, line
9, in
from pkg_resources import resource_string
File “C:\Users\H\Miniconda3\lib\site-packages\pkg_resources_init_.py”, line
73, in
from pkg_resources.extern import appdirs
File "C:\Users\H\Miniconda3\lib\site-packages\pkg_resources\extern_init_.py
", line 61, in load_module
“distribution.”.format(**locals())
ImportError: The ‘appdirs’ package is required; normally this is bundled with th
is package so if you get this warning, consult the packager of your distribution