0

I want to return the URL for an image as JSON with jsonify. Everything is ok, but when I access the URL, error 404.

This is my code:

from flask import Flask, jsonify
import json

app = Flask(__name__)

with open("config.json") as f:
    data = json.load(f)

cache_images = [img for img in os.listdir(data['altImg'])]

@app.route("/random.json")
def randomJSON():
    return jsonify({ "link": data['domain'] + data['altImg'] + "/" + random.choice(cache_images)})

if __name__ == '__main__':
    app.run(debug=True)

The output is: localhost:5000/images/amidamaru.jpg, which is correct but I can't find out why it gives me error 404.

1 Answers1

0

Use a static folder

app = Flask(__name__, static_folder='public/images')

Access the images in the folder via URL

http://localhost:5000/images/sample.jpg
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55