diff options
author | hjoaquim <henriquecjoaquim@gmail.com> | 2024-05-27 16:03:47 +0100 |
---|---|---|
committer | hjoaquim <henriquecjoaquim@gmail.com> | 2024-05-27 16:03:47 +0100 |
commit | 6159c6ec9d05b083a42601610461f3d7a2545449 (patch) | |
tree | 15be7e5034f602833e32c480f451e03264304e9d | |
parent | 8d2c4ee8d1895fcd7abde52d0156c93f41bdc5ab (diff) |
working with async data on the cli
-rw-r--r-- | cli/openbb_cli/controllers/base_platform_controller.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/cli/openbb_cli/controllers/base_platform_controller.py b/cli/openbb_cli/controllers/base_platform_controller.py index ed8f0087516..116746e9844 100644 --- a/cli/openbb_cli/controllers/base_platform_controller.py +++ b/cli/openbb_cli/controllers/base_platform_controller.py @@ -6,6 +6,8 @@ from types import MethodType from typing import Dict, List, Optional import pandas as pd +from anyio.from_thread import start_blocking_portal +from fastapi.responses import StreamingResponse from openbb import obb from openbb_charting.core.openbb_figure import OpenBBFigure from openbb_cli.argparse_translator.argparse_class_processor import ( @@ -201,6 +203,23 @@ class PlatformController(BaseController): df = pd.DataFrame.from_dict(obbject, orient="columns") print_rich_table(df=df, show_index=True, title=title) + elif isinstance(obbject, StreamingResponse): + received_data = [] + + async def stream_data(obbject): + async for data in obbject.body_iterator: + received_data.append(data) + session.console.print(data) + + with start_blocking_portal() as portal: + try: + portal.start_task_soon(stream_data, obbject) + finally: + portal.call(portal.stop) + + df = pd.DataFrame(received_data) + print_rich_table(df=df, show_index=True, title=title) + elif not isinstance(obbject, OBBject): session.console.print(obbject) |