How do you define a session?

You have this list predefined variable :

anvil.server.session (see here : Anvil | Reference)

but there is no “session id” built in. So I do this in a server module :

import uuid
...
def get_uuid():
  if "id" not in anvil.server.session:
    anvil.server.session['id']=str(uuid.uuid4())

  return anvil.server.session['id']

and i store that in the data tables (as a text field) when I need to relate data to a session. Every time I need the “session id” I call this function, which either returns it or sets it first.

Does that help?

(edit) - to expand, you would first search the data table for the uuid, updating the row if it exists or possibly creating it if not. You can see it in action in my “captcha” work-in-progress here (look at the generate captcha server function) :

2 Likes