App for testing dynamic routes.
()
| 15 | |
| 16 | |
| 17 | def UploadFile(): |
| 18 | """App for testing dynamic routes.""" |
| 19 | import nextpy as xt |
| 20 | |
| 21 | class UploadState(xt.State): |
| 22 | _file_data: dict[str, str] = {} |
| 23 | event_order: list[str] = [] |
| 24 | progress_dicts: list[dict] = [] |
| 25 | |
| 26 | async def handle_upload(self, files: list[xt.UploadFile]): |
| 27 | for file in files: |
| 28 | upload_data = await file.read() |
| 29 | self._file_data[file.filename or ""] = upload_data.decode("utf-8") |
| 30 | |
| 31 | async def handle_upload_secondary(self, files: list[xt.UploadFile]): |
| 32 | for file in files: |
| 33 | upload_data = await file.read() |
| 34 | self._file_data[file.filename or ""] = upload_data.decode("utf-8") |
| 35 | yield UploadState.chain_event |
| 36 | |
| 37 | def upload_progress(self, progress): |
| 38 | assert progress |
| 39 | self.event_order.append("upload_progress") |
| 40 | self.progress_dicts.append(progress) |
| 41 | |
| 42 | def chain_event(self): |
| 43 | self.event_order.append("chain_event") |
| 44 | |
| 45 | def index(): |
| 46 | return xt.vstack( |
| 47 | xt.input( |
| 48 | value=UploadState.router.session.client_token, |
| 49 | is_read_only=True, |
| 50 | id="token", |
| 51 | ), |
| 52 | xt.heading("Default Upload"), |
| 53 | xt.upload( |
| 54 | xt.vstack( |
| 55 | xt.button("Select File"), |
| 56 | xt.text("Drag and drop files here or click to select files"), |
| 57 | ), |
| 58 | ), |
| 59 | xt.button( |
| 60 | "Upload", |
| 61 | on_click=lambda: UploadState.handle_upload(xt.upload_files()), # type: ignore |
| 62 | id="upload_button", |
| 63 | ), |
| 64 | xt.box( |
| 65 | xt.foreach( |
| 66 | xt.selected_files, |
| 67 | lambda f: xt.text(f), |
| 68 | ), |
| 69 | id="selected_files", |
| 70 | ), |
| 71 | xt.button( |
| 72 | "Clear", |
| 73 | on_click=xt.clear_selected_files, |
| 74 | id="clear_button", |
no test coverage detected