Flask - render template asynchronously

2019-03-04 00:55发布

I'm making a Flask app and I was wondering if I could render a template for a route, but redirect the user after a function is complete. Currently using Python 2.7 Here is my example

@app.route('/loading/matched')
def match():
    time_match()
    return render_template('matched.html')

def time_match():
    # match two users based on time
    sleep(3) # pretend to be doing
    return redirect('/loading/generation')

I don't know where to begin. Is there a library I should use?

1条回答
走好不送
2楼-- · 2019-03-04 01:33

This sounds more like a client sided thing to me? Do you want something like a loading bar?

You could provide an ajax route which initiates heavy workload on the server side - while the client does show some progress. Once the workload finished you render a template which than gets loaded via ajax.

For asycn workload you could look into Celery, which is a great library for that. It even can do work on a seperate server...

More sources Integration in Flask

查看更多
登录 后发表回答