Passing Server Callable Function Returns to other Server functions

I am relatively new to Anvil and am still trying to wrap my head around building applications
that use a server (I usually just run my code in an IDE), so I appreciate the patience and help.

What I’m trying to do:
I want to use the client as a bridge to pass data between two server-callable functions. I have two server functions that I am calling from the client side:

  1. ‘tod_preprocess’: This function returns three variables (tod_df, dp_plans_all_ser, unq_act_ser) as shown in the cloned app. I am returning these variables since some of them will be needed for the second server-callable function, ‘yaml_process’, to be executed.
  2. ‘yaml_process’

As you can see from the cloned app, I am correctly assigning the returns to variables. However, I get a NameError:

NameError: name 'unq_act_ser' is not defined

Here is applicable part of my client-side code:

def file_loader_1_change(self, file, **event_args):
“”“This method is called when a new file is loaded into this FileLoader”“”
tod_df, dp_plans_all_ser, unq_act_ser = anvil.server.call(‘tod_preprocess’, file)
self.tod_table.content = tod_df # displaying the final dataframe

def file_loader_2_change(self, file, **event_args):
“”“This method is called when a new file is loaded into this FileLoader”“”
yaml_processed = anvil.server.call(‘yaml_process’, file, unq_act_ser)
anvil.media.download(yaml_processed)

And this is a portion of the server-function that I have an issue with (it is a couple hundred lines so I am only including the applicable portion), yaml_process:

import anvil.media

@anvil.server.callable

def yaml_process(file, unq_act_ser):
unq_act_deser = json.loads(unq_act_ser)
#importing the yaml
yml_file = file
import ruamel.yaml

Blockquote

def 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)

Clone link:
share a copy of your app

You’re running into variable scope issues. A variable defined in one function is not automatically available in another function. So the unq_act_ser variable defined in file_loader_1_change is not available in file_loader_2_change.

To pass values between functions in a form like that, set the variable on the form itself using self., e.g.:

tod_df, dp_plans_all_ser, self.unq_act_ser = anvil.server.call(‘tod_preprocess’, file)

and

yaml_processed = anvil.server.call(‘yaml_process’, file, self.unq_act_ser)

Thank you very much, that makes much more sense. However, I do have the following questions:

  1. Now that I understand what I need to do on the client-side, does anything else need to be done on the server-side? That is, I now make a call from the client with ‘self.unq_act_ser’ as an argument. However, on the server-side does the server-callable function ‘yaml_process’ need to be changed to also have an argument of ‘self.unq_act_ser’?

  2. I tried following your suggestion and have not changed anything outside of it. However, when running the code, I get the following error:

TypeError: 'StreamingMedia' object is not subscriptable
at C:/Users/hossa/AppData/Local/Temp/ipykernel_2488/1857508715.py:38
called from Form1, line 24

Thanks a lot for the help and your patience.

Your server function does not need to change.

As far as the error you’re getting, which line is line 24 in Form1?

This one:

yaml_processed = anvil.server.call('yaml_process', file, self.unq_act_ser)

That one I’m not sure about. The error message sounds like at some point you’re trying to index into a media object. You’re not doing that on line 24 in the form, and the error message is pointing to a temporary file on your PC. So your server functions must be in an uplink, right? You might have gotten an error message in the uplink’s console that would give a better idea of where in the server function things went wrong.

Hi,
Yes, you are right. I am not indexing on Line 24 in the form, rather, I am using the Server function ‘yaml_process’ to read the yaml file. On line 38 of the Server function, I am trying to index the yaml file which works fine when I am executing this locally in my Jupyter notebook. However, now it seems that the file is a ‘streamingmedia’ type, there are limitations on how to index it. I think this may belong on a different thread to avoid confusion. Let me know if you concur and I will start a new thread to keep things clear.

I agree, a different topic makes sense. The environment is different between the Anvil server an Jupyter notebooks, so different techniques are often needed in each.

2 Likes