Skip to content
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

upload app to use radix components, hook up progress bar #202

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chakra_apps/crm/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
reflex>=0.3.8
reflex>=0.4.0a1
80 changes: 0 additions & 80 deletions chakra_apps/upload/upload/upload.py

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
109 changes: 109 additions & 0 deletions upload/upload/upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import os
from pathlib import Path
from typing import List

import reflex as rx


class State(rx.State):
"""The app state."""

# Whether we are currently uploading files.
is_uploading: bool

# Progress visuals
upload_progress: int

@rx.var
def files(self) -> list[str]:
"""Get the string representation of the uploaded files."""
return [
"/".join(p.parts[1:])
for p in Path(rx.get_upload_dir()).rglob("*")
if p.is_file()
]

async def handle_upload(self, files: List[rx.UploadFile]):
"""Handle the file upload."""
# Iterate through the uploaded files.
for file in files:
upload_data = await file.read()
outfile = Path(rx.get_upload_dir()) / file.filename.lstrip("/")
outfile.parent.mkdir(parents=True, exist_ok=True)
outfile.write_bytes(upload_data)

def on_upload_progress(self, prog: dict):
print("Got progress", prog)
if prog["progress"] < 1:
self.is_uploading = True
else:
self.is_uploading = False
self.upload_progress = round(prog["progress"] * 100)

def cancel_upload(self, upload_id: str):
self.is_uploading = False
return rx.cancel_upload(upload_id)


color = "rgb(107,99,246)"
upload_id = "upload1"


def index():
return rx.vstack(
rx.upload(
rx.vstack(
rx.button(
"Select File(s)",
height="70px",
width="200px",
color=color,
bg="white",
border=f"1px solid {color}",
),
rx.text(
"Drag and drop files here or click to select files",
height="100px",
width="200px",
),
rx.cond(
rx.selected_files(upload_id),
rx.foreach(
rx.selected_files(upload_id),
rx.text,
),
rx.text("No files selected"),
),
align="center",
),
id=upload_id,
border="1px dotted black",
padding="2em",
),
rx.hstack(
rx.button(
"Upload",
on_click=State.handle_upload(
rx.upload_files(
upload_id=upload_id,
on_upload_progress=State.on_upload_progress
)
),
),
),
rx.heading("Files:"),

rx.cond(
State.is_uploading,
rx.text("Uploading... ", rx.link("cancel", on_click=State.cancel_upload(upload_id))),
),
rx.progress(value=State.upload_progress),
rx.vstack(
rx.foreach(State.files, lambda file: rx.link(file, href=rx.get_upload_url(file)))
),
align="center",
)


app = rx.App()
app.add_page(index, title="Upload")
Loading