Skip to content

Commit ea6dc42

Browse files
authored
use on_mount for aggrid examples (#1116)
* use on_mount to load df data * fix some grids widths
1 parent c9e03a3 commit ea6dc42

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

docs/library/tables-and-data-grids/ag_grid.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,12 @@ from reflex_ag_grid import ag_grid
255255
import pandas as pd
256256

257257
class AGGridEditingState(rx.State):
258-
_data_df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv")
259-
data: list[dict] = _data_df.to_dict("records")
258+
data: list[dict] = []
259+
260+
@rx.event
261+
def load_data(self):
262+
_df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv")
263+
self.data = _df.to_dict("records")
260264

261265
@rx.event
262266
def cell_value_changed(self, row, col_field, new_value):
@@ -280,6 +284,7 @@ def ag_grid_simple_editing():
280284
row_data=AGGridEditingState.data,
281285
column_defs=column_defs,
282286
on_cell_value_changed=AGGridEditingState.cell_value_changed,
287+
on_mount=AGGridEditingState.load_data,
283288
width="100%",
284289
height="40vh",
285290
)
@@ -376,8 +381,12 @@ from reflex_ag_grid import ag_grid
376381
import pandas as pd
377382

378383
class AGGridState2(rx.State):
379-
_data_df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv")
380-
data: list[dict] = _data_df.to_dict("records")
384+
data: list[dict] = []
385+
386+
@rx.event
387+
def load_data(self):
388+
_df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv")
389+
self.data = _df.to_dict("records")
381390

382391
column_defs = [
383392
ag_grid.column_def(field="country"),
@@ -390,6 +399,7 @@ def ag_grid_state_2():
390399
id="ag_grid_state_2",
391400
row_data=AGGridState2.data,
392401
column_defs=column_defs,
402+
on_mount=AGGridState2.load_data,
393403
width="100%",
394404
height="40vh",
395405
)
@@ -594,10 +604,13 @@ def ag_grid_api_simple():
594604
id="ag_grid_basic_row_selection",
595605
row_data=df.to_dict("records"),
596606
column_defs=column_defs,
607+
width="100%",
608+
height="40vh",
597609
),
598610
rx.button("Select All", on_click=my_api.select_all()),
599611
rx.button("Deselect All", on_click=my_api.deselect_all()),
600612
spacing="4",
613+
width="100%",
601614
)
602615
```
603616

@@ -643,10 +656,13 @@ def ag_grid_api_simple2():
643656
id="ag_grid_export_and_resize",
644657
row_data=df.to_dict("records"),
645658
column_defs=column_defs,
659+
width="100%",
660+
height="40vh",
646661
),
647662
rx.button("Export", on_click=my_api.export_data_as_csv()),
648663
rx.button("Resize Columns", on_click=my_api.size_columns_to_fit()),
649664
spacing="4",
665+
width="100%",
650666
)
651667
```
652668

@@ -687,9 +703,12 @@ def ag_grid_api_argument():
687703
id="ag_grid_get_data_as_csv",
688704
row_data=df.to_dict("records"),
689705
column_defs=column_defs,
706+
width="100%",
707+
height="40vh",
690708
),
691709
rx.button("Get CSV data on backend", on_click=my_api.get_data_as_csv(callback=AGGridStateAPI.handle_get_data)),
692710
spacing="4",
711+
width="100%",
693712
)
694713
```
695714

0 commit comments

Comments
 (0)