Closed othalan closed 2 years ago
I notice this suggestion was not included in the latest version (v0.2.2). Is there a different mechanism which will achieve the same result? Or could this functionality be added to the next version?
This is a tough one. Your specific solution certainly works. Personally I'd love to see a much more general approach to issues such as this one.
I no longer use JustPy in my current personal projects as I decided to write the JavaScript code myself, but I still face the same type of problem of passing short JavaScript functions from Python to JavaScript via a JSON data structure. I have in essence taken exactly what I wrote above in my original submission of this issue and made it more generic, which is quite simple:
First off, use JavaScript Function
instead of eval
. It is safer and is the current recommended practice.
Mozilla Reference: Fuction
If you want any field to be able to be compiled as JavaScript code, simply create an internal use only field in your JSON dictionary which lists other fields which should be compiled. Similarly, if you want generic fields to be able to be set in grid_def
, I would use the exact same pattern as above, just changing the special field name and associated JavaScript action.
On the Python side, you might have:
send = {
'field_name': ['a', 'b', 'return a+b'],
'_compile': ['field_name'],
'_global': ['field_name'],
}
On the JavaScript side, making things generic with the above data is equally simple. If you assume data
is the incoming JSON data structure ....
data['_compile'].forEach(field => data[field] = Function(...data[field]));
data['_global'].forEach(field => grid_def[field] = data[field]);
Back in Python, you can easily automate the creation for '_compile' and '_global' flags through any number of methods. For compiling JavaScript, I simply created a class based on list
, no customization needed. When that custom class (JSFunction
) is detected during conversion to JSON format, a special handler automatically creates the _compile
field in the dictionary. The python user code could then simplify to:
send = {
'field_name': JSGlobalFunction(['a', 'b', 'return a+b']),
}
I found this solution to be simple, elegant, and generic. I have used this same pattern in a couple of different ways to simplify my life communicating between Python and JavaScript.
I propose a few simple changes to permit use of custom cell renderers as detailed in this page:
Ag-Grid: Component Cell Renderer
Justification
While similar visual effects can be obtained by use of HTML columns, this solution has the problem of converting numeric data into text. Numbers converted to text will not filter and sort properly.
Cell Renderers are specifically designed to bypass this problem by modifying only the displayed value, not the underlying data value.
Implementation
This can be made possible with a few simple changes to existing code. A cell renderer must be implemented in javascript as the code needs to run on the client side. A location for such components already exists on the javascript side for the implementation of the checkboxRenderer already available in the code. The only change that is necessary is to provide a hook to populate the javascript
grid_def.components
variable from python.Diff based on version 0.1.5, the most recent version of code available at this time.
Use Case Example
The following code displays the "Price" column formatted in US Currency.