烧瓶将变量从一条路径传递到另一条路径

时间:2019-07-26 01:04:02

标签: javascript python for-loop flask jinja2

这是一个带有工作进度条的flask应用程序。而不是基于for循环来取得进度(这里是原始代码How to redirect when server-sent-event is finished in Flask on server side?)。如何在“ /”路径上将表单的输入值传递给“ / progress”路径? app.py

@app.route('/', methods = ['GET', 'POST'])
def index():
    if request.method == 'POST':
        **input_num** = request.form.get('text')
        return render_template('index.html')


@app.route('/progress')
def progress():
    return Response(import_progress(), mimetype='text/event-stream')

def import_progress():
    for number in range(1, **input_num**):
        id = str(number)
        progress = str(number)
        event = 'import-progress'

        if number == 100:
            event = 'last-item'

        yield "id:{_id}\nevent:{event}\ndata:{progress}\n\n".format(
            _id=id, event=event, progress=progress)

index.html

<!DOCTYPE
html>

<html>

<head>

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {

           var source = new EventSource("/progress");

           source.addEventListener('import-progress',function(event)

                    {
                        $('.progress-bar')
                        .css('width',event.data +'%')
                        .attr('aria-valuenow',event.data);
                    }, false
                );
            }
        );
    </script>
</head>
<body>
    <form method=post enctype=multipart/form-data>
    <div class="form">
    <input type=text name=text placeholder="input_number">
    </div>
    </form>

    <h3>Progress Bar With Label</h3>
    <div class="progress">
        <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
        </div>
    </div>
    </div>
</body>
</html>

0 个答案:

没有答案