forms.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. """The settings page for the template."""
  2. import reflex as rx
  3. from ..states.form_state import FormState, UploadState
  4. from ..styles import *
  5. forms_1_code = """rx.chakra.vstack(
  6. rx.chakra.form(
  7. rx.chakra.vstack(
  8. rx.chakra.input(
  9. placeholder="First Name",
  10. id="first_name",
  11. ),
  12. rx.chakra.input(
  13. placeholder="Last Name", id="last_name"
  14. ),
  15. rx.chakra.hstack(
  16. rx.chakra.checkbox("Checked", id="check"),
  17. rx.chakra.switch("Switched", id="switch"),
  18. ),
  19. rx.chakra.button("Submit",
  20. type_="submit",
  21. bg="#ecfdf5",
  22. color="#047857",
  23. border_radius="lg",
  24. ),
  25. ),
  26. on_submit=FormState.handle_submit,
  27. ),
  28. rx.chakra.divider(),
  29. rx.chakra.heading("Results"),
  30. rx.chakra.text(FormState.form_data.to_string()),
  31. width="100%",
  32. )"""
  33. color = "rgb(107,99,246)"
  34. forms_1_state = """class FormState(rx.State):
  35. form_data: dict = {}
  36. def handle_submit(self, form_data: dict):
  37. "Handle the form submit."
  38. self.form_data = form_data"""
  39. forms_2_code = """rx.chakra.vstack(
  40. rx.upload(
  41. rx.chakra.vstack(
  42. rx.chakra.button(
  43. "Select File",
  44. color=color,
  45. bg="white",
  46. border=f"1px solid {color}",
  47. ),
  48. rx.chakra.text(
  49. "Drag and drop files here or click to select files"
  50. ),
  51. ),
  52. border=f"1px dotted {color}",
  53. padding="5em",
  54. ),
  55. rx.chakra.hstack(rx.foreach(rx.selected_files, rx.chakra.text)),
  56. rx.chakra.button(
  57. "Upload",
  58. on_click=lambda: UploadState.handle_upload(
  59. rx.upload_files()
  60. ),
  61. ),
  62. rx.chakra.button(
  63. "Clear",
  64. on_click=rx.clear_selected_files,
  65. ),
  66. rx.foreach(
  67. UploadState.img, lambda img: rx.chakra.image(src=img, width="20%", height="auto",)
  68. ),
  69. padding="5em",
  70. width="100%",
  71. )"""
  72. forms_2_state = """class UploadState(State):
  73. "The app state."
  74. # The images to show.
  75. img: list[str]
  76. async def handle_upload(
  77. self, files: list[rx.UploadFile]
  78. ):
  79. "Handle the upload of file(s).
  80. Args:
  81. files: The uploaded files.
  82. "
  83. for file in files:
  84. upload_data = await file.read()
  85. outfile = rx.get_asset_path(file.filename)
  86. # Save the file.
  87. with open(outfile, "wb") as file_object:
  88. file_object.write(upload_data)
  89. # Update the img var.
  90. self.img.append(f"/{file.filename}")"""
  91. def forms_page() -> rx.Component:
  92. """The UI for the settings page.
  93. Returns:
  94. rx.Component: The UI for the settings page.
  95. """
  96. return rx.chakra.box(
  97. rx.chakra.vstack(
  98. rx.chakra.heading(
  99. "Forms Demo",
  100. font_size="3em",
  101. ),
  102. rx.chakra.vstack(
  103. rx.chakra.form(
  104. rx.chakra.vstack(
  105. rx.chakra.input(
  106. placeholder="First Name",
  107. id="first_name",
  108. ),
  109. rx.chakra.input(placeholder="Last Name", id="last_name"),
  110. rx.chakra.hstack(
  111. rx.chakra.checkbox("Checked", id="check"),
  112. rx.chakra.switch("Switched", id="switch"),
  113. ),
  114. rx.chakra.button(
  115. "Submit",
  116. type_="submit",
  117. bg="#ecfdf5",
  118. color="#047857",
  119. border_radius="lg",
  120. ),
  121. ),
  122. on_submit=FormState.handle_submit,
  123. ),
  124. rx.chakra.divider(),
  125. rx.chakra.heading("Results"),
  126. rx.chakra.text(FormState.form_data.to_string()),
  127. width="100%",
  128. ),
  129. rx.chakra.tabs(
  130. rx.chakra.tab_list(
  131. rx.chakra.tab("Code", style=tab_style),
  132. rx.chakra.tab("State", style=tab_style),
  133. padding_x=0,
  134. ),
  135. rx.chakra.tab_panels(
  136. rx.chakra.tab_panel(
  137. rx.code_block(
  138. forms_1_code,
  139. language="python",
  140. show_line_numbers=True,
  141. ),
  142. width="100%",
  143. padding_x=0,
  144. padding_y=".25em",
  145. ),
  146. rx.chakra.tab_panel(
  147. rx.code_block(
  148. forms_1_state,
  149. language="python",
  150. show_line_numbers=True,
  151. ),
  152. width="100%",
  153. padding_x=0,
  154. padding_y=".25em",
  155. ),
  156. width="100%",
  157. ),
  158. variant="unstyled",
  159. color_scheme="purple",
  160. align="end",
  161. width="100%",
  162. padding_top=".5em",
  163. ),
  164. rx.chakra.heading("Upload Example", font_size="3em"),
  165. rx.chakra.text("Try uploading some images and see how they look."),
  166. rx.chakra.vstack(
  167. rx.upload(
  168. rx.chakra.vstack(
  169. rx.chakra.button(
  170. "Select File",
  171. color=color,
  172. bg="white",
  173. border=f"1px solid {color}",
  174. ),
  175. rx.chakra.text(
  176. "Drag and drop files here or click to select files"
  177. ),
  178. ),
  179. border=f"1px dotted {color}",
  180. padding="5em",
  181. ),
  182. rx.chakra.hstack(rx.foreach(rx.selected_files, rx.chakra.text)),
  183. rx.chakra.button(
  184. "Upload",
  185. on_click=lambda: UploadState.handle_upload(rx.upload_files()),
  186. ),
  187. rx.chakra.button(
  188. "Clear",
  189. on_click=rx.clear_selected_files,
  190. ),
  191. rx.foreach(
  192. UploadState.img,
  193. lambda img: rx.chakra.image(
  194. src=img,
  195. width="20%",
  196. height="auto",
  197. ),
  198. ),
  199. padding="5em",
  200. width="100%",
  201. ),
  202. rx.chakra.tabs(
  203. rx.chakra.tab_list(
  204. rx.chakra.tab("Code", style=tab_style),
  205. rx.chakra.tab("State", style=tab_style),
  206. padding_x=0,
  207. ),
  208. rx.chakra.tab_panels(
  209. rx.chakra.tab_panel(
  210. rx.code_block(
  211. forms_2_code,
  212. language="python",
  213. show_line_numbers=True,
  214. ),
  215. width="100%",
  216. padding_x=0,
  217. padding_y=".25em",
  218. ),
  219. rx.chakra.tab_panel(
  220. rx.code_block(
  221. forms_2_state,
  222. language="python",
  223. show_line_numbers=True,
  224. ),
  225. width="100%",
  226. padding_x=0,
  227. padding_y=".25em",
  228. ),
  229. width="100%",
  230. ),
  231. variant="unstyled",
  232. color_scheme="purple",
  233. align="end",
  234. width="100%",
  235. padding_top=".5em",
  236. ),
  237. style=template_content_style,
  238. ),
  239. style=template_page_style,
  240. )