Life and Tech Talk

Fastapi 파일 첨부 코드
from fastapi import FastAPI, UploadFile, File, Form
from pydantic import BaseModel
from typing import Optional
import pandas as pd
import io

app = FastAPI()

class UserRequest(BaseModel):
    name: str
    inputfile: Optional[UploadFile] = None

@app.post("/upload/")
async def upload_file(name: str = Form(...), inputfile: UploadFile = File(...)):
    # Read the uploaded file
    contents = await inputfile.read()
    # Convert the bytes data into a pandas DataFrame
    df = pd.read_csv(io.BytesIO(contents))
    
    # You can process the DataFrame here as needed
    print(df.head())

    # Create a UserRequest object
    user_request = UserRequest(name=name, inputfile=inputfile)    
    return {"filename": inputfile.filename, "name": user_request.name}

# Run the FastAPI application using:
# uvicorn your_module_name:app --reload
올린날: 2024년 6월 11일
주제: 코드 관련 포스팅 클릭!!

* 올린이: 마이클

* VIEW: 19       0           위키홈     게시판     수정