Display content of files in a particular directory using Flask Display content of files in a particular directory using Flask flask flask

Display content of files in a particular directory using Flask


You probably can read contents of your file and pass it to template in make_tree function like that:

def make_tree(path):    tree = dict(name=os.path.basename(path), children=[])    try: lst = os.listdir(path)    except OSError:        pass #ignore errors    else:        for name in lst:            fn = os.path.join(path, name)            if os.path.isdir(fn):                tree['children'].append(make_tree(fn))            else:                with open(fn) as f:                    contents = f.read()                tree['children'].append(dict(name=name, contents=contents))    return tree

and just add <pre>{{ item.contents }}</pre> to the place where you want to display the contents of the file. For example, here:

{%- for item in tree.children recursive %}     <div class="well well-sm">        <li>            {{ item.name }}            <pre>{{ item.contents }}</pre>            {%- if item.children -%}                <ul>{{ loop(item.children) }}</ul>            {%- endif %}        </li>    </div>{%- endfor %}

This is kind of ugly, but it should show the correct data.