2016-03-12 21 views
2

這可能是一個簡單的問題,但我有點困惑。在通過瓶子web服務器呈現的html頁面中顯示iframe中的文本文件的內容

我使用flask webserver來保持我的UI。該服務器生成一個日誌文件。我想讓我們說,HTML頁面下方的一個iframe(通過燒瓶render_template('index.html')呈現的)顯示該文件的內容。

我知道像this和感謝@davidism的問題我已經學習了關於靜態文件的好的概念,但那不是我想要達到的。

到目前爲止,我可以說,再次感謝@davidism,我有一個main.py文件是這樣的:

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() 

,並通過使用這樣的:

<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> 

我得到的日誌在一個乾淨的整頁。我如何以上述方式得到這個結果。

p.s:我有將在未來呈現的多個html文件,我希望以一種方式在所有頁面中具有該日誌文件的iframe視圖。

回答

0

iframe實際上只是包含在頁面元素中的另一個頁面。所以你的主頁只會這樣做:

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

就是這樣。

相關問題