Sharing Modules between client and server code (Portable classes)

What I’m trying to do:
I am trying to define a class to exchange between the client and server of a single Anvil Application.

I’ve created a very simple application to try and make a simple teaching exercise.

I was prompted to try this in a previous post where I was getting errors:

To solve those errors, I am trying the recommended:

Move Transport somewhere else, likely an isolated client module that doesn’t need the anvil.js.window object.

After trying that a lot with my candidate app I decided to use a simplified application first.

I note from this post:

This answer:

"Answered my own question - you create it as a client module then the server side can import it. "

I checked this Anvil Docs Page:

And the text:

" First, the class definition needs to be accessible to both client and server code, so it should go in an Anvil Module (not a Server Module). Here’s an example:"

By Anvil Module, I am thinking of this as a ‘Client Module’ rather than a ‘Server Module’

Do you need to be on a paid subscription to do this?

If not can you nudge me along to getting the simple application [clone link below] working?

What I’ve tried and what’s not working:
I have created a module called Transport which defines a class Cargo with single attribute ‘stuff’ for exchange between client and server code to try and get this to work:

I have tried both adding the module to the client, and under the form definition for the client but I keep getting error:

Clone link:

Not done this before, I hope this is correct:

share a copy of your app

My apologies in advance if I have missed something very obvious. I fear that my 30 years of writing strongly typed thick client OO based windows applications interacting with RDBMS hosting fully normalised data models is hampering a speedy uptake of the Anvil/Python architecture. i.e. I have a lot of baggage

Can you clarify in one or two sentences what problem you are trying to solve? I see a number of issues and paths you have but I can’t see what objectives you are trying to achieve in this post.

Hi,

I am trying to use a portable class delcared on the client side within the server side as transport for data between the two.

I understand that there will be other Anvil ways of achieving the final outcome, but I wanted to be able to share this module/class between client and server code as has been suggested (I may have interpreted the documentation incorrectly though) in the documentation for portable classes.

I do not as yet have a paid subscription, hence why I am asking do you need to have one to use portable classes as a first step, and if you do not what am I doing wrong?

i.e. Why does it tell me I need “Upgrade to configure your app”

Thanks

Adrian

@aw1 , can you tell me what line 17 in your method GetCargo-click is :
‘reply : Transport.Cargo’

never saw this syntax before with python ??

Hi,

Type hinting I believe.

Part of my luggage, which I may eventually ditch.
I like to know what I am dealing with.

It may be as of much relavance to the runtime engine as a comment though.

Comfort blanket for me.

Cheers

Adrian

very cool , thanks …

Yep not used.

There were a couple issues:

in the form your imports needs to be like this
from .. import Transport
not
import Transport

and in the server function you didn’t actually return the Portable class, so you need to add
return c to the end of the function and then everything works.

Hi Duncan,

Thanks for that nudge, I had been playing with … on my local server but was getting things like:

[ERROR anvil.app-server.run] Error report from client code:
ImportError: attempted relative import beyond top-level package

When I thought I’d strip it right back to get the basics correct.

Sorry just noticed you said form - that works great thanks.

Thanks for the ‘return c’ observation - that would have have been my next chin scratching moment.

Have a nice weekend.

Adrian

I said:

"The dialogue happens immediately I start the application - it has not even attempted to call the server part at that stage or even draw the web page.

Is it some aspect of my account I wonder…"

No, it was my not reading your answer properly - all good now.