1

I'm trying do develop a very simple web app using flask, following the example from this link.

My problem is: I want to show images from my file system in that page but it seems like browsers are protected against that. I have to use something like:

<img src="http://aMessyURL.png">

Instead of being able to use something like:

<img src="images/myImageName.png">

So, I can I (dynamically) show images from my file system?

arocha
  • 27
  • 1
  • 10
  • did you read [Flask Quickstart](http://flask.pocoo.org/docs/0.12/quickstart/#static-files) section for static files? For more you can read [Link to Flask static files with url_for](https://stackoverflow.com/questions/16351826/link-to-flask-static-files-with-url-for) – an0o0nym Mar 25 '18 at 17:19

3 Answers3

0

You should be able to do it when hosting a simple server: https://docs.python.org/2/library/simplehttpserver.html

This should at least allow you to load files in the same folder as your code.

LizardCode
  • 28
  • 5
0

Flask interprets <img src="images/myImageName.png"> as:

app/images/myImageName.png

Normally, Flask projects have a static folder inside app (app/static) which contains your CSS, JS and images. You can either render them like above or use url_for:

<link rel="shortcut icon" href="{{ url_for('static', filename='img/favicon.png') }}">

Which renders fully as:

<link rel="shortcut icon" href="/static/img/favicon.png"> 
RyanH
  • 991
  • 7
  • 13
0

You Have to specify static root path and then use code in html below

app = Flask(__name__, static_url_path='')

In HTML

<img src="/static/images/myImageName.png">
Beqa Bukhradze
  • 409
  • 4
  • 16