4

This is probably a simple question but I got somehow confused.

I'm using flask webserver to keep my UI up. This server generates a log file. I want to have, let's say, an iframe below the html page (the one which is rendered via flask render_template('index.html') ) showing the contents of that file.

I'm aware of the questions like this and thanks to @davidism I've learnt nice concepts about serving the static files but that's not exactly what I want to achieve.

So far I can say, again thanks to @davidism, that I have a main.py file like this:

from time import sleep
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
return render_template('index.html')

@app.route('/stream')
def stream():
    def generate():
         with open('job.log') as f:
            while True:
                yield f.read()
                sleep(1)

    return app.response_class(generate(), mimetype='text/plain')

app.run()

and by using this:

<pre id="output"></pre>
<script>
    var output = document.getElementById('output');

    var xhr = new XMLHttpRequest();
    xhr.open('GET', '{{ url_for('stream') }}');
    xhr.send();

    setInterval(function() {
        output.textContent = xhr.responseText;
    }, 1000);
</script>

I get that log in a clean full page. How can I get this in that aforementioned way.

p.s: I have multiple html files which will be rendered in the future and I want to implement this in a way to have that iframe view of the log file in all pages.

Community
  • 1
  • 1
Sina Sh
  • 1,177
  • 3
  • 15
  • 24

1 Answers1

0

An iframe is literally just another page contained within an element on your page. So your main page would just do:

<iframe src ="/stream"></iframe>

and that's it.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895