tschm commited on
Commit
cae2e25
·
verified ·
1 Parent(s): 005443e

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +47 -0
main.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from pathlib import Path
3
+
4
+ import pandas as pd
5
+ from fastapi import FastAPI
6
+ from fastapi.middleware.cors import CORSMiddleware
7
+ from fastapi.responses import JSONResponse
8
+
9
+
10
+ app = FastAPI()
11
+
12
+ origins = [
13
+ "https://pro.openbb.dev",
14
+ "https://pro.openbb.co",
15
+ "https://excel.openbb.co",
16
+ "https://excel.openbb.dev",
17
+ ]
18
+
19
+ app.add_middleware(
20
+ CORSMiddleware,
21
+ allow_origins=origins,
22
+ allow_credentials=True,
23
+ allow_methods=["*"],
24
+ allow_headers=["*"],
25
+ )
26
+
27
+
28
+ ROOT_PATH = Path(__file__).parent.resolve()
29
+
30
+ @app.get("/")
31
+ def read_root():
32
+ return {"Info": "Full example for OpenBB Custom Backend"}
33
+
34
+
35
+ @app.get("/csv-data")
36
+ def csv_data():
37
+ """Read mock csv data and return it as a table to your widget"""
38
+ # Specify the path to your CSV file
39
+ csv_file_path = "mock_data.csv"
40
+
41
+ try:
42
+ # Convert the DataFrame to a dictionary and return the data
43
+ return pd.read_csv((ROOT_PATH / csv_file_path).open()).to_dict(orient="records")
44
+ except Exception as e:
45
+ # Handle error cases here
46
+ error_message = f"Error reading the CSV file: {str(e)}"
47
+ return JSONResponse(content={"error": error_message}, status_code=500)