forms.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.vstack(
  6. rx.form(
  7. rx.vstack(
  8. rx.input(
  9. placeholder="First Name",
  10. id="first_name",
  11. ),
  12. rx.input(
  13. placeholder="Last Name", id="last_name"
  14. ),
  15. rx.hstack(
  16. rx.checkbox("Checked", id="check"),
  17. rx.switch("Switched", id="switch"),
  18. ),
  19. rx.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.divider(),
  29. rx.heading("Results"),
  30. rx.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.vstack(
  40. rx.upload(
  41. rx.vstack(
  42. rx.button(
  43. "Select File",
  44. color=color,
  45. bg="white",
  46. border=f"1px solid {color}",
  47. ),
  48. rx.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.hstack(rx.foreach(rx.selected_files, rx.text)),
  56. rx.button(
  57. "Upload",
  58. on_click=lambda: UploadState.handle_upload(
  59. rx.upload_files()
  60. ),
  61. ),
  62. rx.button(
  63. "Clear",
  64. on_click=rx.clear_selected_files,
  65. ),
  66. rx.foreach(
  67. UploadState.img, lambda img: rx.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.box(
  97. rx.vstack(
  98. rx.heading(
  99. "Forms Demo",
  100. font_size="3em",
  101. ),
  102. rx.vstack(
  103. rx.form(
  104. rx.vstack(
  105. rx.input(
  106. placeholder="First Name",
  107. id="first_name",
  108. ),
  109. rx.input(placeholder="Last Name", id="last_name"),
  110. rx.hstack(
  111. rx.checkbox("Checked", id="check"),
  112. rx.switch("Switched", id="switch"),
  113. ),
  114. rx.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.divider(),
  125. rx.heading("Results"),
  126. rx.text(FormState.form_data.to_string()),
  127. width="100%",
  128. ),
  129. rx.tabs(
  130. rx.tab_list(
  131. rx.tab("Code", style=tab_style),
  132. rx.tab("State", style=tab_style),
  133. padding_x=0,
  134. ),
  135. rx.tab_panels(
  136. rx.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.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.heading("Upload Example", font_size="3em"),
  165. rx.text("Try uploading some images and see how they look."),
  166. rx.vstack(
  167. rx.upload(
  168. rx.vstack(
  169. rx.button(
  170. "Select File",
  171. color=color,
  172. bg="white",
  173. border=f"1px solid {color}",
  174. ),
  175. rx.text("Drag and drop files here or click to select files"),
  176. ),
  177. border=f"1px dotted {color}",
  178. padding="5em",
  179. ),
  180. rx.hstack(rx.foreach(rx.selected_files, rx.text)),
  181. rx.button(
  182. "Upload",
  183. on_click=lambda: UploadState.handle_upload(rx.upload_files()),
  184. ),
  185. rx.button(
  186. "Clear",
  187. on_click=rx.clear_selected_files,
  188. ),
  189. rx.foreach(
  190. UploadState.img,
  191. lambda img: rx.image(
  192. src=img,
  193. width="20%",
  194. height="auto",
  195. ),
  196. ),
  197. padding="5em",
  198. width="100%",
  199. ),
  200. rx.tabs(
  201. rx.tab_list(
  202. rx.tab("Code", style=tab_style),
  203. rx.tab("State", style=tab_style),
  204. padding_x=0,
  205. ),
  206. rx.tab_panels(
  207. rx.tab_panel(
  208. rx.code_block(
  209. forms_2_code,
  210. language="python",
  211. show_line_numbers=True,
  212. ),
  213. width="100%",
  214. padding_x=0,
  215. padding_y=".25em",
  216. ),
  217. rx.tab_panel(
  218. rx.code_block(
  219. forms_2_state,
  220. language="python",
  221. show_line_numbers=True,
  222. ),
  223. width="100%",
  224. padding_x=0,
  225. padding_y=".25em",
  226. ),
  227. width="100%",
  228. ),
  229. variant="unstyled",
  230. color_scheme="purple",
  231. align="end",
  232. width="100%",
  233. padding_top=".5em",
  234. ),
  235. style=template_content_style,
  236. ),
  237. style=template_page_style,
  238. )