-1

I am trying to make a TicTacToe web app with an AI playing as player 2. To do this I coded the game and AI in python, wrote html/javascript front end, and used a flask server. In the html I have img tags and assets such as (these are embedded in the html file in the traditional manner):

<img id="pic" src="assets/images/hil.jpg" class="X">
or
<audio src="assets/audio/smw_coin.wav" id="marioStart" preload="auto"></audio>

However, I get errors of this form.

127.0.0.1 - - [11/Feb/2016 23:52:22] "GET /assets/audio/smb_pipe.wav HTTP/1.1" 404 -

127.0.0.1 - - [11/Feb/2016 23:52:22] "GET /assets/js/jquery-1.11.3.js HTTP/1.1" 404 -

Flask seems to interpret this as a domain name instead of check the assets folder. Does anyone know how to get Flask to interpret the src request as a directory in the flask templates folder?

PS. This is also an issue because it stops me from including JQuery in my html file.

Nali
  • 1
  • 2
  • Possible duplicate of [Flask not finding files in my package's 'static' directory](http://stackoverflow.com/questions/35593283/flask-not-finding-files-in-my-packages-static-directory) B/c the linked question is asked more generally, this question should be deleted. – franklin Feb 25 '16 at 04:48

1 Answers1

0

You want to use url_for.

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

By default, you'll end up with a URL that looks like /static/images/hil.jpg that is served up from your static folder. If you'd like to keep the URLs you have, you have to tell Flask to use a different URL path for static assets.

app = Flask(__name__, static_url_path='/assets')

This will give you a URL that looks like /assets/images/hil.jpg' that is served up from yourstatic` folder. If you've already named the folder assets and you don't want to rename it, you can tell Flask to look for static assets in a different folder, too.

app = Flask(__name__, static_folder='assets')

This will give you a URL that looks like /assets/images/hil.jpg that is served up from your assets folder. You don't need to specify both arguments in this case because static_url_path uses static_folder by default.

dirn
  • 19,454
  • 5
  • 69
  • 74