What I’m trying to do:
I have a Jupyter Notebook that I am using to load in an Excel file, then use that file to manipulate a given YAML. In this original notebook, I have heavily relied on using the ruamel package for accessing and manipulating specific elements of that YAML file. As you can imagine, passing a datatype whose structure is of key importance as a media object and then using the get_bytes() mehod on it makes me very wary on the integrity of the data as it passes to and from the server. Below is the snippet of the function I use (after modifying it for Anvil purposes) to manipulate a given YAML (I have only included specific snippets as the code is pretty long):
import anvil.media
from datetime import datetime@anvil.server.callable
def yaml_process(file, unq_act_ser, act_all_fun_ser):
unq_act_deser = json.loads(unq_act_ser)
act_all_fun_deser = json.loads(act_all_fun_ser)
#importing the yaml
yml_file = file.get_bytes()
import ruamel.yamldef unique_lists(lst_of_lsts): #defining unique lists for unq_act so that we iterate over them and create plans. unique = set() result = [] for lst in lst_of_lsts: if tuple(lst) not in unique: unique.add(tuple(lst)) result.append(lst) return result unq_act_patt = unique_lists(unq_act_deser) # defining function that finds source pattern def subtract_30(number): while number >= 30: number -= 30 return number #here I am creating and new YAML to add to using safe_load() # Create a new YAML object yaml = ruamel.yaml.YAML() yaml.preserve_quotes = True # added in ruamel.yaml yaml.explicit_start = True # control also available in PyYAML yaml.explicit_end = True # control also available in PyYAML # Open the YAML file in read-write mode # Load the YAML documents using the ruamel.yaml.safe_load() method data = list(yaml.load_all(yml_file)) # Get the third document doc3 = data[2]
#here the code is looking at specific elements in the YAML and updating it
#Once the code has run and manipulate the different data elements, I am trying to write it back to the YAML # Seek to the beginning of the file #file.get_bytes().seek(0) # Overwrite the file with the updated documents yaml.dump_all(data, file.get_bytes()) file.write("...\n") # Close the file #file.truncate() #return the file return file
anvil.server.wait_forever()
What I’ve tried and what’s not working:
Please see my cloned app which has given me the following error:
YAMLStreamError: stream argument needs to have a write() method
at C:/Users/hossa/anaconda3/lib/site-packages/ruamel/yaml/emitter.py:227
called from C:/Users/hossa/anaconda3/lib/site-packages/ruamel/yaml/main.py:644
called from C:/Users/hossa/anaconda3/lib/site-packages/ruamel/yaml/main.py:906
called from C:/Users/hossa/anaconda3/lib/site-packages/ruamel/yaml/main.py:913
called from C:/Users/hossa/anaconda3/lib/site-packages/ruamel/yaml/main.py:583
called from C:/Users/hossa/AppData/Local/Temp/ipykernel_7788/3012870045.py:236
called from Form1, line 24
I am sure there is something I am doing wrong or other approaches to reach my objective in a more sensible manner. I look for your experience and wisdom. Thanks!
Code Sample:
# this is a formatted code snippet.
# paste your code between ```
Clone link:
share a copy of your app