how to refresh automatically in flask

25 Views Asked by At
from flask import Flask, render_template, jsonify, Response
import threading
import time

app = Flask(__name__)

counter = 0


def increment_counter():
    global counter
    while True:
        time.sleep(1)
        counter += 1



counter_thread = threading.Thread(target=increment_counter)
counter_thread.daemon = True
counter_thread.start()


@app.route("/update")
def update():
    def generate():
        while True:
            yield f"data: {counter}"
            time.sleep(0.5)

    return Response(generate(), mimetype="text/event-stream")


if __name__ == "__main__":
    app.run(debug=True)
from flask import Flask, render_template, jsonify, Response
import threading
import time

app = Flask(__name__)

counter = 0


def increment_counter():
    global counter
    while True:
        time.sleep(1)
        counter += 1



counter_thread = threading.Thread(target=increment_counter)
counter_thread.daemon = True
counter_thread.start()


@app.route("/update")
def update():
    def generate():
        while True:
            yield f"data: {counter}"
            time.sleep(0.5)

    return Response(generate(), mimetype="text/event-stream")


if __name__ == "__main__":
    app.run(debug=True)

i want to delete all previous returned counts and only show latest to have real time refresh data but idk how this was the only way i was able to do if there is a better way when i modify smth in a file or var to display the change without having to reload could someone let me know

0

There are 0 best solutions below