grist-core icon indicating copy to clipboard operation
grist-core copied to clipboard

Allow definition of custom functions / classes at document level

Open jperon opened this issue 2 years ago • 4 comments

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.

jperon avatar Dec 31 '22 09:12 jperon

While thinking about it, it would also be practical to have the option to define class functions for each table.

jperon avatar Dec 31 '22 09:12 jperon

This would be super helpful keeping my DB functions clean and easy to update

dmw24 avatar Jan 23 '23 15:01 dmw24

Yes, indeed very useful

emanuelegissi avatar Feb 09 '24 09:02 emanuelegissi

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.

paulfitz avatar Feb 09 '24 21:02 paulfitz

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.

kamilmodest avatar Apr 08 '24 09:04 kamilmodest