Client-side code interacts with your user interface by manipulating the Python objects that represent components. When components raise events, they call functions in your client-side code.
def button_1_click(self, **event_args): """This method is called when the button is clicked""" alert('Logged in as: ' + anvil.users.get_user()['email'])
Client-side code interacts with server-side code by calling server functions.
return_value = anvil.server.call('my_server_function', my_arg=[1, 2, 3])
If you need to use a module or language feature that is not available in the browser, don’t worry – you can make a function call to a Server Module which runs in a full Python environment. You can also make function calls to and from your own Python environments using the Uplink.
Forms and components are represented in code as Python objects. You can instantiate Forms and components like any Python class:
label_instance = Label(text='Hello, World!') from Form1 import Form1 form1_instance = Form1(param='an_argument')
These Python objects have attributes and methods that you use to control the UI:
# Change the text on the Label label_instance.text = "Hello again, World!" # Get a list of all the components in form1_instance components_on_form1 = [x for x in form1_instance.get_components()]
All Forms and components exist in a heirarchy with the top-level Form at the top. To add your Form and component instances into your app, you must add them to a Form or component in the hierarchy:
# Create a Label in code label_instance = Label(text='Hello, World!') # Add the Label to the top-level Form top_level_form = get_open_form() top_level_form.add_component(label_instance)
Since the client-side Python runs in the user’s web browser, you can store data in the browser’s memory by creating variables in Python.
In the following example, we store a list of active filters as an attribute of the
Form1 class. When the Form loads, the list
of filters is empty. The Form contains a series of CheckBoxes, and when one is checked, we add its
text to the list of active filters.
class Form1(): def __init__(self, **properties): # No filters are active on startup. self.active_filters =  def check_box_change(self, **event_args): # When a CheckBox is changed, set the active filter accordingly check_box = event_args['sender'] if check_box.checked: self.active_filters.append(check_box.text) else: self.active_filters.remove(check_box.text)
You can also create global variables that can be used by all your Forms.
This data persists as long as the app is open in the user’s browser tab. If you want to store data to be retrieved when the user re-vists your app, you can use Anvil’s built-in database, or use Sessions and Cookies.
anvil module is Anvil’s Python API.
Most of the Anvil-specific Python code in this documentation uses classes and functions from
anvil. In particular, all the component classes are in the
Anvil’s core server-side functionality is in
anvil.server (along with client-side functionality that relates to the server, such as
anvil.server.call). Other features of Anvil
have their own namespaces within
anvil, such as
anvil module is imported by default in Forms, and must be referred to explicitly in Server Modules (when you add a
Service, the relevant imports are automatically written for you).
A complete listing of classes and functions in the
anvil module is available in the API Reference.
If you want to use a library that is not available in client code, write a server function that uses that library, then call it from the client.