Very simple ClientInfo example


I just want to share a simple example of how to get and use information when a client connects to your app.

In the example we define a server side function (callable) which parses the anvil.server.context.client object.

def check_client_info():
    client_info = anvil.server.context.client # returns client info object
    type = client_info.type
    ip = client_info.ip
    country =
    country_code = client_info.location.country_code
    city =
    subdivision = client_info.location.subdivision
    longitude = client_info.location.longitude
    latitude = client_info.location.latitude

    # in this example we are only interested in the country
    return country

When the form is initialized e.g. when a client connects, we call the server side function check_client_info. In this example, the form will only load if the client is located in Sweden.

class Form1(Form1Template):

    def __init__(self, **properties):
        resp ='check_client_info')

        while resp != "Sweden":
            alert("This app is only allowed in Sweden")

        self.label_1.text = f"You connected to the app from {resp}"
        # Set Form properties and Data Bindings.

Happy hacking :slight_smile: