Quickstart: Connect to your own Python code

Use any back-end﴾s﴿ you like

Anvil allows you to connect to Python code anywhere and make Python function calls directly from your app.

Follow this quickstart to call a Python function on your own machine from an Anvil app.

Create an app

Log in to Anvil and click ‘create app’. Choose the Material Design theme.

Location of the Create App button

Click on ‘Uplink’ in the Gear Menu Icon that looks like a cogwheel:

Location of Uplink dialog in Gear Menu

You’ll see this dialog:

Uplink dialog showing the Enable Uplink button

Click the green ‘Enable Uplink’ button. You’ll get a randomised id for your app.

Uplink dialog showing the Uplink Key

You can also use a ‘client’ Uplink key that only gives the permissions available to client-side code. You can use this to connect to untrusted code with a restricted set of permissions so your data remains secure. We’ll stick with the server key for this quickstart.

On your own computer, install the Uplink library (ensure you have Python installed).

pip install anvil-uplink

Connect a Python script to your app

Create a file called hello.py with the following contents:

import anvil.server

anvil.server.connect("<your Uplink key>")

@anvil.server.callable
def say_hello(name):
  print("Hello from your own machine, %s!" % name)

anvil.server.wait_forever()

Replace the "<your Uplink key>" string with the Uplink key from your app.

Run the script. You should see output like this:

Connecting to wss://anvil.works/uplink
Anvil websocket open
Authenticated OK

Call your local function from your app

Back in the Anvil Editor, close the Uplink dialog and click on the ‘Code’ tab to see the Python code for Form1.

The Design View for Form1 with the Code tab in the top-right highlighted

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 __init__ method.

The Code View for Form1

At the end of the __init__ method, write this line:

    anvil.server.call('say_hello', 'Anvil Developer')

Run your app

Now click the ‘Run’ button at the top of the screen.

Running your app in the editor

Go back to where your script is running. It should have printed this message:

Hello from your own machine, Anvil Developer!

You’ve successfully connected your own machine to your Anvil app.

Copy the example app

Click on the button below to clone a finished version of this app into your account.

This gives you power

A script that runs anvil.server.connect() can do anything an Anvil Server Module can. It can:

  • call functions in your Anvil Server Modules or in any other Uplink-connected script, using anvil.server.call
  • use Data Tables directly
  • check which user is currently logged in

You can use the Uplink to connect to any environment that runs Python. That includes Jupyter Notebooks.

This means you’re not limited by what Anvil provides - you can do anything you can do on your own machine, and use Anvil’s features wherever they make life easier. Many people use Anvil as an add-on to their existing systems, to provide a UI or simple email client, for example.

You’re not even limited by what Python can achieve - a Python process makes the Uplink connection, but since Python can make system calls, you can launch any process you like from your Anvil app (great for automating machinery or running automated tests!)

Next up

Want more depth on this subject?

Read more about connecting your app to any external Python code.

Want another quickstart?

Every quickstart is on the Quickstarts page.