upload.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """A file upload component."""
  2. from typing import Dict, List, Optional
  3. from pynecone.components.component import EVENT_ARG, Component
  4. from pynecone.components.forms.input import Input
  5. from pynecone.components.layout.box import Box
  6. from pynecone.event import EventChain
  7. from pynecone.vars import BaseVar, Var
  8. upload_file = BaseVar(name="e => File(e)", type_=EventChain)
  9. class Upload(Component):
  10. """A file upload component."""
  11. library = "react-dropzone"
  12. tag = "ReactDropzone"
  13. is_default = True
  14. # The list of accepted file types. This should be a dictionary of MIME types as keys and array of file formats as
  15. # values.
  16. # supported MIME types: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
  17. accept: Var[Optional[Dict[str, List]]]
  18. # Whether the dropzone is disabled.
  19. disabled: Var[bool]
  20. # The maximum number of files that can be uploaded.
  21. max_files: Var[int]
  22. # The maximum file size (bytes) that can be uploaded.
  23. max_size: Var[int]
  24. # The minimum file size (bytes) that can be uploaded.
  25. min_size: Var[int]
  26. # Whether to allow multiple files to be uploaded.
  27. multiple: Var[bool] = True # type: ignore
  28. # Whether to disable click to upload.
  29. no_click: Var[bool]
  30. # Whether to disable drag and drop.
  31. no_drag: Var[bool]
  32. # Whether to disable using the space/enter keys to upload.
  33. no_keyboard: Var[bool]
  34. @classmethod
  35. def create(cls, *children, **props) -> Component:
  36. """Create an upload component.
  37. Args:
  38. children: The children of the component.
  39. props: The properties of the component.
  40. Returns:
  41. The upload component.
  42. """
  43. # get only upload component props
  44. supported_props = cls.get_props()
  45. upload_props = {
  46. key: value for key, value in props.items() if key in supported_props
  47. }
  48. # The file input to use.
  49. upload = Input.create(type_="file")
  50. upload.special_props = {BaseVar(name="{...getInputProps()}", type_=None)}
  51. # The dropzone to use.
  52. zone = Box.create(
  53. upload,
  54. *children,
  55. **{k: v for k, v in props.items() if k not in supported_props}
  56. )
  57. zone.special_props = {BaseVar(name="{...getRootProps()}", type_=None)}
  58. # Create the component.
  59. return super().create(zone, on_drop=upload_file, **upload_props)
  60. def get_controlled_triggers(self) -> Dict[str, Var]:
  61. """Get the event triggers that pass the component's value to the handler.
  62. Returns:
  63. A dict mapping the event trigger to the var that is passed to the handler.
  64. """
  65. return {
  66. "on_drop": EVENT_ARG,
  67. }
  68. def _render(self):
  69. out = super()._render()
  70. out.args = ("getRootProps", "getInputProps")
  71. return out