我正在尝试为我的网站创建一个记录器窗口,将 python 记录器消息流式传输到 javascript 弹出窗口。我已经到了如果我关闭窗口并重新打开它会显示新消息的地步,但我想编写一些 JavaScript 每隔 N 秒自动刷新一次窗口(并调用 dajaxice)。
我的 ajax.py:
@dajaxice_register(method='GET')
def getLogs(request):
fname = "/path/to/LOG_2015-07-08.log"
with open(fname,"r") as f:
lines = f.readlines()
lines = lines[-15:] //
logger.info("Displaying Logs")
return json.dumps({'message':lines})
我的 html:
<script language="javascript" type="text/javascript">
function popitup(data) {
sessionStorage.setItem("logs",data.message);
newWindow = window.open('/InterfaceApp/LogViewer', 'Log Viewer', 'height=400,width=800,status=yes');
if(newWindow && !newWindow.closed){
newWindow.location.reload(true); //this is my first attempt at a refresh, wasn't quite what I wanted.
newWindow.focus();
}
}
</script>
<div class="container">
<input id="LogMessages" type="button" value="View Log Messages" onclick="Dajaxice.InterfaceApp.getLogs(popitup)"/>
</div>
重申一下,我想单击按钮并弹出一个弹出窗口。我希望该弹出窗口每隔 N 秒刷新一次,其中包含我的日志文件的最后 15 行(每次用户浏览网站时,这些行都会添加到日志中)。中的 dajaxice 函数用于ajax.py
获取日志文件,以便以某种方式调用需要包含在刷新中。
谁能帮我解决这个问题?我已经为此苦苦挣扎了好几天。
谢谢!!