Skip to content
Advertisement

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. Code:

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

HTML LOG TEMPLATE:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Log</title>
    <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">
</head>
<style>


</style>
<body>
<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>



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

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


</body>
</html>

Added my HTML Template now.

Advertisement

Answer

 return Response(content, mimetype='text/plain')

but really you probably want to use something like logstash…

Advertisement