Bobys
Bobys

Reputation: 677

Load a directory in a js with flask

I would like to declare in a script, a directory.

$images_dir = '{{url_for('.../pictures')}}';

My flask application directory looks like:

Root
-wep.py
-templates
 -gallery.html
-static
-pictures

The picture are located inside the pictures folder, and the html page that contains the script is gallery.html which located in the templates folder.

The purpose of that script is to list all the images that are located in the pictures folder and present them as a gallery view when the gallery.html page is loaded. The script works fine if I run it in a normal apache webserver though.

When I run the web.py, the debuger gives me the error:

BuildError: ('../pictures', {}, None)

So I think the problem is to declare the directory in flask.

UPDATE: Im using this guy's script : http://davidwalsh.name/generate-photo-gallery

As you can see in the source code:

/** settings **/
$images_dir = 'preload-images/';
$thumbs_dir = 'preload-images-thumbs/';

Im trying to adjust those line to work with flask.

Upvotes: 1

Views: 348

Answers (1)

abathur
abathur

Reputation: 1047

I think you're misunderstanding the purpose of url_for. url_for is for generating a link to one of your application's HTTP endpoints/view functions. You don't need any flask-specific method to get a list of files. You might find glob or os.listdir() helpful for this purpose. Then you can pass a list of the relevant paths to your template for rendering.

Upvotes: 1

Related Questions