Global object access in server code

Quick generic question as I can’t seem to find any docs on this… I have a specific object from a library I’m using, which I need to instantiate once and once only. I then have other functions which operate on this specific object (i.e. which need access to it). The state of the object is important to the functionality of my app.

If I instantiate the object inside a server side function then it’s not accessible to any of the other functions, and if I instantiate it outside a function then it gets re-created every time any server function is called.

I have tried saving this instantiated object in a data table (as a simple object) but this doesn’t seem to work either. I’m not sure whether there is the concept of a ‘global’ object, but global myobject() doesn’t work.

Does anybody know the best pattern to enable me to achieve this?

Thanks!

Pretty sure that client side you can create a module and include that in all your forms, and anything inside that will persist until you refresh the app/page. No good for server side (ie sensitive) data though.

I’m curious, what about this didn’t work?

Thanks for the quick response @david.wylie… I think I’m making an amateur mistake here as I’ve just tried to load a dummy list to the Simple Object Data Table column and I’m getting the same error. I’m using app_tables.table.search to return a row and I want to update a specific column of the row with the object. However I’ve realised that I’m trying to update the returned search iterator directly, which errors. What is the simplest pattern to:

  1. Search a data table for a row based on matching a column to a value
  2. Update a different column (in this case a Simple Object type) in the data table row returned in step 1

Thanks!

Please could you knock up a simplest case example app that clearly demonstrates the problem and the steps to reproduce it, and share the clone link either here or in a PM to me?

Might be easier for me to see it.

I haven’t done it myself, but looking here -
https://anvil.works/docs/data-tables/data-tables-in-code#searching-simple-objects
I would say that after doing this :

r = app_tables.my_table.search(object_col={'the_answer': 42})

you have a result list in r. Assuming you only have one result :

if len(r) == 1:
  r[0]['the_answer'] = 43  # That's the wrong answer, of course :)
  # or
  r[0]['the_meaning'] = "life"

Something like that? Note I’ve not tried this.

OK my basic error was using search() instead of get() to return a single row… however my previous issue still persists:

https://anvil.works/build#clone:33CKGZL3UWBJBTCQ=UDPUVIYLIFZHACWNT2DJ6JAU

This saves an arbitrary list to a datatable column in a specific row, but when I try and do the same with an object (by clicking the button) I get an error:

anvil.server.SerializationError: Cannot serialize arguments to function.

Ideally I want to do this server side, however I could look at a client side module as you mention above… thanks again for taking the time

(edited to simplify the code and combine several rambling posts by me for clarity, should anyone else stumble upon this thread)

Ok, the issue is it doesn’t know how to pass the Deck object, as it clearly doesn’t like the unicode representation of the cards, as returned by default from pretty_print.

Looking through the library code here :

I have it working after a fashion :

from treys import Deck, Card
...
@anvil.server.call
def save_deck():
  game_deck = Deck().GetFullDeck() # This returns the deck as a list of ints
  row_ref_to_update = 'C'
  save_object_to_datatable_row(game_deck, row_ref_to_update, 'card_deck')

Ok, so you store a list of ints. You can convert them back to their pretty format like this :

print(Card.print_pretty_cards(Deck().GetFullDeck)

Obviously you would be working on the fetched list, and if you need the list back into the card format for shuffling, etc., then there’s probably a function there to do that (not looked).

Does that help at all? It’s more of a workaround than solving your actual issue.

Thanks so much for taking this time, actually I don’t need the wacky unicode printing… I’ve dug into the library and it’ll be pretty easy to implement by rewriting the parts I need (as you point out, it’s just a list of integers!)

I owe you a beer!

1 Like

And for the record @david.wylie, if you ever need to randomly shuffle a list in place it’ll probably make you love Python a little more:

from random import shuffle

shuffle(my_list)
1 Like

I actually thought there was a mathematically “realistic” way to shuffle a deck of cards in a computer?

That might be a false memory …