export results to csv
This commit is contained in:
@@ -3,12 +3,14 @@ import numpy as np
|
||||
import re
|
||||
import requests
|
||||
import os
|
||||
from fastapi import APIRouter, Security, Depends
|
||||
from fastapi import APIRouter, Security, Depends, HTTPException, Response
|
||||
from fastapi.responses import JSONResponse
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database import get_db, engine, Base
|
||||
from app.security import get_current_user
|
||||
from app.crud import create_song, get_setting, set_setting, get_all_songs_and_votes
|
||||
from typing import Any
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/admin",
|
||||
@@ -130,5 +132,13 @@ async def toggle_veto_mode(db: Session = Depends(get_db)) -> bool:
|
||||
|
||||
|
||||
@router.get("/results")
|
||||
async def get_evaluation(db = Depends(get_db)) -> list:
|
||||
return get_all_songs_and_votes(db)
|
||||
async def get_evaluation(format: str = "json", db = Depends(get_db)) -> Any:
|
||||
res = get_all_songs_and_votes(db)
|
||||
if format == "json":
|
||||
return JSONResponse(content=res)
|
||||
elif format == "csv":
|
||||
df = pd.json_normalize(res)
|
||||
df.columns = [c.split(".")[-1] for c in df.columns]
|
||||
return Response(content=df.to_csv(), media_type="application/csv", headers={'Content-Disposition': 'attachment; filename="results.csv"'})
|
||||
else:
|
||||
raise HTTPException(status_code=404, detail="format must be json or csv")
|
||||
|
||||
Reference in New Issue
Block a user