Understanding how portable classes

I have had a look at the Pass The Potato example.
I just want to make sure I understand it correctly.

From client side you call the server class => the server side class retrieves the instances from the portable class => the server side class returns the instance values to the client code.

Is this understanding correct?

No, that’s not quite how it works…

You define a class in a client side module.

The server side has access to that module, so the class can be used in both client and server side code.

Instances of that class can be created on either side. They can be used as arguments on the client side in anvil.server.call calls and they can be returned by server functions.

However, web servers and browsers don’t understand Python objects, they only know about JSON, so anvil automatically serialises your instances to JSON, sends it and deserialises it to a Python instance at the other end.

So, you can send instances of your class in either direction, but what you get at the other end is essentially a copy of that object rather than the original object itself.

3 Likes

Thanks for the above @owen.campbell
I think I’m starting to understand the concept.

I have recreated the Anvil Person example in the app below.
It works 100%

Then I followed the same method and created my own class, server code etc.
Populating it in the server manually.
This works 100%

Then I attempted to get the data from a text area, returning it to the portable class, and printing it.
This works 100%

I would appreciate it if you could have a look at my example.
I want to know if I did this correctly, or is there a better way?

https://anvil.works/build#clone:KO6BGTH4XNKHL3GT=5MBB3DTUNNG2WEPL2QNIJU5P