reflex
reflex copied to clipboard
[WIP]Yield with uploads
This PR should have uploads working with yield. The code below should work:
import reflex as rx
class State(rx.State):
img : list[str] = []
val : str = "new"
async def handle_upload(self, files : list[rx.UploadFile]):
for file in files:
upload_data = await file.read()
outfile = f".web/public/{file.filename}"
# Save the file.
with open(outfile, "wb") as file_object:
file_object.write(upload_data)
# Update the img var.
self.img.append(file.filename)
yield State.set_value("Finished uploading")
def set_value(self, val: str):
self.val = val
def upload_and_update(self, files : list[rx.UploadFile] ):
yield State.set_value("uploading value")
yield State.handle_upload(files)
def index():
return rx.center(
rx.vstack(
rx.text(State.val),
rx.upload(
rx.button("Select File"),
rx.text("Drag and drop files here or click to select files"),
border="1px dotted black",
padding="20em",
),
rx.button(
"Upload",
on_click=lambda: State.handle_upload(rx.upload_files())
),
rx.foreach(State.img, lambda img: rx.image(src=img)),
)
)
app = rx.App(state=State)
app.add_page(index)
app.compile()