Skip to content

Instantly share code, notes, and snippets.

@Park-Developer
Created February 17, 2022 15:25
Show Gist options
  • Save Park-Developer/6c9987bb588e78e89d6231f5a6e243a5 to your computer and use it in GitHub Desktop.
Save Park-Developer/6c9987bb588e78e89d6231f5a6e243a5 to your computer and use it in GitHub Desktop.
flask realtime text update
from flask import Flask, render_template_string
import time
app = Flask(__name__)
@app.route('/')
def index():
return render_template_string('''<p>This is the current value: <span id="latest_value"></span></p>
<script>
var latest = document.getElementById('latest_value');
var xhr = new XMLHttpRequest();
xhr.open('GET', '{{ url_for('stream') }}');
xhr.onreadystatechange = function() {
var all_lines = xhr.responseText.split('\\n');
last_line = all_lines.length - 2
latest.textContent = all_lines[last_line]
if (xhr.readyState == XMLHttpRequest.DONE) {
/*alert("The End of Stream");*/
latest.textContent = "The End of Stream"
}
}
xhr.send();
</script>''')
@app.route('/stream_time')
def stream():
def generate():
while True:
current_time = time.strftime("%H:%M:%S\n")
print(current_time)
yield current_time
time.sleep(1)
return app.response_class(generate(), mimetype='text/plain')
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment