1

Flask newbie here, trying to show an image through Flask.

==============================================

~/server.py

==============================================

#!/usr/bin/env python3.6

import os
from flask import Flask, request, render_template, g, redirect, Response, send_from_directory

tmpl_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
print(tmpl_dir)
app = Flask(__name__, template_folder=tmpl_dir)

@app.route('/')
def index():
    print(request.args)
    return render_template("index.html")

if __name__ == "__main__":
    app.run()

==============================================

~/templates/index.html:

==============================================

<!DOCTYPE html>
<html>
  <body>
    <h1>Text-to-Image Synthesis</h1>
    <img src="/home/n/templates/tree.jpg" width="700" height="500">
  </body>
</html>

Things I've tried:

  1. send_from_directory()
  2. render_template("index.html", **context)
  3. Literally copying the code from python flask display image on a html page

The image tree.jpg is in the proper directory.

davidism
  • 121,510
  • 29
  • 395
  • 339

1 Answers1

2

Follow these steps,

  1. Move tree.jpg to static folder
  2. As per your project structure, static path for image should be ~/static/tree.jpg
  3. Then use,

    <img src="{{url_for('static', filename='tree.jpg')}}" />

Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81