-
-
Notifications
You must be signed in to change notification settings - Fork 339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to add custom code in documents #1259
base: main
Are you sure you want to change the base?
Conversation
This would be a great addition - I also have complex functions I have to retype in each column. I hope the reviewers approve this and merge ! |
Thanks @XoMEX ! This is very cool. And nice to see positive feedback from others. I'd like to summarize the wishes you've listed for this feature, and add a few. A couple addressed by this PR:
Other related wishes: I'd be interested in having at least a rough idea how Grist might address these more ambitious wishes. Then we can decide whether those ideas would affect this PR to make sure it is both helpful on its own, and gets us closer to the bigger plan. |
Thanks for considering this PR. Sometimes they PRs just get lost in a limbo state. My thoughts for the related wishes:
|
I would love for this PR to land 🤩 FWIW, my dream would be to be able to edit the "code view" directly (so that I can add custom functions but also duplicate a table or a column using a simple copy-paste). Basically turning "nocode" into "code" again 🙃 (But I don't want this separate wish to block the merging of this already super useful PR !) That would let me version control my documents, write unit tests in my documents, and many other useful stuff if you want to take a more "developper" approach to building docs in Grist. |
Let me propose a different implementation, but push-back is acceptable... Instead of a string inserted into documentSettings json field, I suggest introducing a new table. Presenting here by way of examples:
The initial interface could be as you have it, and just always edit the "default" module (a single row of this table). Later, the interface could be extended to allow editing different modules. The This addresses point 3. It also lays a better groundwork for other wishes because e.g. we could extend this table with an attachment column that could have a "wheel" or some kind of packaged bunch of third-party code. And for sharing a module across documents, we'd add it to the list of benefits for syncing data across documents (since there are similar wishes for sharing/syncing a table of data, and when some day implemented, syncing a set of custom modules could piggyback on that feature). |
I'm on board with Dmitry's proposal ! |
Context
This PR adds a document setting "Custom Code".
As for the motivation, see #1258.
tl;dr: I as a Document Maintainer want to be able to define a function once in the document and use it in multiple formulas.
Proposed solution
I added a new string in the document settings. This string is inserted into the python module that is built per document.
I also added two hooks to ensure that the code is used:
load_table
: When the tables are read for the first time, the module is already built, but the code was not available. Once the document settings are loaded, the code is rebuilt.BulkUpdateRecord
: When the document settings are updated, the whole document is invalidated to use the new code. This could be optimized, but that optimization is not easy.While creating the change, I used a dev container. Feel free to keep or remove that.
Related issues
#1258
and "How do I add more python packages?"
This PR should also allow putting imports into the custom code.
Has this been tested?
I did some manual testing (which I guess should be automated):
Each time checking that the cells contain the expected value in the end.
I did not consider:
Screenshots / Screencasts