I’m fairly new to Python and I’m trying to convert a working class and script I already have to work in Anvil. I’ve run into two challenges that I think should be straightforward but haven’t found a good solution:
- I have one file with a couple classes where one opens a JSON file on my file system and loads the JSON data into a class dict variable and a second class that does some custom processing. A separate script file instantiates the classes and initializes them with the desired input file and calls the subsequent class functions to process the json into another set of dicts/lists for output.
I haven’t been able to figure out how to create a server module for the class and instantiate it in a separate server module, so I ended up combining into a single server module. Is there any documentation for best approach to doing this in Anvil?
- Now that I have the classes and scripts together in one server module, I’m trying to load the data into my class from a media type in a table as an equivalent of:
** with open(self.file_obj, encoding=‘latin1’) as ds_file:**
** self.ds_raw_data = json.load(ds_file)**
I’ve tried various approaches unsuccessfully, including:
self.file_obj = app_tables.userfile.get(id=file_id)['datafile'] self.file_obj_data = self.file_obj.get_bytes() self.my_json_data = json.loads(self.file_obj_data, encoding='latin1')
With the above, I get: TypeError: ‘NoneType’ object is not iterable
Other varied errors along the way:
TypeError: expected str, bytes or os.PathLike object, not LazyMedia
and with other variations at a later step: ‘LazyMedia’ object is not iterable
What is the best way to load from a media type into a dict? I’m guessing there is some additional decoding or deserialization missing.