Using python-docx to create/edit docx on Anvil

When working with server functions it’s good practice to avoid using anvil.server.call so much. If you rewrite the logic you only need 1 of those functions decorated with anvil.server.call.

When you do anvil.server.call you are creating a new call context. A common question on the forum is why don’t global variables work in server modules? The same problem is being seen here. Data is not persisting between server calls.

You only need to use anvil.server.call if you want to communicate to a server function from the client (or to communicate between uplink and server functions). When you’re already in the server just call the function.

e.g.

@anvil.server.callable
def write_to_a_file(temp_file_name):
  with open(temp_file_name, 'w+') as f:
    f.write(anvil.server.call('edit_doc_server'))

becomes

def write_to_a_file(temp_file_name):
  with open(temp_file_name, 'w+') as f:
    f.write(edit_doc_server())

It might also be worth accessing the table from the server rather than the client.
I think you can adjust your client code to

def button_edit_click(self, **event_args):
    """This method is called when the button is clicked"""
    anvil.server.call('update_doc') 
    # update_doc is the only function that should be decorated
3 Likes