-
Notifications
You must be signed in to change notification settings - Fork 533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an example of using Django's file storage API to open files #3997
base: main
Are you sure you want to change the base?
Conversation
All contributors have signed the CLA ✍️ ✅ |
I have read the CLA Document and I hereby sign the CLA |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a bit more to this example which explains which Django libraries require to be installed (and how to from pipit I assume)? ( i.e. A basic setup for someone who doesn't know Django would be good ). I would like to be able to test this but don't know Django :)
import pymupdf | ||
from django.core.files.storage import default_storage | ||
|
||
from .models import MyModel |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does "MyModel" come from - is that part of Django?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, so MyModel
is my made up representation of a database table.
The set up is definitely non-trivial. It involves installing packages ( I did create a barebones project if you'd like to test it out for yourself with your own API keys: https://github.com/lancegoyke/pymupdf-django Perhaps this is more than we care to add in here and the user can fend for her or himself. The Based on the last hour or two of work on that repo I pasted above, I might recommend we not try to tackle helping the user setup object storage for their project. Let me know if this is helpful at all or if there's something else I can try to clarify. |
---------- | ||
|
||
|
||
Opening Django Files |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually better as: "Opening Files from the Django Storage Area" - as we are still opening PDFs etc. that might be stored there right? ( i.e. we are not opening "Django system files" )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are totally correct. I'm embarrassed to see that you saw the original title!
The convention in Django is to call these "media files" or "user-uploaded media". How do you feel about "Opening Media Files in a Django Application"?
Django implements a `File Storage API <https://docs.djangoproject.com/en/5.1/ref/files/storage/>`_ to store files. The default is the `FileSystemStorage <https://docs.djangoproject.com/en/5.1/ref/files/storage/#the-filesystemstorage-class>`_, but the `django-storages <https://django-storages.readthedocs.io/en/latest/index.html>`_ library provides a number of other storage backends. | ||
|
||
You can open the file, move the contents into memory, then pass the contents to |PyMuPDF| as a stream. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about including a standout note here? Something like:
.. note::
This assumes some knowledge and familiarity with Django and that you have a Django project in place.
As I appreciate setting up Django is non-trivial - in this case I think we should just add some further notes about Django and assumed knowledge around it. I know Python developers generally love Dango so I think having this kind of example is valuable! |
I have been working with pymupdf inside of a Django project and thought it might be useful to document how to use Django's File Storage API to write code using pymupdf that works with a local filesystem and a remote storage backend like S3.
I was able to figure out what to do without these docs based on what is already there, so feel free to close this if you don't think it's necessary.
Also, I have not tested that the docs will build yet. Wasn't sure how to do that and I'm out of time at the moment.