如何在server-sent-event在服务器端的Flask中完成重定向? [英] How to redirect when server-sent-event is finished in Flask on server side?
问题描述
$ b b $ b
问题描述
在客户端代码(JavaScript)上实现了一个EventSource,在服务器端代码(Flask,Python)上返回了一个Response。如果发送服务器的最后一个项目,EventSource将被关闭。我看不到通过服务器端代码重定向到另一个站点的明确解决方案。也许答案很简单,但我没有得到它。
类似的问题
类似的问题是如何停止服务器发送的事件和一个可能的答案在评论服务器发送的事件如何实际工作?我不确定,如果php和python是一样的,那么我已经开始了一个新的问题。我也没有得到它。
编辑 与此问题密切相关。这个问题主要是看你如何阻止SSE从Flask。
客户端的工作解决方案
所以我想出了一个解决方案(一个进度条),但只在客户端。
相关的代码片段
HTML / Jinja
{%block additional_stylesheets%}
{%endblock%}
{%block additional_javascripts%}
{%endblock%}
{%block content%}
< div class =progress>
role =progressbararia-valuenow =0aria-valuemin =0
aria-valuemax = 100 >
< / div>
< / div>
{%endblock%}
JavaScript
$(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
);
source.addEventListener('last-item '),函数()
{
source.close();
redirect();
},false
);
}
);
#这个工程!但如何做到这一点,而无需调用客户端网站上的redirect()
#?
function redirect()
{
window.document.location.href = window.location.protocol +//+
window.location.host +/ products。 HTML;
$ / code>
烧瓶
from foo import app $ b $ from flask import render_template,Response
@ app.route('/ data_import.html')
def data_import ):
return render_template(
'data_import.html')
$ b $ app_route('/ progress')
def progress()
return(import_progress(),mimetype ='text / event-stream')
$ b $ def import_progress():
只是一个小例子
(1,101):
sse_id = str(number)
sse_progress = str(number)
sse_event ='import-progress '
if number == 100:
sse_event ='last-item'
yieldid:{_ id} \\\
event:{event} \ ndata:{progress} \\\
\\\
.format(
_id = sse_id,event = sse_event,progress = sse_progress)
我尝试了很多重定向工作G。但我不知道该怎么做。到目前为止,每一次尝试都失败了。
您想要做的是发送重定向网址作为最后一个事件 - 仍然需要使用JavaScript进行重定向,但是您不必对路径进行硬编码:
$ b
def import_progress() :
只是一个小例子
(1,101):
sse_id = str(number)
sse_data = str(number)
sse_event ='import-progress'
if number == 100:
sse_event ='last-item'
sse_data = url_for ('product_list')
yieldid:{_ id} \ event:{event} \ data:{data} \\\
\\\
.format(
_id = sse_id,event = sse_event,data = sse_data)
然后你的最后一项处理函数变成:
source.addEventListener('last-item',function(event){
source.close();
重定向(event.data);
},假
);
和重定向
变得很简单:函数重定向(url){
document.location = url;
}
Question
How can one redirect to another page when server-sent-event is finished in Flask and on server side?
Problem description
An EventSource was implemented on client side code (JavaScript) and a Response was returned on server side code (Flask, Python). The EventSource will be closed if the last item from server was sent. I can't see a clear solution to redirect to another site via server side code. Maybe the answer is simple, but I'm not getting it.
Similar problem
A similar question is How to stop Server-Sent Events and one possible answer in the comment How do server-sent events actually work?. I'm not sure, if it's the same for php and python, so I've started a new question for that. I also didn't get it.
EDIT How do I close a Server-Send Events connection in Flask? is strongly related to this question. The question there is mainly to see how you can stop SSE from Flask.
Working solution on client side
So I came up with a solution that worked (a progress bar), but only on the client side. How do I have to change the code to get a working example of reconnecting via Flask functions?
Relevant code snippets
HTML/Jinja
{% block additional_stylesheets %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="{{ url_for('static', filename='data_import.css') }}" />
{% endblock %}
{% block additional_javascripts %}
<script src="{{ url_for('static', filename='js/data_import.js') }}"></script>
{% endblock %}
{% block content %}
<div class="progress">
<div class="progress-bar progress-bar-striped active"
role="progressbar" aria-valuenow="0" aria-valuemin="0"
aria-valuemax="100">
</div>
</div>
{% endblock %}
JavaScript
$(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
);
source.addEventListener('last-item', function()
{
source.close();
redirect();
}, false
);
}
);
# This works! But how to do the same thing without calling redirect()
# on client site?
function redirect()
{
window.document.location.href = window.location.protocol + "//" +
window.location.host + "/products.html";
}
Flask
from foo import app
from flask import render_template, Response
@app.route('/data_import.html')
def data_import():
return render_template(
'data_import.html')
@app.route('/progress')
def progress():
return Response(import_progress(), mimetype='text/event-stream')
def import_progress():
"""
Just a small example
"""
for number in range(1, 101):
sse_id = str(number)
sse_progress = str(number)
sse_event = 'import-progress'
if number == 100:
sse_event = 'last-item'
yield "id:{_id}\nevent:{event}\ndata:{progress}\n\n".format(
_id=sse_id, event=sse_event, progress=sse_progress)
I tried a lot to get a redirect working. But I don't know exactly how to do it. Every attempt so far has failed.
What you want to do is send the redirect URL as the last event - you'll still need to redirect using JavaScript, but you won't have to hard-code the path any more:
def import_progress():
"""
Just a small example
"""
for number in range(1, 101):
sse_id = str(number)
sse_data = str(number)
sse_event = 'import-progress'
if number == 100:
sse_event = 'last-item'
sse_data = url_for('product_list')
yield "id:{_id}\nevent:{event}\ndata:{data}\n\n".format(
_id=sse_id, event=sse_event, data=sse_data)
Then your last-item handler becomes:
source.addEventListener('last-item', function(event) {
source.close();
redirect(event.data);
}, false
);
And redirect
becomes a simple:
function redirect(url) {
document.location = url;
}
这篇关于如何在server-sent-event在服务器端的Flask中完成重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!