Live Chat

We'll need to share your messages (and your email address if you're logged in) with our live chat provider, Drift. Here's their privacy policy.

If you don't want to do this, you can email us instead at contact@anvil.works.

Python in the Browser

The user interface of an Anvil app is programmed in Python. Anvil compiles that Python into Javascript, and when someone visits your app the client-side Python code runs in their web browser.

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])

What kind of Python?

Your Form code, which runs in the browser, runs in approximately Python 2.7. We say “approximately”, because in fact your Python code is being compiled to Javascript so it can run right in the user’s browser. The web browser is a constrained environment, so there are a couple of things missing (e.g. you can’t open a file in the browser!). That said, most things should work.

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.

Our client-side Python support uses the open source Skulpt Python-to-Javascript compiler. We believe strongly in giving back to open source - if you want to join us, visit us on GitHub.

Everything is an object

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)

Storing data in memory

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.

The anvil module

API Docs

The 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 module. 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.email, anvil.users and anvil.http.

The 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.

Limits to Python in the browser

Anvil compiles client-side Python into Javascript using the Skulpt compiler. This compiler supports a subset of Python 2.7. Some standard libraries are missing or incomplete – particularly libraries such as file manipulation that do not make much sense in a web browser.

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.