Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying a .txt file in my html using Python Flask

Tags:

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.

like image 465
Julian Avatar asked Oct 25 '16 18:10

Julian


People also ask

How do you display Python output on a flask HTML page?

Create a simple HTML page to display text. create a route ”/” and return home. html from the function. Then run your api.py file and click on the link that it provides after running.


1 Answers

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

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

like image 70
Joran Beasley Avatar answered Oct 13 '22 15:10

Joran Beasley