from pathlib import Path from typing import Dict import requests # ruff: noqa def main() -> bool: widgets_json_url = "https://raw.githubusercontent.com/OpenBB-finance/widgets-library/main/widgets.json" response = requests.get(widgets_json_url, timeout=10) widgets_data: Dict[str, Dict[str, dict]] = response.json() # Process the data into the desired format # New all widgets new_all_widgets = {} for widget_id, widget_info in widgets_data.items(): # Skip disabled widgets if widget_info.get("disabled", False): continue category_1 = widget_info.get("category") # this is a mandatory field if category_1: # Initialize dict for category_1 within category_root if category_1 not in new_all_widgets: new_all_widgets[category_1] = {} category_2 = widget_info.get("sub_category") # OPTIONAL field if category_2: if category_2 not in new_all_widgets[category_1]: new_all_widgets[category_1][category_2] = [] new_all_widgets[category_1][category_2].append( { "name": widget_info.get("name"), "description": widget_info.get("description"), "source": widget_info.get("source"), "widgetId": widget_info.get("widgetId"), } ) else: # The CATEGORYLESS is used to indicate that the widget doesn't has a level 2 category if "CATEGORYLESS" not in new_all_widgets[category_1]: new_all_widgets[category_1]["CATEGORYLESS"] = [] new_all_widgets[category_1]["CATEGORYLESS"].append( { "name": widget_info.get("name"), "description": widget_info.get("description"), "source": widget_info.get("source"), "widgetId": widget_info.get("widgetId"), } ) else: print("SHOULD NEVER GET HERE BECAUSE CATEGORY_1 IS A MANDATORY FIELD!") website_path = Path(__file__).parent.absolute() base_path = website_path / "content" / "pro" / "widgets-library" ## Handle category_root level index_path = base_path / "index.mdx" # Check if the directory exists, if not create it if not index_path.parent.exists(): index_path.parent.mkdir(parents=True) # Create content for index file text = """# Widgets Library import NewReferenceCard from "@site/src/components/General/NewReferenceCard";
{widget['description']}.
{'**Source:** ' + ','.join(widget['source']) if widget['source'] else ''}
"""
with widget_path.open(
"w", encoding="utf-8", newline="\n"
) as widget_file:
widget_file.write(text)
else:
# Create each individual widget file
for widget in widgets:
widget_path = (
base_path
/ category_1.lower().replace(" ", "-")
/ category_2.lower().replace(" ", "-")
/ f"{widget['widgetId']}.md"
)
text = f"""---
title: {widget['name']}
description: {widget['description']}
keywords:
- OpenBB
- Terminal Pro
- Investment Research
- Widgets
- {widget['name']}
- {category_1}
- {category_2}
---
import HeadTitle from '@site/src/components/General/HeadTitle.tsx';
{widget['description']}.
{'**Source:** ' + ','.join(widget['source']) if widget['source'] else ''}
"""
with widget_path.open(
"w", encoding="utf-8", newline="\n"
) as widget_file:
widget_file.write(text)
return True
if __name__ == "__main__":
main()