你可以简单地在AJAX的帮助下完成此操作…这是一个示例,该示例调用python函数,该函数在不重定向或刷新页面的情况下打印问候。
在app.py中,将其放在代码段下方。
#rendering the HTML page which has the button
@app.route('/json')
def json():
return render_template('json.html')
#background process happening without any refreshing
@app.route('/background_process_test')
def background_process_test():
print ("Hello")
return ("nothing")
并且你的json.html页面应如下所示。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
$(function() {
$('a#test').bind('click', function() {
$.getJSON('/background_process_test',
function(data) {
//do nothing
});
return false;
});
});
</script>
//button
<div class='container'>
<h3>Test</h3>
<form>
<a href=# id=test><button class='btn btn-default'>Test</button></a>
</form>
</div>