Repository: OpenBB-finance/OpenBBTerminal
File: openbb_platform/extensions/platform_api/openbb_platform_api/main.py (Python)

Code before:
"""OpenBB Platform API.

Launch script and widgets builder for the OpenBB Terminal Custom Backend.
"""

import json
import os

import uvicorn
from fastapi.responses import JSONResponse
from openbb_core.api.rest_api import app

from .utils.api import check_port, get_user_settings, get_widgets_json, parse_args

FIRST_RUN = True

HOME = os.environ.get("HOME") or os.environ.get("USERPROFILE")

if not HOME:
    raise ValueError("HOME or USERPROFILE environment variable not set.")

CURRENT_USER_SETTINGS = os.path.join(HOME, ".openbb_platform", "user_settings.json")
USER_SETTINGS_COPY = os.path.join(HOME, ".openbb_platform", "user_settings_backup.json")

# Load widget_exclude_filter.json if it exists
# Widget filtering is optional and can be used to exclude widgets from the widgets.json file
# You can generate this filter on OpenBB Hub: https://my.openbb.co/app/platform/widgets
WIDGET_EXCLUDE_FILTER_PATH = os.path.join(
    HOME, ".openbb_platform", "widget_exclude_filter.json"
)
if os.path.exists(WIDGET_EXCLUDE_FILTER_PATH):
    with open(WIDGET_EXCLUDE_FILTER_PATH) as f:
        try:
            widget_exclude_filter = json.load(f)
        except json.JSONDecodeError:
            widget_exclude_filter = {}
else:
    widget_exclude_filter = {}

openapi = app.openapi()
kwargs = parse_args()
build = kwargs.pop("build", True)
build = False if kwargs.pop("no-build", None) else build
login = kwargs.pop("login", False)

# We don't need the current settings,
# but we need to call the function to update, login, and/or identify the settings file.
current_settings = get_user_settings(login, CURRENT_USER_SETTINGS, USER_SETTINGS_COPY)

widgets_json = get_widgets_json(build, openapi, widget_exclude_filter)


Review comment (suggestion):
Can I test this? I currently get a 500 error on both `.co` and `.dev`.

Code after:
"""OpenBB Platform API.

Launch script and widgets builder for the OpenBB Terminal Custom Backend.
"""

import json
import os

import uvicorn
from fastapi.responses import JSONResponse
from openbb_core.api.rest_api import app

from .utils.api import check_port, get_user_settings, get_widgets_json, parse_args

FIRST_RUN = True

HOME = os.environ.get("HOME") or os.environ.get("USERPROFILE")

if not HOME:
    raise ValueError("HOME or USERPROFILE environment variable not set.")

CURRENT_USER_SETTINGS = os.path.join(HOME, ".openbb_platform", "user_settings.json")
USER_SETTINGS_COPY = os.path.join(HOME, ".openbb_platform", "user_settings_backup.json")

# Widget filtering is optional and can be used to exclude widgets from the widgets.json file
# You can generate this filter on OpenBB Hub: https://my.openbb.co/app/platform/widgets
WIDGET_SETTINGS = os.path.join(HOME, ".openbb_platform", "widget_settings.json")

kwargs = parse_args()
build = kwargs.pop("build", True)
build = False if kwargs.pop("no-build", None) else build
login = kwargs.pop("login", False)
dont_filter = kwargs.pop("no-filter", False)

if not dont_filter and os.path.exists(WIDGET_SETTINGS):
    with open(WIDGET_SETTINGS) as f:
        try:
            widget_exclude_filter = json.load(f)["exclude"]
        except json.JSONDecodeError:
            widget_exclude_filter = []
else:
    widget_exclude_filter = []

openapi = app.openapi()

# We don't need the current settings,
# but we need to call the function to update, login, and/or identify the settings file.
current_settings = get_user_settings(login, CURRENT_USER_SETTINGS, USER_SETTINGS_COPY)

widgets_json = get_widgets_json(build, openapi, widget_exclude_filter)