Skip to content

Latest commit

 

History

History
41 lines (36 loc) · 1016 Bytes

File metadata and controls

41 lines (36 loc) · 1016 Bytes

Flask directory listing dirtree.html <!doctype html>

<title>Path: {{ tree.name }}</title>

{{ tree.name }}

    {%- for item in tree.children recursive %}
  • {{ item.name }} {%- if item.children -%}
      {{ loop(item.children) }}
    {%- endif %}
  • {%- endfor %}
dirtree.py 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: tree['children'].append(dict(name=name)) return tree

import os from flask import Flask, render_template

app = Flask(name)

@app.route('/') def dirtree(): path = os.path.expanduser(u'~') return render_template('dirtree.html', tree=make_tree(path))

if name=="main": app.run(host='localhost', port=8888, debug=True)