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.

Setting up the Uplink

To set up the Uplink, you need to enable it in the Anvil IDE, pip install a Python library, then write three lines of code.

The Anvil uplink is a library you add to your own code, running outside Anvil. It connects securely to the Anvil server, and allows your Anvil app to call functions in your project. You can also call server modules within your app from your uplinked code. The Anvil uplink works through most firewalls, as it initiates the connection to the Anvil server.

Start by selecting the Anvil Uplink in the Gear Menu Gear Icon:

The Gear Menu with Uplink highlighted.

It will display an authentication key that you will use to connect your code to your app.

The Uplink dialog with the Uplink enabled and the key displayed.

Install the Python library on your machine:

pip install anvil-uplink

Connecting

In your local Python code, call anvil.server.connect() with your app’s connection key in order to link this program with your Anvil app.

The function anvil.server.wait_forever() keeps the Python script running to allow Anvil to call functions in it. You can use any other way to keep the process alive - you can even run the Uplink in a Python REPL!

# In a script on your own machine (or anywhere)

import anvil.server
anvil.server.connect("[YOUR UPLINK KEY]")

@anvil.server.callable
def get_file():
  with open("./image.jpg") as f:
    return anvil.BlobMedia("image/jpeg", f.read())

anvil.server.wait_forever()

Advanced setup

Custom setup code

Sometimes, it is necessary to initialise an uplink’s session before interacting with your app. For example, a client (unprivileged) uplink process might need to authenticate with the app before it will be allowed to perform certain operations.

If you pass the init_session= keyword parameter to anvil.server.connect, it will be called after the uplink connection is established, but before any other interaction (such as server calls or function registrations) have occurred. If connection is lost and the uplink library reconnects, init_session will be called again before any interaction occurs. This guarantees that init_session has completed whenever you interact with the Anvil app.

import anvil.users

def setup():
  anvil.users.login_with_email("my_user@example.com", "MY_PASSWORD")

anvil.server.connect("[YOUR UPLINK KEY]", init_session=setup)

anvil.server.call("some_func")

If init_session raises an exception, all subsequent Anvil interactions will fail and raise exceptions.

Suppressing connection messages

By default, the Uplink prints some output during the connection phase:

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

You can pass an optional keyword argument “quiet” to anvil.server.connect() to suppress this. Errors (such as connection failure and reconnection attempts) will still be printed.

# Connect without printing to console
anvil.server.connect("[YOUR UPLINK KEY]", quiet=True)