Learn how to run code in Anvil’s hosted Python backend
Anvil provides a server-side Python environment for your apps.
Follow this quickstart to write a server function and call it from the client.
Log in to Anvil and click ‘New Blank App’. Choose the Material Design theme.
In the App Browser, click the + next to Server Modules to add a new Server Module.
You will see a code editor with a yellow background.
Write this function into the Server Module:
def say_hello(name): print("Hello from the server " + name) return [1, 2, 3, 4]
say_hello function, add
@anvil.server.callable. The function now looks like this:
@anvil.server.callable def say_hello(name): print("Hello from the server " + name) return [1, 2, 3, 4]
That makes the
say_hello function callable from the client side.
Under Forms in the App Browser, select Form1.
Click on the ‘Code’ tab to see the Python code for
You will see a few lines of pre-written code. Your Form is represented as a class called Form1. It currently has
only one method, the
At the end of the
__init__ method, write these lines:
return_value = anvil.server.call('say_hello', 'Anvil Developer') print("The return value was %s" % return_value)
Now click the ‘Run’ button at the top of the screen.
The Output Panel should display these lines:
The output with the yellow background came from a Python process running in Anvil’s cloud cluster. The output
with the white background ran in your browser. You made the browser communicate with the server over the internet
anvil.server.call('say_hello', 'Anvil Developer').
Click on the button below to clone a finished version of this app into your account.
Want more depth on this subject?
Read more about server-side code in Anvil.
Want another quickstart?
Every quickstart is on the Quickstarts page.