5

I am using Google App Engine for hosting my website which is mostly static pages. But there is a part where I need to get a list of images from a folder in the server and dynamically add those images to the page using JavaScript.

I got it working in my local machine using os.walk("static/images/temp/1.jpg") in a python script. But after deploying the site, it doesn't work. The python script return empty. Should there be a different approach for getting list of static files in Google App Engine?

RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
John Ng
  • 869
  • 3
  • 15
  • 28

1 Answers1

4

The main issue is that files in a static directory (which I'm assuming you're using to serve your files) are stored in a different location than your code, and are therefore not in your code's file directory.

One thing that you can do (see here for a similar issue) is create symlinks to your files (or just copy them) in another directory that you do not declare as a static_dir (or at all) in your app.yaml. It will then live with your application code and you can reference it using os. For instance, here is some very simple code that iterates through the files in a directory called upload_stuff in the root:

class MainPage(webapp2.RequestHandler):
  def get(self):
    path = os.path.join(os.path.dirname(__file__), 'upload_stuff')
    for filename in os.listdir(path):
      # Here you can do what you need with the files
      self.response.out.write(filename)

You can adjust this to work how you want, but the basic idea is that you will need to store the files with your code as well as in static form.

Community
  • 1
  • 1
RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
  • follow up question, if I just move the files to another directory and not declare them as static so python script can access them, will my static .htm page be able to access those images? The main reason why I need the python script is so that I can upload as many photos as I want in a particular folder, get the list and dynamically add them to the static .htm page. – John Ng Nov 26 '12 at 21:24
  • @JohnNg I think in your case it is the names of the files that are important, yeah? If so, you could do something like retrieve the list of all files in that folder, and then feed them into your script with the proper reference to the `static` folder. For instance, if you got your list back and it contained `file1.png`, you could then be sure that referencing `/static/images/file1.png` (or whatever your path to images is) would return the image, since your non-static dir will contain the exact same content as the static. Does that make sense? – RocketDonkey Nov 26 '12 at 21:28
  • Yes, I only need the filenames. This means I have to keep 2 folders (1 static and 1 non-static) to contain same images. I am trying to avoid this. I was just wondering if it's possible to just have a non-static folder of images. Let python script get the filenames from that non-static folder, then access the non-static folder from a static html file through `` tag – John Ng Nov 26 '12 at 22:21
  • @JohnNg One way you could do it is to create a symlink from the directory that contains the actual files - that way you don't ever have to worry about modifying two directories (changes to the main one will be reflected in the symlinked one). On Linux, that would be something like `ln -s images/ linked_images/`, where `linked_images` is the folder that will contain the symlinks that point to the static directory. – RocketDonkey Nov 26 '12 at 22:31
  • @JohnNg The reason it isn't possible is that a file at a given location (let's say `root/images/image1.png`) is not necessarily accessible by saying `/images/image1.png`. Therefore when you tried to serve them, you wouldn't have a way of saying 'I want to serve this image found at this path'. Therefore you have to create a `static_dir`, the filesystem of which lives in a different area than your code. Does that make sense? – RocketDonkey Nov 26 '12 at 22:34
  • Great. I'm using windows now so I'll have to research on how to create symlinks on window. – John Ng Nov 26 '12 at 23:33
  • @JohnNg Sounds great - this looks like it may help (http://lifehacker.com/5496652/how-to-use-symlinks-in-windows). Good luck with everything! – RocketDonkey Nov 27 '12 at 00:01