grist-core
grist-core copied to clipboard
Allow definition of custom functions / classes at document level
This is an idea I had several times, but which is very well illustrated by this case: it’s sometimes necessary to use custom functions / classes, and while they can be defined within column formulas, this leads to code duplication. It would be very handy if we had a way to add python code that would go to the top of the code, just after imports:
import grist
from functions import * # global uppercase functions
import datetime, math, re # modules commonly needed in formulas
<<<HERE WOULD GO CUSTOM CODE>>>
In the case linked, this would allow to define SList at the document level, not within the function body.
While thinking about it, it would also be practical to have the option to define class functions for each table.
This would be super helpful keeping my DB functions clean and easy to update
Yes, indeed very useful
It does seems a good idea. For UI, I could imagine an editable region in the Code Viewer. The data engine would need a bit of tweaking to make it use the code, and update correctly when it changes. If anyone wants to take a shot at it, it does seem an achievable feature, though not trivial.
While it's not a solution, unfortunately, but a workaround that could be helpful for someone who found this page via googling:
https://community.getgrist.com/t/install-third-party-libraries/1087/3
TL;DR; you need to create your own Dockerfile with injecting all the necessary additional Python libraries in it.