Skip to content

Commit

Permalink
initial file upload support (#31)
Browse files Browse the repository at this point in the history
* initial file upload support
  • Loading branch information
ogrodnek authored Aug 4, 2024
1 parent e26341d commit 36bc887
Show file tree
Hide file tree
Showing 13 changed files with 1,119 additions and 272 deletions.
10 changes: 10 additions & 0 deletions examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CheckboxLiveView,
PresenceLiveView,
MapLiveView,
FileUploadDemoLiveView,
)

app = PyView()
Expand Down Expand Up @@ -147,6 +148,15 @@ def content_wrapper(_context, content: Markup) -> Markup:
forth between the liveview and the JS library.
""",
),
(
"/file_upload",
FileUploadDemoLiveView,
"File Upload",
"file_upload",
"""
File upload example, with previews and progress bars.
""",
),
]

for path, view, _, _, _ in routes:
Expand Down
2 changes: 2 additions & 0 deletions examples/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
start:
PYVIEW_SECRET=`openssl rand -base64 16` poetry run uvicorn examples.app:app --reload
101 changes: 99 additions & 2 deletions examples/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package-mode = false
python = "^3.9.16"
pyview-web = {path = "..", develop = true}
aiohttp = "^3.9.4"
pillow = "^10.4.0"


[build-system]
Expand Down
2 changes: 2 additions & 0 deletions examples/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .checkboxes import CheckboxLiveView
from .presence import PresenceLiveView
from .maps import MapLiveView
from .file_upload import FileUploadDemoLiveView

__all__ = [
"CountLiveView",
Expand All @@ -27,4 +28,5 @@
"CheckboxLiveView",
"PresenceLiveView",
"MapLiveView",
"FileUploadDemoLiveView",
]
48 changes: 48 additions & 0 deletions examples/views/file_upload/file_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import collections
import base64
from PIL import Image
from io import BytesIO


class FileEntry:
file_name: str
file_bytes: bytes
content_type: str

def __init__(self, file_name, file_path, content_type):
self.file_name = file_name
self.content_type = content_type
self.file_bytes = create_thumbnail_bytes(file_path)

@property
def base64(self):
return base64.b64encode(self.file_bytes).decode("utf-8")

@property
def inline_image(self):
return f"data:{self.content_type};base64,{self.base64}"


class FileRepository:
"""
A simple repository to store file entries thumbnails as base64 strings for demo purposes.
"""
def __init__(self):
self.all_files = collections.deque(maxlen=10)

def add_file(self, file_name, file_path, content_type):
file_entry = FileEntry(file_name, file_path, content_type)
self.all_files.append(file_entry)
return file_entry

def get_all_files(self):
return self.all_files


def create_thumbnail_bytes(input_image_path, size=(128, 128)):
with Image.open(input_image_path) as img:
img.thumbnail(size)
byte_io = BytesIO()
img.save(byte_io, "JPEG")
byte_io.seek(0)
return byte_io.getvalue()
Loading

0 comments on commit 36bc887

Please sign in to comment.