|
import json |
|
from pathlib import Path |
|
|
|
import pandas as pd |
|
from fastapi import FastAPI |
|
from fastapi.middleware.cors import CORSMiddleware |
|
from fastapi.responses import JSONResponse |
|
|
|
|
|
app = FastAPI() |
|
|
|
origins = [ |
|
"https://pro.openbb.dev", |
|
"https://pro.openbb.co", |
|
"https://excel.openbb.co", |
|
"https://excel.openbb.dev", |
|
] |
|
|
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=origins, |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
|
|
|
|
ROOT_PATH = Path(__file__).parent.resolve() |
|
|
|
@app.get("/") |
|
def read_root(): |
|
return {"Info": "Full example for OpenBB Custom Backend"} |
|
|
|
|
|
@app.get("/csv-data") |
|
def csv_data(): |
|
"""Read mock csv data and return it as a table to your widget""" |
|
|
|
csv_file_path = "mock_data.csv" |
|
|
|
try: |
|
|
|
return pd.read_csv((ROOT_PATH / csv_file_path).open()).to_dict(orient="records") |
|
except Exception as e: |
|
|
|
error_message = f"Error reading the CSV file: {str(e)}" |
|
return JSONResponse(content={"error": error_message}, status_code=500) |
|
|
|
|