Reputation: 617
Here's an example:
Suppose a user, let's say 'Avi' is using my web app. Avi uploads a file (say and .xls/.xlsx) to my web app. I use django-storages to upload the files to either S3 or Azure. The problem arises when I create a dataframe from the uploaded file. When I export the dataframe, it does not get uploaded but rather gets exported to the location of the project.
Current Solution: I use azure, boto3 api for converting the dataframe to a stream of bytes and then uploading to the corresponding container or s3 bucket.
Is there a easier way where I could pass the newly created dataframe through a serializer so it gets uploaded to the respective location?
Upvotes: 0
Views: 517
Reputation: 617
After some digging and searching all over the internet. I've found a better solution to the existing one. I'll try to put down a detailed snippet! Using SimpleUploadedFile which is a part of django.core.files.uploadedfile solves most of the problem and makes the code much easier to read.
Here's a snippet:
# Other imports
from django.core.files.uploadedfile import SimpleUploadedFile
import pandas as pd
import io
df = pd.DataFrame()
# Do something with dataframe
# After processing the dataframe do the following
output_stream = io.BytesIO()
df.to_excel(output_stream, index=False)
output_file = SimpleUploadedFile('sample.xlsx', output_stream.getvalue())
data = { 'record_type': 'OUTPUT_FILE', 'record':output_file }
record_serializer = RecordSerializer(data=data)
if record_serializer.is_valid():
record_serializer.save()
# Display data from the serializer/ Return serialized data
else:
# Return an exception or error from the serializer
Do let me know if it works for any of you who've tried this. Also, since you're passing the file as a param to the serialzer, no matter if you're using default local storage or configures S3, Azure etc using django-storages, the file will be uploaded to your corresponding location without the hassle of calling the API to upload files created by python/django every now and then.
Upvotes: 1