Components (widgets) in server modules

Can widgets / components be created in server modules, like in

@anvil.server.callable
def createLink():
  l = Link(text="Some link")
  return l

I can see anvil.Component(), but don’t know what it is :blush:

I’m creating a dynamic menu from records in db and would like to create everything on the server and then pass an array of links.

Furthermore, can a reference to a function be passed to a server? I get a serialization error when I call a server function defined as:

@anvil.server.callable
def createLink(eventHandlerFunction):
  l = Link(text="Some link")
  l.set_event_handler("click", eventHandlerFunction)
  return l

Maybe I just want too much :sunglasses:

Don’t think so. No - see second part about RPC.

I would return the data for the links from the server and then create the links themselves on the form. Think that broadly achieves the same result?

Steps would be roughly :

  • call a server function from your form to create a list of link urls (and text)
  • dynamically create a link from the returned data in the form
  • add the link to your menu placeholder.
  • iterate over the returned data until all links have been created

Something like that.

No, server comms is over RPC and the manual says :

Arguments and return values to RPC calls can be strings, numbers, dicts, lists, datetime.date/datetime, and None, and may not be circular.

Full details here : Anvil | Reference

1 Like

Many thanks for explanation and a link to documentation. I’ll do it as you suggested.

I have made a habit of just doing everything between the server and a Form as a JSON object. It keeps things simple and is pretty flexible.

What about pickle? According to this article pickle is more “native” to Python. I’m relatively fresh in Python, coming from the world of c#, java, js, etc. …

Pickle handles almost anything. I have used it to save somewhat complicated objects.

The downside: it is not necessarily portable from one OS / machine / processor to the next, so think of it as a way to store but not transport data.

1 Like

… besides, with pickle I get “NotImplementedError: pickle is not yet implemented in Skulpt” …