Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a favicon to a Flask server without HTML

My flask server constantly reports

xx.xxx.xxx.xxx - - [DD/MM/YYYY HH:MM:SS] "GET /favicon.ico HTTP/1.1" 404 -

In the code for my flask server I've added,

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

and I've added a favicon titled favicon.ico to the same directory that my flask server is running from.

Favicon location

If I try to navigate to http://www.myurl.com/favicon.ico I get a 404. My flask server isn't serving an html landing page so I can't add <link rel='shortcut icon' href='favicon.ico' type='image/x-icon'/ > anywhere. I don't really care about actually having a favicon, I just want to stop the error from showing up. How can I serve a favicon/stop the error?

like image 223
Emilio Garcia Avatar asked Nov 19 '16 18:11

Emilio Garcia


People also ask

How do I add a favicon to my Flask website?

First, of course, you need an icon. It should be 16 × 16 pixels and in the ICO file format. This is not a requirement but a de-facto standard supported by all relevant browsers. Put the icon in your static directory as favicon.

How do I embed a favicon?

To add a favicon to your website, either save your favicon image to the root directory of your webserver, or create a folder in the root directory called images, and save your favicon image in this folder. A common name for a favicon image is "favicon.ico".


1 Answers

Put the icon in your static directory as favicon.ico. and below code in python file

import os
from flask import send_from_directory

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

href - http://flask.pocoo.org/docs/0.12/patterns/favicon/

like image 197
Raj Pandey Avatar answered Nov 05 '22 08:11

Raj Pandey