Files
OpenBB/openbb_sdk/extensions/charting/openbb_charting/builder.py
Henrique Joaquim 9348f098a5 [SDK v4] General improvements related with the charting extension (#5283)
* introducing fig on the chart model

* charting manager to expect fig and content. also, adding a 1st example of its usage for stocks/load route

* leveraging fig.show on command output

* introducing the render_chart function

* using render_chart() on the command output

* drop to_plotly_json function

* a way of removing fields from the api only

* remove references to to_plotly_json

* fix charting_router to use new convention on returning fig and content

* adjustments on the builder script

* ruff and black

* typo

* stocks_news chart implementation

* repr on chart model

* adding table style config

* removing to_plotly_json from the charting manager

* typo and a little change on the way to_table behaves so we can leverage inheritance

* introducing OpenBBFigureTable as a workarround to have tables on the sdk

* renaming to to_chart

* multiples implementation

* black
2023-08-08 15:18:57 +01:00

47 lines
1.6 KiB
Python

from inspect import getmembers, isfunction
from typing import List
from openbb_charting import charting_router
from openbb_core.app.router import RouterLoader
# refers to the root of the repo, not the root of the extension
# this script should be run from the root of the repo
CHARTING_ROUTER = "openbb_sdk/extensions/charting/openbb_charting/charting_router.py"
def get_routes() -> List[str]:
router = RouterLoader.from_extensions()
return [route.path for route in router.api_router.routes] # type: ignore
def create_charting_functions_w_routes(routes_to_add: List[str]):
with open(CHARTING_ROUTER, "a") as f:
for route in routes_to_add:
original_route = route.replace("_", "/")
f.write(
f"""
def {route}(**kwargs) -> Tuple[OpenBBFigure, Dict[str, Any]]:
raise NotImplementedError("Command `{original_route}` does not have a charting function")
"""
)
def get_charting_functions() -> List[str]:
functions = getmembers(charting_router, isfunction)
if functions:
return [function[0] for function in functions]
return []
if __name__ == "__main__":
routes = get_routes()
# format routes to be valid function names
routes = [route.replace("/", "_")[1:] for route in routes]
# get functions already in charting_router.py
known_functions = get_charting_functions()
# remove known functions from routes
routes = [route for route in routes if route not in known_functions]
# create new functions in charting_router.py
create_charting_functions_w_routes(routes)