<p>Flasky方法是使用<a href="http://flask.pocoo.org/docs/0.12/api/#template-rendering" rel="nofollow noreferrer">^{<cd1>}</a>。创建一个HTML文件并使用<a href="http://flask.pocoo.org/docs/0.12/templating/" rel="nofollow noreferrer">Jinja syntax</a>显示数据。像这样:</p>
<h2>在应用程序副本在</h2>
<pre><code>from flask import Flask, render_template
app = Flask(__name__)
def parse_key(match):
for x, y in enumerate(match):
if y.isdigit():
return match[:x]
@app.route('/')
def index():
data = {}
matches = [
'Ubuntu10.1.iso',
'WindowsServer2016.iso',
'Ubuntu10.2.iso'
]
data = {parse_key(x): [y for y in matches if parse_key(y) == parse_key(x)] for x in matches}
for key in data:
data[key].sort()
// check if the request is coming from JS.
if request.is_xhr:
return json.dumps(data)
return render_template('ISOs.html', data=data)
</code></pre>
<h2>模板/ISOs.html在</h2>
^{pr2}$
<h2>结果:</h2>
<h2>ISO列表</h2>
<h3>Windows服务器:</h3>
<ul>
<li>WindowsServer2016.iso</li>
</ul>
<h3>Ubuntu:</h3>
<ul>
<li>Ubuntu10.1.iso</li>
<li>Ubuntu10.2.iso</li>
</ul>
<h2>编辑:</h2>
<p>我没意识到这个名单必须更新。不需要重新加载页面,只需添加一些JavaScript来命中<a href="https://www.w3schools.com/jsref/met_win_setinterval.asp" rel="nofollow noreferrer">interval</a>上的端点。注意,我已经改变了这两个应用程序副本以及ISOs.html与JS合作。你应该读一下这个<a href="http://flask.pocoo.org/docs/0.12/api/#flask.Request.is_xhr" rel="nofollow noreferrer">method</a>。在</p>
<h3>JavaScript</h3>
<pre><code>var $isoListDiv = $('#isoList'),
checkISOList = function checkISOList() {
$.ajax({
url: '/',
type: 'GET'
}).done(function(data) {
try {
JSON.parse(data);
} catch (e) {
// display error
return false;
}
setISOList(data);
}).fail(function(jqXHR) {
// display error
});
}, setISOList = function (data) {
var isoList = "";
Object.keys(data).forEach(function(key) {
isoList += "<h3>" + key + "</h3><ul>";
for (var x = 0; x < data[key].length; x++) {
isoList += "<li>" + data[key][x] + "</li>";
}
isoList += "</ul><br>";
});
$isoListDiv.html(isoList);
};
setInterval(checkISOList, 2000);
</code></pre>