Julian Julian - 1 year ago 116
Python Question

Displaying a .txt file in my html using Python Flask

I want to display my log.txt in my log.html.
For some reason my page is completely blank.
And I dont get to see anything from my file.

def log():
with open("logs.txt", "r") as f:
content = f.read()
return render_template('log.html', content=content)


<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<link rel="stylesheet" href="/static/styles/nav.css" />
<link rel="stylesheet" href="/static/styles/basiclayout.css" />
<meta name="viewport" content="width=device-width, initial-scale=1">

<ul class="nav">
<li ><a href="{{ url_for('hello_world') }}" >Home</a></li>
<li ><a href="{{ url_for('notepad') }}">Notepad</a></li>
<li ><a href="{{ url_for('explorer') }}">Explorer </a></li>
<li class="active"><a href="{{ url_for('log') }}">Log </a></li>
<li ><a href="{{ url_for('upload') }}">Upload </a></li>
<li ><a href="{{ url_for('uploads') }}">Uploads </a></li>
<li ><a href="{{ url_for('logout') }}">Logout</a></li>

<div class="alert">
{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %}

<pre>{{ content }}</pre>


Added my HTML Template now.

Answer Source
 return Response(f.read(), mimetype='text/plain')

but really you probably want to use something like logstash...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download