diff options
author | Pratyush Shukla <ps4534@nyu.edu> | 2024-03-11 16:17:16 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-11 10:47:16 +0000 |
commit | 782f8d19fbd582c536029b9b9ab934fa00c749cb (patch) | |
tree | 93fdd0f6a0bced19cca36ea278a00fdf183d93b5 | |
parent | 3ea6bcf1a634710a159e52d0dde7828751ce450f (diff) |
added code to get new examples as string (#6181)
-rw-r--r-- | website/generate_platform_v4_markdown.py | 85 |
1 files changed, 54 insertions, 31 deletions
diff --git a/website/generate_platform_v4_markdown.py b/website/generate_platform_v4_markdown.py index 66842d771f4..f50a509e6a8 100644 --- a/website/generate_platform_v4_markdown.py +++ b/website/generate_platform_v4_markdown.py @@ -7,11 +7,12 @@ import json import re import shutil from pathlib import Path -from typing import Any, Callable, Dict, List +from typing import Any, Callable, Dict, List, Optional +from openbb_core.app.model.example import Example from openbb_core.app.provider_interface import ProviderInterface from openbb_core.app.router import RouterLoader -from openbb_core.app.static.package_builder import MethodDefinition +from openbb_core.app.static.package_builder import DocstringGenerator, MethodDefinition from openbb_core.provider import standard_models from pydantic_core import PydanticUndefined @@ -77,6 +78,42 @@ def get_field_data_type(field_type: Any) -> str: return field_type +def get_endpoint_examples( + path: str, + func: Callable, + examples: Optional[List[Example]], +) -> str: + """Get the examples for the given standard model or function. + + For a given standard model or function, the examples are fetched from the + list of Example objects and formatted into a string. + + Args: + path (str): Path of the router. + func (Callable): Router endpoint function. + examples (Optional[List[Example]]): List of Examples (APIEx or PythonEx type) + for the endpoint. + + Returns: + str: Formatted string containing the examples for the endpoint. + """ + sig = inspect.signature(func) + parameter_map = dict(sig.parameters) + formatted_params = MethodDefinition.format_params( + path=path, parameter_map=parameter_map + ) + explicit_params = dict(formatted_params) + explicit_params.pop("extra_params", None) + param_types = {k: v.annotation for k, v in explicit_params.items()} + + return DocstringGenerator.build_examples( + path.replace("/", "."), + param_types, + examples, + "website", + ) + + def get_provider_parameter_info(endpoint: Callable) -> Dict[str, str]: """Get the name, type, description, default value and optionality information for the provider parameter. @@ -151,16 +188,18 @@ def get_provider_field_params( ) # fmt: skip # Add information for the providers supporting multiple symbols - if params_type == "QueryParams" and field_info.json_schema_extra: - multiple_items = ", ".join( - field_info.json_schema_extra["multiple_items_allowed"] - ) - cleaned_description += ( - f" Multiple items allowed for provider(s): {multiple_items}." - ) - # Manually setting to List[<field_type>] for multiple items - # Should be removed if TYPE_EXPANSION is updated to include this - field_type = f"Union[{field_type}, List[{field_type}]]" + if params_type == "QueryParams" and ( + field_extra := field_info.json_schema_extra + ): + multiple_items_list = field_extra.get("multiple_items_allowed", None) + if multiple_items_list: + multiple_items = ", ".join(multiple_items_list) + cleaned_description += ( + f" Multiple items allowed for provider(s): {multiple_items}." + ) + # Manually setting to List[<field_type>] for multiple items + # Should be removed if TYPE_EXPANSION is updated to include this + field_type = f"Union[{field_type}, List[{field_type}]]" default_value = "" if field_info.default is PydanticUndefined else str(field_info.default) # fmt: skip @@ -339,7 +378,8 @@ def generate_reference_file() -> None: reference[path]["description"] = re.sub(" +", " ", description) # Add endpoint examples - reference[path]["examples"] = route.openapi_extra.get("examples", []) + examples = route.openapi_extra["examples"] + reference[path]["examples"] = get_endpoint_examples(path, route_func, examples) # Add endpoint parameters fields for standard provider if route_method == {"GET"}: @@ -507,21 +547,6 @@ def create_reference_markdown_intro( return markdown -def create_reference_markdown_examples(examples: List[str]) -> str: - """Create the example section for the markdown file. - - Args: - examples (List[str]): List of examples - - Returns: - str: Example section for the markdown file - """ - - examples_str = "from openbb import obb\n" + "\n".join(examples) - markdown = f"Example\n-------\n\n```python\n{examples_str}\n```\n\n" - return markdown - - def create_reference_markdown_tabular_section( parameters: Dict[str, List[Dict[str, str]]], heading: str ) -> str: @@ -965,9 +990,7 @@ def generate_platform_markdown() -> None: reference_markdown_content += create_reference_markdown_intro( path[1:], description, path_data["deprecated"] ) - reference_markdown_content += create_reference_markdown_examples( - path_data["examples"] - ) + reference_markdown_content += path_data["examples"] if path_parameters_fields := path_data["parameters"]: reference_markdown_content += create_reference_markdown_tabular_section( |