Bootstrap 3.3.7-在另一条路线上运行函数时在按钮中加载gif

时间:2018-09-19 14:29:09

标签: flask twitter-bootstrap-3 flask-bootstrap

我的烧瓶应用程序上有一些按钮,单击这些按钮可在另一条路线上触发功能。我要在页面等待功能完成时添加加载gif。我从SO尝试了很多例子,但是我没有运气。请注意,我正在使用Bootstrap 3.3.7,因此无法使用

HTML:  路线/ dashboard(按钮位于此处)

{% block styles %}
<!-- Bootstrap latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Custom CSS -->
<link  href="{{ url_for('static', filename='css/custom-base.css') }}" rel="stylesheet">
<!-- Font Awesome-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Pretty loading buttons -->
<link  href="{{ url_for('static', filename='css/loading-btn.css') }}" rel="stylesheet">
{% endblock %}

<!-- run function-->
<div class="container">
    <div class="jumbotron">
        <h2>Run Model</h2>
           <div class="container" align="left">
                <a href="/run_model" target="">
                    <button class="btn btn-primary ">Run</button></a>
           </div>
    </div>
</div>

{% block scripts %}
    <!-- JQuery -->
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <!-- Bootstrap JS -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>


{% endblock %}

路线: 该按钮链接到run_model函数,执行并重定向 到仪表板页面。

{% block content %}

@app.route('/run_model')
def run_model():
try:
    # run a machine learning mode


except:
    # if errors found flash some error
    flash('Looks like something went wrong. Please try again! :(', 'danger')

return redirect(url_for('dashboard'))
{% endblock %}

因此,从本质上讲,当浏览器“正在等待127.0.0.1:5000/run_model时,我希望使按钮仍在/ dashboard页面上进行动画处理 完成并重定向回/dashboard

1 个答案:

答案 0 :(得分:1)

所以这是一个JavaScript问题,而不是烧瓶问题。单击链接并重定向后,所有javascript / css都将被杀死。因此,点击按钮后,首先显示您的加载图标,然后重定向到您想要的任何页面:

完整示例:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
/* Center the loader */
#loader {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 1;
  width: 150px;
  height: 150px;
  margin: -75px 0 0 -75px;
  border: 16px solid #f3f3f3;
  border-radius: 50%;
  border-top: 16px solid #3498db;
  width: 120px;
  height: 120px;
  -webkit-animation: spin 2s linear infinite;
  animation: spin 2s linear infinite;
}

@-webkit-keyframes spin {
  0% { -webkit-transform: rotate(0deg); }
  100% { -webkit-transform: rotate(360deg); }
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* Add animation to "page content" */
.animate-bottom {
  position: relative;
  -webkit-animation-name: animatebottom;
  -webkit-animation-duration: 1s;
  animation-name: animatebottom;
  animation-duration: 1s
}

@-webkit-keyframes animatebottom {
  from { bottom:-100px; opacity:0 }
  to { bottom:0px; opacity:1 }
}

@keyframes animatebottom {
  from{ bottom:-100px; opacity:0 }
  to{ bottom:0; opacity:1 }
}

#myDiv {
  display: none;
  text-align: center;
}
</style>
</head>
<body style="margin:0;">

<div id="loader" style="display:none;"></div>

<div  id="myDiv" style="display:block" class="animate-bottom">
  <h2>Tada!</h2>
  <p>Some text in my newly loaded page..</p>
  <a href="/run_model" target=""><button class="btn btn-primary" onclick="testfunc()">Run</button></a>
</div>

<script>


function testfunc(){
  document.getElementById("loader").style.display = "block"
  document.getElementById("myDiv").style.display = "none"
  setTimeout(redirect, 5)

}

function redirect(){
  window.location.href = 'http://127.0.0.1:5000/';
}

</script>
</body>
</html>

修改自:https://www.w3schools.com/howto/howto_css_loader.asp