-
Notifications
You must be signed in to change notification settings - Fork 25
/
20_cell_renderer_class_example.py
executable file
·171 lines (144 loc) · 4.49 KB
/
20_cell_renderer_class_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import datetime
import numpy as np
import pandas as pd
import streamlit as st
from st_aggrid import AgGrid, GridOptionsBuilder, JsCode, GridUpdateMode
now = int(datetime.datetime.now().timestamp())
start_ts = now - 3 * 30 * 24 * 60 * 60
@st.cache_data()
def make_data():
df = pd.DataFrame(
{
"timestamp": np.random.randint(start_ts, now, 20),
"side": [np.random.choice(["buy", "sell"]) for i in range(20)],
"base": [np.random.choice(["JPY", "GBP", "CAD"]) for i in range(20)],
"quote": [np.random.choice(["EUR", "USD"]) for i in range(20)],
"amount": list(
map(
lambda a: round(a, 2),
np.random.rand(20) * np.random.randint(1, 1000, 20),
)
),
"price": list(
map(
lambda p: round(p, 5),
np.random.rand(20) * np.random.randint(1, 10, 20),
)
),
"clicked": [""] * 20,
}
)
df["cost"] = round(df.amount * df.price, 2)
df.insert(
0,
"datetime",
df.timestamp.apply(lambda ts: datetime.datetime.fromtimestamp(ts)),
)
return df.sort_values("timestamp").drop("timestamp", axis=1)
# an example based on https://www.ag-grid.com/javascript-data-grid/component-cell-renderer/#simple-cell-renderer-example
jsfnc = """
class BtnCellRenderer {
init(params) {
this.params = params;
this.eGui = document.createElement('div');
this.eGui.innerHTML = `
<span>
<button id='click-button'
class='btn-simple'
style='color: ${this.params.color}; background-color: ${this.params.background_color}'>Click!</button>
</span>
`;
this.eButton = this.eGui.querySelector('#click-button');
this.btnClickedHandler = this.btnClickedHandler.bind(this);
this.eButton.addEventListener('click', this.btnClickedHandler);
}
getGui() {
return this.eGui;
}
refresh() {
return true;
}
destroy() {
if (this.eButton) {
this.eGui.removeEventListener('click', this.btnClickedHandler);
}
}
btnClickedHandler(event) {
if (confirm('Are you sure you want to CLICK?') == true) {
if(this.params.getValue() == 'clicked') {
this.refreshTable('');
} else {
this.refreshTable('clicked');
}
console.log(this.params);
console.log(this.params.getValue());
}
}
refreshTable(value) {
this.params.setValue(value);
}
};
"""
BtnCellRenderer = JsCode(jsfnc)
df = make_data()
gb = GridOptionsBuilder.from_dataframe(df)
gb.configure_default_column(editable=True)
grid_options = gb.build()
grid_options["columnDefs"].append(
{
"field": "clicked",
"headerName": "Clicked",
"cellRenderer": BtnCellRenderer,
"cellRendererParams": {
"color": "red",
"background_color": "black",
},
}
)
st.title("Custom cellRenderer Class Example")
st.markdown(
f"""
This example uses a custom class `BtnCellRenderer`, that implements [ICellRendererComp](https://www.ag-grid.com/javascript-data-grid/component-cell-renderer/#custom-components) interface.
"""
)
with st.expander("*BtnCellRenderer* implementation", expanded=False):
st.markdown(
f"""
```javascript
{jsfnc}
```
"""
)
st.markdown(
"""
The custom renderer is then added as an extra column on gridOptions:
```python
grid_options['columnDefs'].append({
"field": "clicked",
"headerName": "Clicked",
"cellRenderer": BtnCellRenderer
})
```
clicked cells will appear in the response dataframe
"""
)
tabs = st.tabs(["AgGrid", "Response Data"])
with tabs[0]:
response = AgGrid(
df,
theme="streamlit",
key="table1",
gridOptions=grid_options,
allow_unsafe_jscode=True,
fit_columns_on_grid_load=True,
reload_data=False,
try_to_convert_back_to_original_types=False,
)
with tabs[1]:
st.write(response.data)
st.markdown("#### Clicked rows:")
clicked = response.data[response.data.clicked == "clicked"]
if clicked.empty:
st.write("Nothing was clicked")
else:
st.write(clicked)