SerializationError: Cannot pass Form1 object to a server function: arguments [0]

Hi All,

I’ve tried to connect my code to my server code and I get the following error:
SerializationError: Cannot pass Form1 object to a server function: arguments [0]

Here is my code:

Server side:

@anvil.server.callable
def enter_text(self):    
    text2 = text1
    arr = bytes(text2, 'utf-8')

    #create temporary text file 

    temp = tempfile.TemporaryFile()


    temp.write(arr)
    temp.seek(0)
    text_read = str(temp.read())

    #split the text
    paragraphs = re.split(r"[,.]", text_read)
    return paragraphs
    video_gen()

frontend:

 def Run_click(self, keys,sender, event_name):
    """This method is called when the button is clicked"""
    text1 = self.Script_Box.text 
    call_enter_text = anvil.server.call('enter_text', self)
    pass

I appreciate any help given, thanks!

Welcome to the forum!

You don’t want to pass your form to the server function. Instead pass data to the server function, e.g. the text from the form:

 def Run_click(self, keys,sender, event_name):
    """This method is called when the button is clicked"""
    text1 = self.Script_Box.text 
    call_enter_text = anvil.server.call('enter_text', text1)

And then use that text in the server function.

Edit: the original error, SerializationError, always means you’re trying to pass something to the server (or from server to client) that you cannot pass between them.

2 Likes

Passing a Form to the Server, so that it could add data, was one of the first things I tried, many moons ago.

But that’s not allowed (see Valid arguments and return values). In fact, it’s actually much more efficient (faster) to pass just the data back and forth. So, here the limitation works in your favor.

1 Like

Hi, thanks for your reply.

I got another error:
NameError: name ‘text1’ is not defined

would you be able to help again at all?

Thanks again, highly appreciated!

Since this is a different error, you should start a new post with it, and give the relevant code. There’s not enough context to be able to know what code is causing the error.

1 Like

your server callable function also needs to change

@anvil.server.callable
def enter_text(self):    
    text2 = text1
    arr = bytes(text2, 'utf-8')
    ...

should be

@anvil.server.callable
def enter_text(text):    
    arr = bytes(text, 'utf-8')
    ...
1 Like