You are currently viewing the new Anvil Editor Docs.
Switch to the Classic Editor Docs
You are currently viewing the Classic Editor Docs.
Switch to the new Anvil Editor Docs

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 ‘New Blank App’. Choose the Material Design theme.

Location of the Create App button

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

Click the + button in the Sidebar Menu, and select Uplink:

Click the + button, then select Uplink

Click the + button, then select Uplink

If you haven’t already deployed your app, you’ll see a prompt to create Uplink keys. Click the Add Uplink keys button:

If you’ve already deployed your app, you’ll see all your deployment environments:

Click one to open it up:

Next to Server Uplink, click Enable. This will create a secret key that you can use to connect external code to your application. Copy it to your clipboard:

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 Uplink key for this quickstart. Learn more about Client Uplink.

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(f"Hello from your own machine, {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 ‘Code’ to see the Python code for Form1.

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

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

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

Now click the Run button at the top right 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.


Do you still have questions?

Our Community Forum is full of helpful information and Anvil experts.