栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

文本/事件流被视为下载

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

文本/事件流被视为下载

我已经解决了,但是对于其他遇到相同问题的人:

index.html页面从不实际加载,因为从未在app.py中调用它。完成此操作的方法是转到单独的路由(例如/ page),然后返回

send_file('index/html')
。这将加载索引页面,创建链接到/ stream的EventSource,然后将在app.py中启动stream方法并产生正确的数据。

通过

x
每0.2秒增加一个进度条并将其显示在网页上的示例:

app.py

@app.route('/page')def get_page():    return send_file('templates/progress.html')@app.route('/progress')def progress():    def generate():        x = 0        while x < 100: print x x = x + 10 time.sleep(0.2) yield "data:" + str(x) + "nn"    return Response(generate(), mimetype= 'text/event-stream')

progress.html

<!DOCTYPE html><html><head>    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>    <script>    var source = new EventSource("/progress");    source.onmessage = function(event) {        $('.progress-bar').css('width', event.data+'%').attr('aria-valuenow', event.data);       }    </script></head><body>    <div  >        <div  role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" ></div>    </div></body></html>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/395156.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号