datatable.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. """The settings page for the template."""
  2. from typing import Any
  3. import reflex as rx
  4. from reflex.components.datadisplay.dataeditor import DataEditorTheme
  5. from ..styles import *
  6. from ..webui.state import State
  7. class DataTableState(State):
  8. """Datatable state."""
  9. cols: list[Any] = [
  10. {"title": "Title", "type": "str"},
  11. {
  12. "title": "Name",
  13. "type": "str",
  14. "group": "Data",
  15. "width": 300,
  16. },
  17. {
  18. "title": "Birth",
  19. "type": "str",
  20. "group": "Data",
  21. "width": 150,
  22. },
  23. {
  24. "title": "Human",
  25. "type": "bool",
  26. "group": "Data",
  27. "width": 80,
  28. },
  29. {
  30. "title": "House",
  31. "type": "str",
  32. "group": "Data",
  33. },
  34. {
  35. "title": "Wand",
  36. "type": "str",
  37. "group": "Data",
  38. "width": 250,
  39. },
  40. {
  41. "title": "Patronus",
  42. "type": "str",
  43. "group": "Data",
  44. },
  45. {
  46. "title": "Blood status",
  47. "type": "str",
  48. "group": "Data",
  49. "width": 200,
  50. },
  51. ]
  52. data = [
  53. [
  54. "1",
  55. "Harry James Potter",
  56. "31 July 1980",
  57. True,
  58. "Gryffindor",
  59. "11' Holly phoenix feather",
  60. "Stag",
  61. "Half-blood",
  62. ],
  63. [
  64. "2",
  65. "Ronald Bilius Weasley",
  66. "1 March 1980",
  67. True,
  68. "Gryffindor",
  69. "12' Ash unicorn tail hair",
  70. "Jack Russell terrier",
  71. "Pure-blood",
  72. ],
  73. [
  74. "3",
  75. "Hermione Jean Granger",
  76. "19 September, 1979",
  77. True,
  78. "Gryffindor",
  79. "10¾' vine wood dragon heartstring",
  80. "Otter",
  81. "Muggle-born",
  82. ],
  83. [
  84. "4",
  85. "Albus Percival Wulfric Brian Dumbledore",
  86. "Late August 1881",
  87. True,
  88. "Gryffindor",
  89. "15' Elder Thestral tail hair core",
  90. "Phoenix",
  91. "Half-blood",
  92. ],
  93. [
  94. "5",
  95. "Rubeus Hagrid",
  96. "6 December 1928",
  97. False,
  98. "Gryffindor",
  99. "16' Oak unknown core",
  100. "None",
  101. "Part-Human (Half-giant)",
  102. ],
  103. [
  104. "6",
  105. "Fred Weasley",
  106. "1 April, 1978",
  107. True,
  108. "Gryffindor",
  109. "Unknown",
  110. "Unknown",
  111. "Pure-blood",
  112. ],
  113. [
  114. "7",
  115. "George Weasley",
  116. "1 April, 1978",
  117. True,
  118. "Gryffindor",
  119. "Unknown",
  120. "Unknown",
  121. "Pure-blood",
  122. ],
  123. ]
  124. code_show = """rx.hstack(
  125. rx.divider(orientation="vertical", height="100vh", border="solid black 1px"),
  126. rx.vstack(
  127. rx.box(
  128. rx.data_editor(
  129. columns=DataTableState.cols,
  130. data=DataTableState.data,
  131. draw_focus_ring=True,
  132. row_height=50,
  133. smooth_scroll_x=True,
  134. smooth_scroll_y=True,
  135. column_select="single",
  136. # style
  137. theme=DataEditorTheme(**darkTheme),
  138. width="80vw",
  139. height="80vh",
  140. ),
  141. ),
  142. rx.spacer(),
  143. height="100vh",
  144. spacing="25",
  145. ),
  146. )"""
  147. state_show = """class DataTableState(State):
  148. cols: list[Any] = [
  149. {"title": "Title", "type": "str"},
  150. {
  151. "title": "Name",
  152. "type": "str",
  153. "group": "Data",
  154. "width": 300,
  155. },
  156. {
  157. "title": "Birth",
  158. "type": "str",
  159. "group": "Data",
  160. "width": 150,
  161. },
  162. {
  163. "title": "Human",
  164. "type": "bool",
  165. "group": "Data",
  166. "width": 80,
  167. },
  168. {
  169. "title": "House",
  170. "type": "str",
  171. "group": "Data",
  172. },
  173. {
  174. "title": "Wand",
  175. "type": "str",
  176. "group": "Data",
  177. "width": 250,
  178. },
  179. {
  180. "title": "Patronus",
  181. "type": "str",
  182. "group": "Data",
  183. },
  184. {
  185. "title": "Blood status",
  186. "type": "str",
  187. "group": "Data",
  188. "width": 200,
  189. },
  190. ]"""
  191. data_show = """[
  192. ["1", "Harry James Potter", "31 July 1980", True, "Gryffindor", "11' Holly phoenix feather", "Stag", "Half-blood"],
  193. ["2", "Ronald Bilius Weasley", "1 March 1980", True,"Gryffindor", "12' Ash unicorn tail hair", "Jack Russell terrier", "Pure-blood"],
  194. ["3", "Hermione Jean Granger", "19 September, 1979", True, "Gryffindor", "10¾' vine wood dragon heartstring", "Otter", "Muggle-born"],
  195. ["4", "Albus Percival Wulfric Brian Dumbledore", "Late August 1881", True, "Gryffindor", "15' Elder Thestral tail hair core", "Phoenix", "Half-blood"],
  196. ["5", "Rubeus Hagrid", "6 December 1928", False, "Gryffindor", "16' Oak unknown core", "None", "Part-Human (Half-giant)"],
  197. ["6", "Fred Weasley", "1 April, 1978", True, "Gryffindor", "Unknown", "Unknown", "Pure-blood"],
  198. ["7", "George Weasley", "1 April, 1978", True, "Gryffindor", "Unknown", "Unknown", "Pure-blood"],
  199. ]"""
  200. darkTheme = {
  201. "accent_color": "#8c96ff",
  202. "accent_light": "rgba(202, 206, 255, 0.253)",
  203. "text_dark": "#ffffff",
  204. "text_medium": "#b8b8b8",
  205. "text_light": "#a0a0a0",
  206. "text_bubble": "#ffffff",
  207. "bg_icon_header": "#b8b8b8",
  208. "fg_icon_header": "#000000",
  209. "text_header": "#a1a1a1",
  210. "text_header_selected": "#000000",
  211. "bg_cell": "#16161b",
  212. "bg_cell_medium": "#202027",
  213. "bg_header": "#212121",
  214. "bg_header_has_focus": "#474747",
  215. "bg_header_hovered": "#404040",
  216. "bg_bubble": "#212121",
  217. "bg_bubble_selected": "#000000",
  218. "bg_search_result": "#423c24",
  219. "border_color": "rgba(225,225,225,0.2)",
  220. "drilldown_border": "rgba(225,225,225,0.4)",
  221. "link_color": "#4F5DFF",
  222. "header_font_style": "bold 14px",
  223. "base_font_style": "13px",
  224. "font_family": "Inter, Roboto, -apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Ubuntu, noto, arial, sans-serif",
  225. }
  226. darkTheme_show = """darkTheme={
  227. "accent_color": "#8c96ff",
  228. "accent_light": "rgba(202, 206, 255, 0.253)",
  229. "text_dark": "#ffffff",
  230. "text_medium": "#b8b8b8",
  231. "text_light": "#a0a0a0",
  232. "text_bubble": "#ffffff",
  233. "bg_icon_header": "#b8b8b8",
  234. "fg_icon_header": "#000000",
  235. "text_header": "#a1a1a1",
  236. "text_header_selected": "#000000",
  237. "bg_cell": "#16161b",
  238. "bg_cell_medium": "#202027",
  239. "bg_header": "#212121",
  240. "bg_header_has_focus": "#474747",
  241. "bg_header_hovered": "#404040",
  242. "bg_bubble": "#212121",
  243. "bg_bubble_selected": "#000000",
  244. "bg_search_result": "#423c24",
  245. "border_color": "rgba(225,225,225,0.2)",
  246. "drilldown_border": "rgba(225,225,225,0.4)",
  247. "link_color": "#4F5DFF",
  248. "header_font_style": "bold 14px",
  249. "base_font_style": "13px",
  250. "font_family": "Inter, Roboto, -apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Ubuntu, noto, arial, sans-serif",
  251. }"""
  252. def datatable_page() -> rx.Component:
  253. """The UI for the settings page.
  254. Returns:
  255. rx.Component: The UI for the settings page.
  256. """
  257. return rx.box(
  258. rx.vstack(
  259. rx.heading(
  260. "Data Table Demo",
  261. font_size="3em",
  262. ),
  263. rx.hstack(
  264. rx.vstack(
  265. rx.box(
  266. rx.data_editor(
  267. columns=DataTableState.cols,
  268. data=DataTableState.data,
  269. draw_focus_ring=True,
  270. row_height=50,
  271. smooth_scroll_x=True,
  272. smooth_scroll_y=True,
  273. column_select="single",
  274. # style
  275. theme=DataEditorTheme(**darkTheme),
  276. width="80vw",
  277. ),
  278. ),
  279. rx.spacer(),
  280. spacing="25",
  281. ),
  282. ),
  283. rx.tabs(
  284. rx.tab_list(
  285. rx.tab("Code", style=tab_style),
  286. rx.tab("Data", style=tab_style),
  287. rx.tab("State", style=tab_style),
  288. rx.tab("Styling", style=tab_style),
  289. padding_x=0,
  290. ),
  291. rx.tab_panels(
  292. rx.tab_panel(
  293. rx.code_block(
  294. code_show,
  295. language="python",
  296. show_line_numbers=True,
  297. ),
  298. width="100%",
  299. padding_x=0,
  300. padding_y=".25em",
  301. ),
  302. rx.tab_panel(
  303. rx.code_block(
  304. data_show,
  305. language="python",
  306. show_line_numbers=True,
  307. ),
  308. width="100%",
  309. padding_x=0,
  310. padding_y=".25em",
  311. ),
  312. rx.tab_panel(
  313. rx.code_block(
  314. state_show,
  315. language="python",
  316. show_line_numbers=True,
  317. ),
  318. width="100%",
  319. padding_x=0,
  320. padding_y=".25em",
  321. ),
  322. rx.tab_panel(
  323. rx.code_block(
  324. darkTheme_show,
  325. language="python",
  326. show_line_numbers=True,
  327. ),
  328. width="100%",
  329. padding_x=0,
  330. padding_y=".25em",
  331. ),
  332. width="100%",
  333. ),
  334. variant="unstyled",
  335. color_scheme="purple",
  336. align="end",
  337. width="100%",
  338. padding_top=".5em",
  339. ),
  340. style=template_content_style,
  341. ),
  342. style=template_page_style,
  343. )