使用 Flask 显示特定目录中的文件内容
Display content of files in a particular directory using Flask
我正在使用 Flask 实现一个应用程序,我正在尝试显示我放在日志目录中的文本文件的内容。所以我这样做了:
@app.route('/files', methods = ['GET'])
def config():
if 'username' in session :
path = os.path.expanduser(u'~/path/to/log/')
return render_template('files.html', tree=make_tree(path))
else:
return redirect(url_for('login'))
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
在我的 html 页面中 files.html:
<title>Path: {{ tree.name }}</title>
<h1>{{ tree.name }}</h1>
<div class="accordion-heading" >
<div class="accordion-toggle" >
<a data-toggle="collapse" data-target="#files_list" href="#files_list">
<b>
<ul>
{%- for item in tree.children recursive %}
<div class="well well-sm"> <li>{{ item.name }} </div>
{%- if item.children -%}
<ul>{{ loop(item.children) }}</ul>
{%- endif %}</li>
{%- endfor %}
</ul>
</b></div>
</a>
</div>
这只显示我的日志目录中的文件名,但我无法显示文件的内容。
我想也许我可以使用类似的东西:
import fnmatch
def display_files():
for dirpath, dirs, files in os.walk('log'):
for filename in fnmatch.filter(files, '*.*'):
with open(os.path.join(dirpath, filename)) as f:
file_contents = f.read().strip()
print file_contents
return render_template('files.html',title="index",file_contents=file_contents)
有什么帮助吗??
您可能可以读取文件内容并将其传递给 make_tree
函数中的模板,如下所示:
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
然后在要显示文件内容的地方加上<pre>{{ item.contents }}</pre>
即可。例如,这里:
{%- 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 %}
这有点难看,但它应该显示正确的数据。
我正在使用 Flask 实现一个应用程序,我正在尝试显示我放在日志目录中的文本文件的内容。所以我这样做了:
@app.route('/files', methods = ['GET'])
def config():
if 'username' in session :
path = os.path.expanduser(u'~/path/to/log/')
return render_template('files.html', tree=make_tree(path))
else:
return redirect(url_for('login'))
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
在我的 html 页面中 files.html:
<title>Path: {{ tree.name }}</title>
<h1>{{ tree.name }}</h1>
<div class="accordion-heading" >
<div class="accordion-toggle" >
<a data-toggle="collapse" data-target="#files_list" href="#files_list">
<b>
<ul>
{%- for item in tree.children recursive %}
<div class="well well-sm"> <li>{{ item.name }} </div>
{%- if item.children -%}
<ul>{{ loop(item.children) }}</ul>
{%- endif %}</li>
{%- endfor %}
</ul>
</b></div>
</a>
</div>
这只显示我的日志目录中的文件名,但我无法显示文件的内容。 我想也许我可以使用类似的东西:
import fnmatch
def display_files():
for dirpath, dirs, files in os.walk('log'):
for filename in fnmatch.filter(files, '*.*'):
with open(os.path.join(dirpath, filename)) as f:
file_contents = f.read().strip()
print file_contents
return render_template('files.html',title="index",file_contents=file_contents)
有什么帮助吗??
您可能可以读取文件内容并将其传递给 make_tree
函数中的模板,如下所示:
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
然后在要显示文件内容的地方加上<pre>{{ item.contents }}</pre>
即可。例如,这里:
{%- 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 %}
这有点难看,但它应该显示正确的数据。