I need to expose some icons from a folder in flask like so:
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
I need to make the assets
routeable so I can access the png files from urls like this:
https://pythonlinuxtest.azurewebsites.net/assets/icon-16.png
https://pythonlinuxtest.azurewebsites.net/assets/icon-32.png
https://pythonlinuxtest.azurewebsites.net/assets/icon-64.png
Here is what I have in my app.py
so far:
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")
I am not sure how to add the assets
directory to the app.py so the png files are accessible.
CodePudding user response:
I figured it out!! But if anyone knows a better way let me know.
I added the pictures to the app.py using send_file
Here is the whole app.py now :)
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')
If anyone knows a more efficient method I will give you credit for the answer. Thanks
CodePudding user response:
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')
Could you try this one