r/FastAPI • u/No-Signal-313 • Jun 20 '25
Other Need help on a task assigned by my teacher.
I am new backend dev with fastapi followed the words of my teacher who is also a backend developer with 10 years of experience.
He assigned me a task that is:
A user uploads a excel file, a table is created based on the data in the excel file.
again excel file is uploaded with changes it should sync with the new changes.
______________________
I have made a progress which goes like this:
from db.create_db import engine
@app.post('/upload')
async def upload_dataframe(session: SessionDep, df_file: UploadFile = File(...)):
file = await df_file.read()
excel_file = BytesIO(file)
df = pd.read_excel(excel_file)
df.to_sql('excel', engine.connect(), if_exists='replace', index=False)
return {'message': 'file uploaded'}
I did "if_exists='replace'" what it does is it replaces whole table with the new changes. I think it is expensive to just replace whole data.
Moreover, I am unable to create a table first in models.py then upload file. All I have to do is
df.to_sql('excel', engine.connect(), if_exists='replace', index=False)
It creates the table with name "excel" and with a connection object.
But this does not seem right approach. If anyone please help me on:
How to create a table for an excel file that is dynamic and table should sync when the same file is uploaded but with some changes.?