-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
86 lines (70 loc) · 1.89 KB
/
server.py
File metadata and controls
86 lines (70 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from typing import Union
from pydantic import BaseModel
from fastapi import FastAPI
import pandas as pd
from fastapi.middleware.cors import CORSMiddleware
class SelectColumnReq(BaseModel):
data: dict
columns: list
class SelectRowsReq(BaseModel):
data: dict
start: int
end: int
class FillnaReq(BaseModel):
data: dict
method: str
origins = [
"http://localhost:3000",
"http://localhost:8000",
]
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["GET", "POST"],
allow_headers=["*"],
)
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.post("/select-columns")
def read_root(request_data: SelectColumnReq):
data = request_data.data
columns = request_data.columns
for column in data.keys():
data[column] = {k: v.strip() for k, v in data[column].items() if v.strip() != ''}
df = pd.DataFrame(data)
df = df[columns]
return_df = df.to_dict()
return {"data": return_df}
@app.post("/select-rows")
def read_root(request_data: SelectRowsReq):
data = request_data.data
start = request_data.start
end = request_data.end
df = pd.DataFrame(data)
try:
df = df[start:end]
return_df = df.to_dict()
except:
return {"error": "Something wrong with start and end"}
return {"data": return_df}
@app.post("/fillna")
def read_root(request_data: FillnaReq):
data = request_data.data
method = request_data.method
df = pd.DataFrame(data)
try:
df = df.fillna(method=method)
return_df = df.to_dict()
except:
return {"error": "Something wrong with fill na method"}
return {"data": return_df}
@app.post("/execute")
def ExecCode(code_string):
try:
result = exec(code_string)
return {"result": result}
except:
return {"error": "Code not executed"}