All the Polars examples show reading an Excel file from a path string.
df = pl.read_excel("docs/assets/data/path.xlsx")
But I am passing in the Excel file from a Django Form Post.
file_name = request.FILES.get('file')
df = pl.read_excel(file_name)
I am getting a "InvalidParametersError".
How do input the file to Polars from a HTML form?
or do I somehow have to get the path from the request.FILES and use that?
I am new to the whole Polars thing but very excited to try it.
thank you.
request.FILES
[Django-doc] does not store the path of the file at the server location, but an UploadedFile
[Django-doc]. Unless the file is quite large, it is typically not stored as file on the server, but in memory.
If you know for sure the uploaded files will be small, you can use to read the content of the file in memory:
file = request.FILES['file']
df = pl.read_excel(file.read())
But it is typically better to use .chunks(…)
[Django-doc] to prevent the memory getting drained with a (malicious) file, and thus read the first ~10 MiB for example.
Note: Please use
request.FILES['files']
overrequest.FILES.get('files')
. Indeed, using.get(…)
will not raise an error in case the key is missing, but if we really need to value for the associated key, and the key is thus required, this usually only will result in more trouble later in the process.