Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the favicon in Flask/Dash

Trying to get the favicon to load I have followed suggestions from the internet:

server = Flask(__name__, static_folder='static')
app =  dash.Dash(external_stylesheets=external_stylesheets, server=server)

app.css.config.serve_locally = False
app.scripts.config.serve_locally = True

@server.route('/favicon.ico')
def favicon():
    print('Server root path', server.root_path)
    return send_from_directory(os.path.join(server.root_path, 'static'),
                               'dice.ico', mimetype='image/vnd.microsoft.icon')

   ...
   app.run_server(debug=True)

If I browse to the favicon, I see it:

http://www.example.com/favicon.ico

However, when I browse to

http://www.example.com

I see the dash default icon with it's own description. How do I ensure my ownfavicon loads correctly?

like image 945
Ivan Avatar asked Feb 02 '20 04:02

Ivan


2 Answers

To simply change the favicon all you need to do is to create a folder called assets next to your app.py and place your favicon.ico inside of that folder and it will work perfectly.

app.py:

import flask
import dash
import dash_html_components as html

server = flask.Flask(__name__)

@server.route('/')
def index():
    return 'Hello Flask app'

app = dash.Dash(
    __name__,
    server=server,
    routes_pathname_prefix='/dash/'
)

app.layout = html.Div("My Dash app")

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

Here is the docs link for more information: Dash docs

like image 168
Wlne Avatar answered Sep 18 '22 07:09

Wlne


An alternative used in Dash is:

app = dash.Dash()
app._favicon = ("path_to_folder/(your_icon).co")
like image 30
Wellingon Avatar answered Sep 22 '22 07:09

Wellingon