diff --git a/demos/sse/README.md b/demos/sse/README.md deleted file mode 100644 index 16c91c991..000000000 --- a/demos/sse/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# Server Side Events Demo -This demo visualizes events from `tqdm` during a standard HTTP request. - -To start the demo, run: -```bash -python3 test_sse_display_of_tqdm.py -``` - -This will start a Flask server at http://127.0.0.1:8000/. When started, you can click on Ctrl+Click on the link printed on the console to open that page. - -Once the page opens, it should automatically connect to the SSE endpoint (`/events`) and send a request to the `tqdm` endpoint (`/tqdm`). While this is returning, you will see the progress of the `tqdm` instance print as a list on the page. diff --git a/demos/sse/templates/index.html b/demos/sse/templates/index.html deleted file mode 100644 index 775c83880..000000000 --- a/demos/sse/templates/index.html +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - Document - - - - - - diff --git a/demos/sse/test_sse_display_of_tqdm.py b/demos/sse/test_sse_display_of_tqdm.py deleted file mode 100644 index 9292dfcc0..000000000 --- a/demos/sse/test_sse_display_of_tqdm.py +++ /dev/null @@ -1,74 +0,0 @@ -import asyncio -import os -import random -import sys -import time -from typing import List - -from flask import Flask, Response, render_template -from tqdm import tqdm as base_tqdm - -SCRIPT_DIR = os.path.dirname(os.path.abspath(os.path.join(__file__, "..", "..", "src", "pyflask"))) -sys.path.append(os.path.dirname(SCRIPT_DIR)) -from pyflask.manageNeuroconv.info.sse import MessageAnnouncer - - -class SSEProgress(base_tqdm): - def update(self, n=1, always_callback=False): - if super(SSEProgress, self).update(n) or always_callback: - announcer.announce(self.format_dict) - - -# Helper Functions for the Demo -async def sleep_func(sleep_duration: float = 1) -> float: - start_time = time.time() - await asyncio.sleep(delay=sleep_duration) - end_time = time.time() - elapsed_time = end_time - start_time - - return elapsed_time - - -async def run_multiple_sleeps(sleep_durations: List[float]) -> List[float]: - tasks = [] - for sleep_duration in sleep_durations: - task = asyncio.create_task(sleep_func(sleep_duration=sleep_duration)) - tasks.append(task) - - actual_sleep_durations = [await f for f in SSEProgress(asyncio.as_completed(tasks), total=len(tasks))] - - return actual_sleep_durations - - -announcer = MessageAnnouncer() - -app = Flask(__name__) - - -@app.route("/") -def home(): - return render_template("index.html") - - -def listen(): - messages = announcer.listen() # returns a queue.Queue - while True: - msg = messages.get() # blocks until a new message arrives - yield msg - - -@app.route("/tqdm", methods=["GET"]) -def tqdm(): - n = 10**5 - sleep_durations = [random.uniform(0, 5.0) for _ in range(n)] - asyncio.run(run_multiple_sleeps(sleep_durations=sleep_durations)) - return Response() - - -@app.route("/events", methods=["GET"]) -def events(): - return Response(listen(), mimetype="text/event-stream") - - -if __name__ == "__main__": - app.run(port=8000, debug=True)