Skip to content

Instantly share code, notes, and snippets.

@peteristhegreat
Created January 3, 2020 21:12
Show Gist options
  • Save peteristhegreat/ce5f89ae314086ff75211d259d881c88 to your computer and use it in GitHub Desktop.
Save peteristhegreat/ce5f89ae314086ff75211d259d881c88 to your computer and use it in GitHub Desktop.
PyInstaller, Plotly, Dash, external css, how to include local css files

In order for a portable version (i.e. a pyinstaller built version) of a Dash app to find the assets at runtime, you need to host it with flash and list them in the external_stylesheets parameter of the constructor.

The handwaving approach of just put it in the assets folder and Dash will find it, only works if you are shipping the entire python environment and the module can be found at runtime.

Micromanaging the assets is the only supported way as of Jan 2020 with Dash v0.43.0 .

def local_css_dist():
    links = []
    # Consider finding these files at runtime instead of hardcoding it
    css_files = ['0loading.css',
                 '1normalize.min.css',
                 '2skeleton.min.css',
                 '3css.css',
                 '4old-report-adj.css']
    for css_file in css_files:
        #links.append(html.Link(href='/assets/'+css_file, rel='stylesheet'))
        links.append('/assets/'+css_file)
    return links

app = dash.Dash(__name__, server=server, assets_folder='assets', external_stylesheets=local_css_dist()) #a

cached_cwd = os.getcwd()
@app.server.route('/assets/<path:path>')
def static_file(path):
    static_folder = os.path.join(cached_cwd, 'assets')
    return send_from_directory(static_folder, path)

Also you need to copy the files into the cwd at build time, like is described here: https://stackoverflow.com/a/59415662/999943

And be sure to included the data file directories into the normal pyinstaller paths, also demoed in the link above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment