Replies: 1 comment
-
Here is some sample code which creates a ZipFile in memory using import io
from zipfile import ZipFile
import reflex as rx
def create_zip(file_content: dict[str, bytes]) -> bytes:
with io.BytesIO() as zip_buffer:
with ZipFile(zip_buffer, 'a') as zip_file:
for file_name, content in file_content.items():
zip_file.writestr(file_name, content)
return zip_buffer.getvalue()
class State(rx.State):
_test_files: dict[str, bytes] = {
'file1.txt': b'This is content for file1',
'file2.txt': b'Content for file2',
'subfolder/file3.txt': b'Content for file3 in a subfolder'
}
def download_zip(self):
return rx.download(filename="sample.zip", data=create_zip(self._test_files))
@rx.page()
def index() -> rx.Component:
return rx.link("Download ZIP", on_click=State.download_zip)
app = rx.App() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Using builtin python standard library functions, can I create an return an archive containing multiple files and subfolders from an event handler?
Beta Was this translation helpful? Give feedback.
All reactions