-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #503 from lona-web-org/fscherf/setup-buckets
setup buckets for file-uploads
- Loading branch information
Showing
23 changed files
with
1,333 additions
and
38 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
is_template: False | ||
|
||
|
||
Buckets | ||
======= | ||
|
||
The bucket API implements the server-side of file-uploads and can be used to | ||
make files accessible over HTTP. It does not implement the browser-side, so it | ||
is best used with a library like | ||
`lona-dropzone </demos/file-upload/index.html>`_. | ||
|
||
A bucket is basically a temporary directory, that gets created when a bucket | ||
is initialized with a request object, and closed automatically when the view, | ||
associated with the request, gets removed from the server. | ||
|
||
|
||
Usage | ||
----- | ||
|
||
.. code-block:: python | ||
from lona import View, Bucket, HTML, A | ||
class BucketView(View): | ||
def handle_request(self, request): | ||
self.bucket = Bucket( | ||
request=request, | ||
on_add=self.on_add, # optional | ||
on_delete=self.on_delete, # optional | ||
) | ||
return HTML( | ||
A( | ||
'Bucket' | ||
href=self.bucket.get_url(), # link to the bucket | ||
target='_blank', # open link in new tab | ||
interactive=False, | ||
), | ||
) | ||
def on_add(self, file_names): | ||
# this method gets called whenever a file gets added (uploaded) | ||
# `file_names` is a list of strings | ||
pass | ||
def on_delete(self, file_names): | ||
# this method gets called whenever a file gets deleted | ||
# `file_names` is a list of strings | ||
pass | ||
Regardless of the URL of the view that opened the bucket, all buckets are | ||
accessible at ``/buckets/<request_id>/<bucket_id>``. | ||
|
||
To upload a file, issue a multipart POST request to | ||
``/buckets/<request_id>/<bucket_id>/add``. | ||
|
||
To delete a file, issue a POST form request to | ||
``/buckets/<request_id>/<bucket_id>/delete``. The bucket will search for the | ||
form key ``name``. | ||
|
||
The URL prefix (``/buckets/``) can be changed using | ||
``settings.BUCKETS_URL_PREFIX``. | ||
|
||
When ``Bucket.index`` is enabled, a generic frontend for listing, adding, and | ||
deleting files for a bucket is available at | ||
``/buckets/<request_id>/<bucket_id>``. | ||
|
||
.. image:: bucket-index.png | ||
|
||
|
||
Arguments | ||
--------- | ||
|
||
.. api-doc:: lona.Bucket.__init__ | ||
|
||
|
||
Methods | ||
------- | ||
|
||
.. api-doc:: lona.Bucket.get_path | ||
.. api-doc:: lona.Bucket.get_file_names | ||
.. api-doc:: lona.Bucket.get_size | ||
.. api-doc:: lona.Bucket.get_url | ||
.. api-doc:: lona.Bucket.get_add_url | ||
.. api-doc:: lona.Bucket.get_delete_url | ||
|
||
|
||
Customization | ||
------------- | ||
|
||
The Bucket index page can be customized by overriding the template | ||
``lona/bucket.html``. | ||
|
||
.. code-block:: html | ||
:include: ../../../lona/templates/lona/bucket.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from datetime import datetime | ||
|
||
from lona_picocss.html import ScrollerPre, HTML, Grid, Div, H1, A | ||
from lona_picocss import install_picocss | ||
from lona_dropzone import Dropzone | ||
|
||
from lona import View, App | ||
|
||
app = App(__file__) | ||
|
||
install_picocss(app, debug=True) | ||
|
||
|
||
@app.route('/') | ||
class DropzoneView(View): | ||
def handle_request(self, request): | ||
self.dropzone = Dropzone( | ||
request=request, | ||
on_add=self.on_add, | ||
on_delete=self.on_delete, | ||
) | ||
|
||
self.scroller = ScrollerPre(height='30em') | ||
|
||
return HTML( | ||
H1('Dropzone'), | ||
Grid( | ||
Div( | ||
self.dropzone, | ||
), | ||
Div( | ||
self.scroller, | ||
A( | ||
'Bucket URL', | ||
href=self.dropzone.bucket.get_url(), | ||
target='_blank', | ||
interactive=False, | ||
), | ||
), | ||
), | ||
) | ||
|
||
def on_add(self, file_names): | ||
# this method gets called whenever a file gets added (uploaded) | ||
|
||
self.scroller.append( | ||
f'{datetime.now()}: on_add: {file_names=}\n', | ||
) | ||
|
||
self.show() | ||
|
||
def on_delete(self, file_names): | ||
# this method gets called whenever a file gets deleted | ||
|
||
self.scroller.append( | ||
f'{datetime.now()}: on_delete: {file_names=}\n', | ||
) | ||
|
||
self.show() | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
|
||
File Upload | ||
=========== | ||
|
||
This demo showcases a file-uploads, using | ||
:link:`lona-dropzone <https://github.com/lona-web-org/lona-dropzone>`, which | ||
uses the :link:`bucket API </api-reference/buckets.rst>` internally. | ||
|
||
.. image:: demo.gif | ||
|
||
|
||
Install Dependencies | ||
-------------------- | ||
|
||
.. code-block:: text | ||
pip install lona lona-picocss | ||
Source code | ||
----------- | ||
|
||
.. code-block:: python | ||
:include: demo.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.