# How can I include assets of a dash app into an exe file created with pyinstaller --onefile?

**URL:** <https://community.plotly.com/t/how-can-i-include-assets-of-a-dash-app-into-an-exe-file-created-with-pyinstaller-onefile/22133>\
**Category:** Dash Python\
**Created:** [April 10, 2019, 6:13pm UTC](https://community.plotly.com/t/how-can-i-include-assets-of-a-dash-app-into-an-exe-file-created-with-pyinstaller-onefile/22133 "2019-04-10T18:13:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DataWiz](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DataWiz](https://community.plotly.com/u/DataWiz)\
**Post date:** [April 10, 2019, 6:13pm UTC](https://community.plotly.com/t/how-can-i-include-assets-of-a-dash-app-into-an-exe-file-created-with-pyinstaller-onefile/22133/1 "2019-04-10T18:13:56Z")

</div>

I have the same question posted on stackoverflow, but with no answer: [python - How can I include assets of a dash app into an exe file created with pyinstaller --onefile? - Stack Overflow](https://stackoverflow.com/questions/55596932/how-can-i-include-assets-of-a-dash-app-into-an-exe-file-created-with-pyinstaller)

Basically, it seems like dash is unable to access the asset folder in a temp directory. I have the following code to get the temp directory:

```
# get temp working directory
import sys
import os
def resource_path(relative_path):

# get absolute path to resource
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

print(resource_path("assets"))

```

This gives me a the path to the current working directory/assets. It works in an anaconda environment as well when bundling the app into an .exe file, showing me the path where the assets are in the console.

But the next part is where I run into trouble:

```
# create app
app = dash.Dash( __name__ , assets_url_path=resource_path("assets"))

```

For some reason, this only works in the Anaconda virtual environment, but not when running the code from .exe. The dash app opens just fine, but the css is not loaded from .exe, despite `print(resource_path("assets"))` showing the temporary path to which I can navigate and see the css file…

What am I missing?

---

<div class="post-metadata">

**Author:** ![abeljads](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@abeljads](https://community.plotly.com/u/abeljads)\
**Post date:** [November 25, 2020, 12:53pm UTC](https://community.plotly.com/t/how-can-i-include-assets-of-a-dash-app-into-an-exe-file-created-with-pyinstaller-onefile/22133/2 "2020-11-25T12:53:17Z")

</div>

@DataWiz, have you managed to solve this problem? In addition to an assets folder, I have a data folder as well. I still could not manage to successfully create an .exe file. Any input is highly apperciated.

---

<div class="post-metadata">

**Author:** ![DataWiz](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DataWiz](https://community.plotly.com/u/DataWiz)\
**Post date:** [November 25, 2020, 1:16pm UTC](https://community.plotly.com/t/how-can-i-include-assets-of-a-dash-app-into-an-exe-file-created-with-pyinstaller-onefile/22133/3 "2020-11-25T13:16:57Z")

</div>

Sorry, I have since abandoned the idea of pyinstaller and dash and moved to a web app. I’d be curious to know if it has become any easier though.

---

<div class="post-metadata">

**Author:** ![zoeyrose](https://avatars.discourse-cdn.com/v4/letter/z/53a042/32.png) [@zoeyrose](https://community.plotly.com/u/zoeyrose)\
**Post date:** [December 9, 2020, 5:39pm UTC](https://community.plotly.com/t/how-can-i-include-assets-of-a-dash-app-into-an-exe-file-created-with-pyinstaller-onefile/22133/4 "2020-12-09T17:39:58Z")

</div>

@abeljads did you find a solution? I can package my app without issue, but my css file doesn’t get recognized so clearly packaging the assets folder isn’t working.

---

<div class="post-metadata">

**Author:** ![abeljads](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@abeljads](https://community.plotly.com/u/abeljads)\
**Post date:** [December 10, 2020, 9:02am UTC](https://community.plotly.com/t/how-can-i-include-assets-of-a-dash-app-into-an-exe-file-created-with-pyinstaller-onefile/22133/5 "2020-12-10T09:02:11Z")

</div>

I have not got any solution yet. Can you share your code that is working for you without including the asset folder? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![zoeyrose](https://avatars.discourse-cdn.com/v4/letter/z/53a042/32.png) [@zoeyrose](https://community.plotly.com/u/zoeyrose)\
**Post date:** [December 10, 2020, 6:53pm UTC](https://community.plotly.com/t/how-can-i-include-assets-of-a-dash-app-into-an-exe-file-created-with-pyinstaller-onefile/22133/6 "2020-12-10T18:53:54Z")

</div>

@abeljads sure! Note that I’m using a windows machine. For Linux I think you just need to replace semi-colons with colons.

I actually did ultimately get it to work! I think the second `--add-data` option might not be necessary, but its working for me now and I’m not going to change it 🙂

```auto
pyinstaller --onefile --hidden-import pyodbc --add-data="assets;assets" --add-data "assets\base.css;assets" --additional-hooks-dir=hooks app.py

```

You might have to make some changes, like the hidden import command line option.

Importantly, I have a folder in the same directory as the app.py file called “hooks”. In it is a handful of individual .py files. They all follow the same structure, though, and just change the package they’re supporting. I’ll give an example with dash bootstrap components. The name of the file is `hook-dash_boostrap_components.py`and the code in the file is below.

```auto
# hooks/hook-dash_boostrap_components.py

from PyInstaller.utils.hooks import collect_all

datas, binaries, hiddenimports = collect_all('dash_bootstrap_components')

```

For my application I had to create these files for dash bootstrap components, dash core components, dash html components, dash renderer, dash table, and flask compress. I determined these were needed via trial and error.

---

<div class="post-metadata">

**Author:** ![adionisio](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@adionisio](https://community.plotly.com/u/adionisio)\
**Post date:** [March 21, 2024, 5:24pm UTC](https://community.plotly.com/t/how-can-i-include-assets-of-a-dash-app-into-an-exe-file-created-with-pyinstaller-onefile/22133/7 "2024-03-21T17:24:17Z")

</div>

It’s been several years since this thread was last opened but I was unable to find a single resource that showed how to include javascript/css/etc. (basically anything in the assets folder) in the dash executable and have it actually work.

I was finally able to get it working and it’s a relatively simple workaround.

The first thing I did was follow this comment/thread on allowing dash to see the assets folder:

> [@Convert Dash to executable file (.exe)](https://community.plotly.com/t/convert-dash-to-executable-file-exe/14222/30):
>
> I have some updates! Running cx\_Freeze from a virtual environment works! It creates a much smaller folder/executable as it only brings the packages you installed in the virtualenv. HOWEVER, cx\_Freeze has a bug that prevents it to work with virtualenv out of the box with some packages. Fortunately, there is a workaround here: [https://stackoverflow.com/questions/14247130/cxfreeze-missing-distutils-module-inside-virtualenv](https://stackoverflow.com/questions/14247130/cxfreeze-missing-distutils-module-inside-virtualenv) your executable will not be able to access your local CSS/JS/Favicon t…

But instead of following this exactly (since it wasn’t working), I modified the call in dash.Dash() to look like this `app = dash.Dash( __name__ , assets_folder=find_data_file('_internal/assets/'), include_assets_files=True)`. The find\_data\_file function is included at the post above. The reason for the ‘\_internal’ inclusion was that I noticed once the app is packaged up as an executable when running `pyinstaller app.spec` all of the included modules (including the assets folder) were ending up in there instead of at the same level as the executable. Since, presumably, `assets` needs to be accessible at the same level as the executable, I figured I should just point the app to that location beforehand. This also necessitates your assets folder being placed into a directory called ‘\_internal’ before you package everything up. You can test out the app to make sure it still has access to those files when running via Python on command line.

The app.spec file also needs to include the ‘\_internal’ in the path to the assets folder. So your ‘datas’ variable should look like this:

```auto
datas=[('_internal/assets/javascript_stuff.js','assets'), 
             ...,
             ...],

```

Once I did that, the javascript was accessible by the executable and worked just as expected.

I wanted to share on the off chance someone else needed a workaround for this still. If anyone has any better solutions, I’m still open to them. Nothing seems to have been negatively affected otherwise.
