我正在尝试按照此处给出的说明进行操作:Adding a favicon to a Flask server without HTML 关于如何将网站图标添加到 Flask 应用程序,但它对我不起作用。这是我的申请文件:
def first_call(*args, **kwargs) -> int:
"""Call this first."""
# you can also access to the params with args array such as args[0]
return args[0]
def second_call(*args, **kwargs) -> int:
"""Call this second."""
return 2
def router_function(*args, **kwargs) -> int:
"""Call the first function, then with this same function object, call the second."""
call = kwargs.get("call")
if call == 1:
return first_call(*args, **kwargs)
elif call == 2:
return second_call(*args, **kwargs)
return 0
# Desired behavior
first_call_ret = router_function(100, call=1) # Calls first_call
second_call_ret = router_function(200, call=2) # Calls second_call
print(first_call_ret) # OUTPUT: 100 because we returned the first argument at first_call
print(second_call_ret) # OUTPUT: 2 since we returned 2 from second_call (hardcoded)
这是我的目录结构:
from flask import Flask,send_from_directory
application=Flask(__name__)
@application.route('/')
def main():
return '<html><p>hello world</p></html>'
@application.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(application.root_path, 'static'),
'favicon.ico',mimetype='image/vnd.microsoft.icon')
if __name__=='__main__': application.run(debug = True)
当我在 Firefox 中运行该应用程序时,没有显示网站图标,而当我在 Chrome 中运行它时,会显示默认的网站图标。我使用此网站将 png 转换为 ico 文件: https://www.freeconvert.com/png-to-ico
请告诉我哪里出错了。
当我在 chrome 中运行应用程序时,我在控制台中收到此错误:
➜ demo ls -R
application.py static
./static:
favicon.ico
答案 0 :(得分:0)
我需要导入 os
,这是工作应用程序:
from flask import Flask,send_from_directory
import os
application=Flask(__name__)
@application.route('/')
def main():
return '<html><p>hello world</p></html>'
@application.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(application.root_path, 'static'),
'favicon.ico',mimetype='image/vnd.microsoft.icon')
if __name__=='__main__': application.run(debug = True)