1

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.

  • 1
    The most efficient method is to let the http server to serve static files. See here: https://stackoverflow.com/questions/20646822/how-to-serve-static-files-in-flask – VPfB Dec 22 '21 at 20:25

2 Answers2

0

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

0
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

Bao Tran
  • 618
  • 4
  • 15