forked from datapane/be-hiring-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
97 lines (81 loc) · 3.48 KB
/
Copy pathserver.py
File metadata and controls
97 lines (81 loc) · 3.48 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
86
87
88
89
90
91
92
93
94
95
96
97
from fastapi import FastAPI, File, UploadFile
from fastapi import Response
from fastapi.responses import JSONResponse, FileResponse
import pandas as pd
from pandas import DataFrame
import uuid
# import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import plt
app = FastAPI()
datasets = {}
# It works
@app.get("/")
def home():
return {"message": "It works!"}
# return Response(content = "It works!", media_type = "text/plain")
# return the list of datasets
@app.get("/datasets")
def list_dataset():
if len(datasets) == 0: return {"message": "no_datasets"}
filename_id = {datasets[i]["filename"][0]: str(i) for i in datasets}
return {"message": "datasets_list", "datasets": filename_id}
# return Response(content = datasets, media_type = "application/json")
# clear all datasets
@app.delete("/datasets")
def clear_all_datasets():
datasets.clear()
return {"message": "datasets_cleared"}
# return Response(headers = "datasets_cleared", media_type = "application/json")
# create a dataset
@app.post("/datasets")
def create_dataset(dataset: UploadFile = File(...)):
contents = dataset.file
df = pd.read_csv(contents)
df["filename"] = dataset.filename
dataset_id = uuid.uuid4()
datasets[dataset_id] = df
return {"message": "dataset_created", "dataset_id": dataset_id}
# return Response(headers = "dataset_created", content = {"dataset_id": dataset_id}, media_type = "application/json")
# delete a dataset
@app.delete("/datasets/{dataset_id}")
def delete_dataset(dataset_id: uuid.UUID):
if dataset_id not in datasets: return {"message": "dataset_not_found"}
del datasets[dataset_id]
return {"message": "dataset_deleted"}
# return Response(content = "dataset_deleted", media_type = "text/plain")
# return the file name, and size of the dataset object
@app.get("/datasets/{dataset_id}")
def info_dataset(dataset_id: uuid.UUID):
if dataset_id not in datasets: return {"message": "dataset_not_found"}
return {"message": "dataset_info", "filename": datasets[dataset_id]["filename"][0], "size": datasets[dataset_id].shape[0]}
# return Response(header = "dataset_info",
# content = {"filename": datasets[dataset_id]["filename"][0], "size": datasets[dataset_id].shape[0]},
# media_type = "application/json")
# return the dataset in excel format
@app.get("/datasets/{dataset_id}/excel")
def excel_dataset(dataset_id: uuid.UUID):
if dataset_id not in datasets: return {"message": "dataset_not_found"}
return {"message": "dataset_excel", "dataset_excel": datasets[dataset_id].to_csv()}
# return the stats of the dataset
@app.get("/datasets/{dataset_id}/stats")
def stats_dataset(dataset_id: uuid.UUID):
if dataset_id not in datasets: return {"message": "dataset_not_found"}
return {"message": "dataset_stats", "stats": datasets[dataset_id].describe()}
# return the dataset in pdf format
@app.get("/datasets/{dataset_id}/plot")
def plot_dataset(dataset_id: uuid.UUID):
if dataset_id not in datasets: return {"message": "dataset_not_found"}
df = datasets[dataset_id]
tmp = "plt.pdf"
plt.plot_hist(df, tmp)
# pp = PdfPages(tmp)
# for col in df.select_dtypes(include="number").columns:
# plt.hist(df[col], bins=10)
# plt.title(f"Histogram of {col}")
# plt.xlabel(col)
# plt.ylabel("Frequency")
# pp.savefig(plt.gcf())
# plt.clf()
# pp.close()
return FileResponse(tmp, media_type="application/pdf")