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

Server Modules

Anvil’s Server Modules are a full server-side Python environment.

The user has ultimate control over their own browser: If they open up their browser’s developer tools or Javascript console, they can see and modify all the client-side code in your Anvil app (that’s your Forms and Modules).

Sometimes, you want to write code that always executes exactly as it is written (for example, checking a password before changing a file), or code that contains some secret information (for example, your secret authentication keys for a third-party API).

You can write this code in a server module, and it will never be seen by your app’s users (even if they’re experts and poking around inside their own browser). You can call functions in your server modules from your client side code.

You can pass useful things like data table rows and Media objects to and from server functions, as well as simple Python data structures (more information below).

Calling server functions from client code

To make a server function available from your client code, decorate it as @anvil.server.callable.

import anvil.server

def guess(number):

  if number < CORRECT_NUMBER:
    return "Too low!"
  elif number > CORRECT_NUMBER:
    return "Too high!"
    return "Correct!"

From ordinary client-side code (for example, the event handler in a Form), you can call your server-side function using<function_name>), where function_name is the function name as a string.

# In client code (eg a form):
import anvil.server

def do_guess(n):
  result ="guess", n)
  print("You guessed: " + n)
  print("The server said: " + result)
  return result

Just call, more_arguments…). Any extra arguments are passed through to the server function. This includes keyword arguments.

You can call server functions from other server functions, as well as from client-side code and uplink functions. ( is available everywhere.)

Valid arguments and return values

The arguments and return values of @anvil.server.callable functions must be very simple. They may only be strings, numbers, lists, dicts,, datetime.datetime,None, Media objects, or rows from a data table. They may not be circular (for example, a dict may not contain itself).

Suppressing the Spinner

Using in client code displays a spinner. To call server functions without displaying a spinner, enclose your code in a with anvil.server.no_loading_indicator: block:

def do_guess_silently(n):
  with anvil.server.no_loading_indicator:"guess", n)

You can also call a server function without displaying a spinner by using anvil.server.call_s instead of

def do_guess_silently(n):
  anvil.server.call_s("guess", n)


Customise Server Function name

You can customise the name of your callable function by passing a parameter to @anvil.server.callable. In this example, the function foo must be called with'my_func').

import anvil.server

def foo():
  return 42

Persistent Server Modules

By default, all variables are wiped clean after each server function call returns, except for anvil.server.session. This can be avoided by enabling the Persistent Server option, available on our Business plan and above.

Datetime object timezones

Anvil ‘stamps’ datetime.datetime objects with timezones as they pass across interfaces. This creates a timezone-aware datetime object, so you don’t lose track of what timezone a datetime object was created in:

  • Any timezone-aware datetime object retains its timezone when transferred in any direction between client, server and Data Tables.
  • Any naïve datetime object that is transferred between client and server gets automatically ‘stamped’ with the timezone of the place where it was created before it is transferred. This means that datetime objects generated on the client are automatically ‘stamped’ with the timezone of the browser, while those generated on the server are ‘stamped’ with the timezone of the server, which is always guaranteed to be UTC.

These simple rules mean that you can always compare datetime objects, no matter where they were created and no matter how much you’ve moved them around or stored them in data tables.

You can explicitly create timezone-aware datetime objects if you wish, using helper classes from the module:


naive_local =
# 2019-08-09 10:10:00.406000
aware_local =
# 'stamped' with the timezone of the browser
# 2019-08-09 10:10:00.418000+01:00

naive_utc = datetime.utcnow()
# 2019-08-09 09:10:00.426000
aware_utc = # note: NOT datetime.utcnow()
# 'stamped' with the UTC timezone (timezone of the server)
# 2019-08-09 09:10:00.433000+00:00

aware_custom = datetime(2017,11,16,23,45,15,0,
# 'stamped' with the UTC+3 timezone
# 2017-11-16 23:45:15+03:00

Server function timeout

Server functions run synchronously and should return fairly quickly. By default, the timeout is set to 30 seconds, after which a running function will be stopped. For longer-running jobs, you should use Background Tasks.