/
Lanmorive
/
learning_fastapi
Обзор
Документация
Войти
/
Lanmorive
/
learning_fastapi
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.py
54 строки
2 KB
Lanmorive
first proba
22 ноя 2024, 02:20
22 ноя 2024, 02:20
0ce59f6
Код
Авторство
О чём код?
import matplotlib.pyplot as plt from fastapi import FastAPI,UploadFile,HTTPException import pandas as pd from io import BytesIO from fastapi.responses import StreamingResponse app = FastAPI() data = None @app.post('/upload') async def get_data(file:UploadFile) -> dict: global data if file.content_type not in ["text/csv", "application/vnd.ms-excel"]: return HTTPException(status_code=400,detail='Плохой формат') try: if file.filename.endswith('.csv'): data =pd.read_csv(file.file,index_col = 0) elif file.filename.endswith(".xls") or file.filename.endswith(".xlsx"): data =pd.read_excel(file.file,index_col = 0) else: raise HTTPException(status_code=400, detail="Неподдерживаемое расширение файла") except: return HTTPException(status_code=400,detail = 'Неправильно загружен файл') return { 'messege':'Файл успешно загружен', 'columns':list(data.columns), 'rows_count':data.shape } @app.get('/process') async def get_paint(x:str,y:str): global data if data is None: return HTTPException(status_code=400,detail = 'Данных нет') if x not in data.columns or y not in data.columns: raise HTTPException(status_code=400, detail="Указанные колонки не найдены в данных") plt.figure(figsize= (10,6)) plt.scatter(data[x],data[y],marker = 'o') plt.xlabel(x) plt.ylabel(y) plt.title(f'Графики {y} от {x}') buf = BytesIO() plt.savefig(buf,format ='png') buf.seek(0) plt.close() return StreamingResponse(buf, media_type="image/png")