【问题标题】:route to directory in flask路由到烧瓶中的目录
【发布时间】:2022-01-23 23:11:32
【问题描述】:

我需要从烧瓶中的文件夹中公开一些图标,如下所示:

PROJECT NAME
>> static
  >> assets
      >> icon-16.png
      >> icon-32.png
      >> icon-64.png
      >> icon-80.png
      >> icon-120.png
      >> logo-filled.png
>> templates
  >> commands.html
  >> index.html
  >> taskpane.html
>> app.py

我需要使assets 可路由,这样我就可以从这样的网址访问 png 文件: https://pythonlinuxtest.azurewebsites.net/assets/icon-16.png https://pythonlinuxtest.azurewebsites.net/assets/icon-32.png https://pythonlinuxtest.azurewebsites.net/assets/icon-64.png

这是我目前的app.py 中的内容:

from flask import Flask
from flask import render_template

app = Flask(__name__)

# @app.route("/")
# def hello():
#     return "Hello, World!"

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/taskpane.html")
def taskpane():
    return render_template("taskpane.html")

@app.route("/commands.html")
def commands():
    return render_template("commands.html")

我不确定如何将 assets 目录添加到 app.py 以便可以访问 png 文件。

【问题讨论】:

标签: python flask directory


【解决方案1】:
from flask import Flask
from flask import render_template
from flask.helpers import send_file

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/taskpane.html")
def taskpane():
    return render_template("taskpane.html")

@app.route("/commands.html")
def commands():
    return render_template("commands.html")

@app.route("/assets/<file_name>")
def get_image(file_name):
    return send_file(f"./static/assets/{file_name}",mimetype='image/png')

你可以试试这个

【讨论】:

    【解决方案2】:

    我想通了!!但如果有人知道更好的方法,请告诉我。

    我使用send_file将图片添加到app.py

    现在是整个 app.py :)

    from flask import Flask
    from flask import render_template
    from flask.helpers import send_file
    
    app = Flask(__name__)
    
    @app.route("/")
    def index():
        return render_template("index.html")
    
    @app.route("/taskpane.html")
    def taskpane():
        return render_template("taskpane.html")
    
    @app.route("/commands.html")
    def commands():
        return render_template("commands.html")
    
    @app.route("/assets/icon-16.png")
    def icon16():
        return send_file("./static/assets/icon-16.png",mimetype='image/png')
    
    @app.route("/assets/icon-32.png")
    def icon32():
        return send_file("./static/assets/icon-32.png",mimetype='image/png')
    
    @app.route("/assets/icon-64.png")
    def icon64():
        return send_file("./static/assets/icon-64.png",mimetype='image/png')
    
    @app.route("/assets/icon-80.png")
    def icon128():
        return send_file("./static/assets/icon-80.png",mimetype='image/png')
    
    @app.route("/assets/logo-filled.png")
    def iconlogofilled():
        return send_file("./static/assets/logo-filled.png",mimetype='image/png')
    

    如果有人知道更有效的方法,我会给你答案。谢谢

    【讨论】:

      猜你喜欢
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 2019-12-15
      • 2019-10-05
      • 1970-01-01
      相关资源
      最近更新 更多