Hi everyone…
This question may well be wider than just Anvil, perhaps applicable to all client/server web apps in fact, but I’m running aground repeatedly here in Anvil-land and was hoping for some inspiration from more experienced brains please…
Before Anvil, I had a collection of classes and methods that worked nicely on their own on the desktop, or their own on the server. For example:
class Video_Captions:
""" Contains helpful methods for handling, downloading, and converting
and correcting subtitles/captions """
def convert_vtt_to_srt(self):
""" VTT is a subtitle format commonly used by YouTube, SRT is another
commonly used by social media platforms like Twitter and Facebook.
This method takes self.vtt and creates self.srt in place. """
The nice thing here is that it’s very easy to get and set self.vtt
and self.srt
etc. with the object being updated in place - no need to supply arguments, no need to return values, no pre-processing, no post-processing. The problem comes with the client code trying to call this server code, because I can’t (I believe?) return a Video_Caption
object to the client, only a string, list, dictionary or json value (not a set, strangely?), or bytes as a ‘media’ object. I also can’t call the convert_vtt_to_srt()
method from the client without recreating a Video_Caption object each time.
This is causing me to write a whole lot of extra code to strip out the relevant Video_Caption attributes and return them, or create a temporary new object just to run the method, return a json then discard the object, then recreate it.
So… I just wondered if anyone knew of some best practice, or a pattern, or a general approach to this (presumably quite common?) challenge, which doesn’t involve me refactoring all of my original code, converting an object orientated approach back to a pipeline of functions with their inputs and outputs each preceded by @anvil.server.callable
?
Other options I’m exploring include:
- Use a database - but this creates additional server calls every time the client wants to refresh or update an attribute.
- Use a decorator function, and maybe CleverDict to auto-generate json return values e.g. from
vars()
- Make all my methods Class methods
- Make all my methods Static methods
- Move these methods into the client code - but makes it really bloated and also exposes all the code making security etc really tricky
- Playing with the Anvil
media
object but I’m tying myself in knots trying to reconstruct a usable Video_Captions object on the client side. - Convert objects to nametuples - curiously Anvil can return those but not sets!
- Write a client function to convert dictionaries to objects with attributes
I’ve also read:
If anyone has general thoughts or can signpost me to topics/discussions/tutorials that would be really helpful thank you. Often it’s just a question of knowing that the topic has a specific name already (like “dependency injection” or “dispatcher”) but if you don’t know it’s a “thing” or what that “thing” is called, you don’t know what to search for!
Many thanks in advance,
Peter