Serve_locally option with additional scripts and style sheets

@HansG - For now, you can insert CSS with a html.Link component inside app.layout. This will bypass the unflexible append_css method. Here’s an example:

import dash
import dash_html_components as html
import os

from flask import send_from_directory

app = dash.Dash()
app.css.config.serve_locally = True
app.scripts.config.serve_locally = True

app.layout = html.Div([

    html.Link(
        rel='stylesheet',
        href='/static/stylesheet.css'
    ),

    html.Div('Hello world')

])


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


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

and here’s the folder structure:

-- app.py
-- static/
-- static/stylesheet.css

(Note that this solution was originally shared in this topic: How do I use Dash to add local css?)

1 Like