[Showcase + Feature Request] Reusable user code repository

Thanks @TomNit for your kind reply. Indeed I should have provided more context. What I am trying to build is a piece of “Reusable user code”, i.e. a python function, that:

  1. is called as a trigger in a column of table S (see next point)
  2. takes as input the names of two tables S=source and D=destination and two dicts C=condition and A=action
  3. all records in D which match conditions in C are updated as specified in A
    (e.g. rows where name=John and surname=Doe are updated with found=true and year=2024)
    The fact that I would like to have S and D as strings makes, correct me if I am wrong, your suggestion of using YourTableName.lookupOne(…) inapplicable.
    I can however, and this is what I will do if my preferred option is unviable, search the records according to your suggestion and then pass those to a function which does the update.
    Thanks again!

Hi @Piergiorgio_Alotto, great to see you’re planning a RUC contribution! :smiley:

I think what you’re trying to achieve might be quite possible:

  1. To get a table by name (from S and D, respectively), use Table.get() from the RUC repo.
  2. To use lookupOne or lookupRecords with the search arguments in a dict, just unpack that using **: YourTable.lookupRecord(**your_conditions_dict).

Updating the matched records can then be done quite easily with Record.update() from the repo.

Good luck!

@TomNit thanks again. What you suggest is what I planned and mostly did (your point 1. and the Record.update from the repo). What troubles me is: can I simply substitute YourTable with S?
Without bothering you more, I will just try and keep my fingers crossed.

You’ll need to substitute YourTable with Table.get("your_table_name_as_a_string"). Then it’ll work. Happy coding!

@TomNit great everyting works! But…
The function is called from a trigger column on “new record” and the table is populated by a form.
Everyting is fine if I add records after logging in Grist however the function fails when a new record is created from the form. I suspect this has to do with the privileges but the error message from the form doesn’t give any useful information. Any idea how to trace/debug the problem? I am running self hosted.

@Piergiorgio_Alotto Unfortunately, I don’t think I can help you with that. I fiddled around with forms the other day and had kind of the same experience. It seems to me that something ACL-related is quite broken for forms at the moment, at least if trigger formulas are involved. If someone from the team could shed some light on this, I’d appreciate it.

@TomNit you helped me already more than enough. Thanks again for that.
I would also appreciate if the team could have a look at what happens when a form feeds data into a table which has trigger functions.

Did anybody from the team have the time to confirm that there is indeed an issue with forms feeding data into tables which have trigger functions? Thanks!

Hello,

That’s a really great tool, I find it especially useful to generate records.
Have you had any luck with deleting records? I don’t see this function in the Records class.

Thank you

G.

@Guillaume_L: I’ve just uploaded a new and revised versioned of the RUC that also includes functions Record.remove and Record.remove_multiple. Please see here. Hopefully these will suit?

@TomNit so great ! I’ll try that very soon :slight_smile:

Thanks again for this great tool.

How about cross-document reusable components? I’m not an active user but I listen to the Grist webinars. One question came up was about having a component that could be used by multiple documents - for example a currency conversion widget. The response from Grist at the time was that this is not possible.

@roger_tubby That’s the current state of things, I’m afraid. Looking here (under “Researching”), it would seem that some work is being done on that front, but how much or when anything might be ready for release, I can’t say. It would be great to hear from the dev team on this.

Hmmm. I thought I sent a reply a few days ago on this but it doesn’t show up. This is what I thought I wrote before:

Would it be possible to use a Python component to return a read-only object? This component could be included in each document. For example, the component could emit a JSON object from a static dictionary that was accessed by a specified key. Or even a row of data from another DB query - even from a Grist document via an API call.