Skip to content

Commit 9047338

Browse files
tgberkeleyTom Gotsman
andauthored
api for ag grid (#1086)
* first version * example with a return value * updates from masen comments * update requirements.txt --------- Co-authored-by: Tom Gotsman <[email protected]>
1 parent 24dbbda commit 9047338

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

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

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,144 @@ The following props are available for `column_defs` as well as many others that
555555
- `checkbox_selection`: `bool | None`: Set to true to render a checkbox for row selection.
556556
- `cell_editor`: `AGEditors | str | None`: Provide your own cell editor component for this column's cells. (Check out the Editing section of this page for more information)
557557
- `cell_editor_params`: `dict[str, list[Any]] | None`: Params to be passed to the cellEditor component.
558+
559+
560+
561+
## Functionality you need is not available in Reflex
562+
563+
Since Reflex AG Grid is wrapping the underlying AG Grid library, there is much more functionality available that is currently not exposed in Reflex. Check out this [documentation](https://www.ag-grid.com/react-data-grid/reference/) for more information on what is available in AG Grid.
564+
565+
As Reflex does not expose all the functionality of AG Grid, you can use `ag_grid.api()`, which is hanging off the `ag_grid` namespace, to access the underlying AG Grid API. This allows you to access the full functionality of AG Grid.
566+
567+
Best practice is to create a single instance of `ag_grid.api()` with the same `id` as the `id` of the `ag_grid` component that is to be referenced, `"ag_grid_basic_row_selection"` in this first example.
568+
569+
The example below uses the `select_all()` and `deselect_all()` methods of the AG Grid API to select and deselect all rows in the grid. This method is not available in Reflex directly. Check out this [documentation](https://www.ag-grid.com/react-data-grid/grid-api/#reference-selection-selectAll) to see what the methods look like in the AG Grid docs.
570+
571+
```md alert info
572+
# Ensure that the docs are set to React tab in AG Grid
573+
```
574+
575+
```python demo exec
576+
import reflex as rx
577+
from reflex_ag_grid import ag_grid
578+
import pandas as pd
579+
580+
df = pd.read_csv(
581+
"https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv"
582+
)
583+
584+
column_defs = [
585+
ag_grid.column_def(field="country", checkbox_selection=True),
586+
ag_grid.column_def(field="pop"),
587+
ag_grid.column_def(field="continent"),
588+
]
589+
590+
def ag_grid_api_simple():
591+
my_api = ag_grid.api(id="ag_grid_basic_row_selection")
592+
return rx.vstack(
593+
ag_grid(
594+
id="ag_grid_basic_row_selection",
595+
row_data=df.to_dict("records"),
596+
column_defs=column_defs,
597+
),
598+
rx.button("Select All", on_click=my_api.select_all()),
599+
rx.button("Deselect All", on_click=my_api.deselect_all()),
600+
spacing="4",
601+
)
602+
```
603+
604+
605+
The react code for the `select_all()` event handler is `selectAll = (source?: SelectionEventSourceType) => void;`.
606+
607+
To use this in Reflex as you can see, it should be called in snake case rather than camel case. The `void` means it doesn't return anything. The `source?` indicates that it takes an optional `source` argument.
608+
609+
610+
611+
```md alert info
612+
# Another way to use the AG Grid API
613+
It is also possible to use the AG Grid API directly with the event trigger (`on_click`) of the component. This removes the need to create a variable `my_api`. This is shown in the example below. It is necessary to use the `id` of the `ag_grid` component that is to be referenced.
614+
```python
615+
rx.button("Select all", on_click=ag_grid.api(id="ag_grid_basic_row_selection").select_all()),
616+
```
617+
618+
619+
### More examples
620+
621+
The following example lets a user [export the data as a csv](https://www.ag-grid.com/javascript-data-grid/grid-api/#reference-export-exportDataAsCsv) and [adjust the size of columns to fit the available horizontal space](https://www.ag-grid.com/javascript-data-grid/grid-api/#reference-columnSizing-sizeColumnsToFit). (Try resizing the screen and then clicking the resize columns button)
622+
623+
624+
```python demo exec
625+
import reflex as rx
626+
from reflex_ag_grid import ag_grid
627+
import pandas as pd
628+
629+
df = pd.read_csv(
630+
"https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv"
631+
)
632+
633+
column_defs = [
634+
ag_grid.column_def(field="country", checkbox_selection=True),
635+
ag_grid.column_def(field="pop"),
636+
ag_grid.column_def(field="continent"),
637+
]
638+
639+
def ag_grid_api_simple2():
640+
my_api = ag_grid.api(id="ag_grid_export_and_resize")
641+
return rx.vstack(
642+
ag_grid(
643+
id="ag_grid_export_and_resize",
644+
row_data=df.to_dict("records"),
645+
column_defs=column_defs,
646+
),
647+
rx.button("Export", on_click=my_api.export_data_as_csv()),
648+
rx.button("Resize Columns", on_click=my_api.size_columns_to_fit()),
649+
spacing="4",
650+
)
651+
```
652+
653+
The react code for both of these is shown below. The key point to see is that both of these functions return `void` and therefore does not return anything.
654+
655+
`exportDataAsCsv = (params?: CsvExportParams) => void;`
656+
657+
`sizeColumnsToFit = (paramsOrGridWidth?: ISizeColumnsToFitParams | number) => void;`
658+
659+
660+
### Example with a Return Value
661+
662+
This example shows how to get the data from `ag_grid` as a [csv on the backend](https://www.ag-grid.com/javascript-data-grid/grid-api/#reference-export-getDataAsCsv). The data that was passed to the backend is then displayed as a toast with the data.
663+
664+
```python demo exec
665+
import reflex as rx
666+
from reflex_ag_grid import ag_grid
667+
import pandas as pd
668+
669+
class AGGridStateAPI(rx.State):
670+
def handle_get_data(self, data: str):
671+
yield rx.toast(f"Got CSV data: {data}")
672+
673+
df = pd.read_csv(
674+
"https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv"
675+
)
676+
677+
column_defs = [
678+
ag_grid.column_def(field="country", checkbox_selection=True),
679+
ag_grid.column_def(field="pop"),
680+
ag_grid.column_def(field="continent"),
681+
]
682+
683+
def ag_grid_api_argument():
684+
my_api = ag_grid.api(id="ag_grid_get_data_as_csv")
685+
return rx.vstack(
686+
ag_grid(
687+
id="ag_grid_get_data_as_csv",
688+
row_data=df.to_dict("records"),
689+
column_defs=column_defs,
690+
),
691+
rx.button("Get CSV data on backend", on_click=my_api.get_data_as_csv(callback=AGGridStateAPI.handle_get_data)),
692+
spacing="4",
693+
)
694+
```
695+
696+
The react code for the `get_data_as_csv` method of the AG Grid API is `getDataAsCsv = (params?: CsvExportParams) => string | undefined;`. Here the function returns a `string` (or undefined).
697+
698+
In Reflex to handle this returned value it is necessary to pass a `callback` as an argument to the `get_data_as_csv` method that will get the returned value. In this example the `handle_get_data` event handler is passed as the callback. This event handler will be called with the returned value from the `get_data_as_csv` method.

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ mistletoe>=1.2.1
1313
reflex-image-zoom>=0.0.2
1414
reflex-chat==0.0.2a1
1515
reflex_type_animation==0.0.1
16-
reflex-ag-grid==0.0.8a1
16+
reflex-ag-grid==0.0.10
1717
replicate==0.32.1
1818
reflex-pyplot==0.1.3

0 commit comments

Comments
 (0)