有趣的时钟流与python和flask的文本 [英] fun clock streaming text with python and flask

查看:52
本文介绍了有趣的时钟流与python和flask的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助.用python和flask创建一个简单的文本时钟应用程序需要什么代码来演示烧瓶流有效吗?理想情况下,该应用将从每秒一次的更新流中显示文本时间,并在屏幕上覆盖.

Please help. What code would it take to make a simple text clock application with python and flask to demonstrate how flask streaming works? Ideally the app would show text time in place, overwriting on screen, from the once per second updating stream.

时钟流是实时文本流的简化情况.真正的基本需求是服务器推出同步视频和视频.文本流(无音频),例如米格尔·格林伯格的视频流与烧瓶和在客户端的屏幕上显示两个更新.Miguel的视频流演示作品.但是,我还不知道如何使同步文本流正常工作.

The clock stream is a simplified case of a real time text stream. The real underlying need is for a server to push out simultaneous video & text streams (no audio) like Miguel Grinberg's Video Streaming with Flask and to show both updates on screen on the client side. Miguel's video stream demo works. However I do not yet know how to get a simultaneous text stream to work.

我尝试了下面的代码,但有一些奇怪之处:<​​/p>

I have tried the code below and there are some quirks:

  1. html p元素只是文本的占位符.可以将其更改为可能更有效的任何方法.
  2. http://localhost:5000 在我希望显示时间字符串内容时显示'/time_feed'/time_feed.
  3. http://localhost:5000/time_feed 在烧瓶服务器处于运行状态时什么也没有显示跑步.当烧瓶服务器停止运行时,突然之间会有大量更新出现在浏览器中,例如:

  1. The html p element is just a place holder for text. It could be changed to anything that might work better.
  2. http://localhost:5000 shows '/time_feed' when I want it to instead show the time string contents of /time_feed.
  3. http://localhost:5000/time_feed shows nothing at all while the flask server is running. When the flask server is stopped all of the sudden a bunch of updates appear in the browser like:

2018.11.01 | 20:29:272018.11.01 | 20:29:282018.11.01 | 20:29:292018.11.01 | 20:29:302018.11.01 | 20:29:312018.11.01 | 20:29:322018.11.01 | 20:29:33

2018.11.01|20:29:272018.11.01|20:29:282018.11.01|20:29:292018.11.01|20:29:302018.11.01|20:29:312018.11.01|20:29:322018.11.01|20:29:33

我尝试了使用Python和Flask进行数据流,但不了解如何将javascript或jinga答案应用于时钟代码.我是新学习网络和Flask开发的人.

I tried Streaming data with Python and Flask but do not understand how to apply either the javascript or jinga answers to my clock's code. I am new learning web and flask development.

app.py:

#!python3 Flask 
# streaming clock per http://flask.pocoo.org/docs/1.0/patterns/streaming

from flask import Flask, Response, render_template, url_for
from datetime import datetime
import time

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/time_feed')
def time_feed():
    def generate():
        while True:
            yield datetime.now().strftime("%Y.%m.%d|%H:%M:%S")
            time.sleep(1)
    return Response(generate(), mimetype='text')

if __name__ == '__main__':
    app.run(debug=True, threaded=True)

./templates/index.html:

./templates/index.html:

<title>Clock</title>
<h1>Clock</h1>
<p>{{ url_for('time_feed') }}</p>

推荐答案

在该示例中,您可以避免 while sleep :

in view you can avoid while and sleep in that example:

@app.route('/time_feed')
def time_feed():
    def generate():
        yield datetime.now().strftime("%Y.%m.%d|%H:%M:%S")  # return also will work
    return Response(generate(), mimetype='text') 

在模板中:

<p id="clock">Here will be date|time</p>

<script>
    var clock = document.getElementById("clock");

    setInterval(() => {
        fetch("{{ url_for('time_feed') }}")
        .then(response => {
                response.text().then(t => {clock.innerHTML = t})
            });
        }, 1000);  
</script>

在您使用视频广告的示例中,这只是技巧,它不是真正的视频流的解决方案,只是因为不提供音频.如果您需要真实的视频流,则需要将 webRTC 与Kurento-media-server一起使用.对于python,请查看 aiortc lib.

in your example with video streem it is just trick, it is not solution for real video streaming, just because not provide audio. If you need for real video stream you need to use webRTC with Kurento-media-server for example. For python look on aiortc lib.

这篇关于有趣的时钟流与python和flask的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆