Generating HDF5 and downloading

51 views
Skip to first unread message

avil...@ucsc.edu

unread,
Apr 28, 2017, 8:01:40 PM4/28/17
to Django users
Hi everybody, 

I've successfully gotten a web app that takes user data, uses that to make a query, and the outputs a CSV file. However, what I'd really like is to output an HDF5 file. I googled around and there doesn't seem to be any documentation on outputting an HD5F file from a Django web app. I tried adapting the Django example of outputting a PDF (https://docs.djangoproject.com/en/1.10/howto/outputting-pdf/) like this,


I'm making a website using the Django framework. I've successfully gotten a web app that takes user data, uses that to make a query, and the outputs a CSV file. However, what I'd really like is to output an HDF5 file. I googled around and there doesn't seem to be any documentation on outputting an HD5F file from a Django web app. I tried adapting the Django example of outputting a PDF (https://docs.djangoproject.com/en/1.10/howto/outputting-pdf/) like this,

response = HttpResponse(content_type='application/hdf5')
response['Content-Disposition'] = 'attachment; filename="test.hdf5"'

h = h5py.File(response)
h.create_dataset('Name', data=test[0].name)

but I get the following error, 

'HttpResponse' object has no attribute 'encode'



So I guess I'm misunderstanding how to use content_type in Django's HttpResponse. Does anybody have any experience with outputting HDF5 files form a Django web app or could help clarify how I might adapt the HttpResponse to work with HDF5?

 

avil...@ucsc.edu

unread,
Apr 28, 2017, 8:02:06 PM4/28/17
to Django users
Hi everybody,

I'm making my first website with Django (also my first website ever). I've successfully gotten a web app that takes user data, uses that to make a query, and the outputs a CSV file. However, what I'd really like is to output an HDF5 file. I googled around and there doesn't seem to be any documentation on outputting an HD5F file from a Django web app. I tried adapting the Django example of outputting a PDF (https://docs.djangoproject.com/en/1.10/howto/outputting-pdf/) like this,

response = HttpResponse(content_type='application/hdf5')
response['Content-Disposition'] = 'attachment; filename="test.hdf5"'

h = h5py.File(response)
h.create_dataset('Name', data=test[0].name)

but I get an error,

Antonis Christofides

unread,
May 2, 2017, 2:09:17 AM5/2/17
to django...@googlegroups.com

Hi,

Please show the full traceback.

Antonis Christofides
http://djangodeployment.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/44223100-ac75-4b12-818e-15c9b464bbab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michal Petrucha

unread,
May 2, 2017, 4:30:09 AM5/2/17
to Django users
Hi,

I've never personally used h5py, but at least from the docs [1] it
looks like it only supports writing output to real files in the local
filesystem, not into any file-like object. From a cursory look, the
documentation seems to imply that all file operations are performed by
low-level C code that doesn't know a thing about Python, which makes
it hard to support file-like Python objects.

According to the docs, the “core” file driver with backing_store=False
might do the trick, but I don't see how you could then read the byte
stream of the in-memory file afterwards. Barring that, you'll need to
create a temporary file (for example with the tempfile module [2]),
write the HDF5 file there, read it into the response, and finally
delete the temporary file.

Good luck,

Michal

[1]: http://docs.h5py.org/en/latest/high/file.html#File
[2]: https://docs.python.org/3/library/tempfile.html
signature.asc
Reply all
Reply to author
Forward
0 new messages