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:
from gluon.tools import Expose
def myfolder():
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:
def myfolder():
return dict(files=Expose('/path/to/myfolder', basename='.',
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.