Publishing a folder

Consider the problem of sharing a folder (and subfolders) on the web. web2py makes this very easy. You just need a controller like this:

  1. from gluon.tools import Expose
  2. def myfolder():
  3. return dict(files=Expose('/path/to/myfolder'))

which you can render in a view with {{=files}}. It will create an interface to view the files and folders, and navigate the tree structure. Images will have a preview.

The path prefix “/path/to/myfolder” will be hidden to the visitors. For example a file called “/path/to/myfolder/a/b.txt” will be replaced with “base/a/b.txt”. The “base” prefix can be specified using the basename argument of the Expose function. Using the argument extensions can specify a list of file extensions to be listed, other files will be hidden. For example:

  1. def myfolder():
  2. return dict(files=Expose('/path/to/myfolder', basename='.',
  3. extensions=['.py', '.jpg']))

Files and folders that contain the word “private” in the path or have file that start with “.” or terminate in “~” are always hidden.