To set up the Uplink, you need to enable it in the Anvil IDE,
pip install a Python library, then write three lines of
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 :
It will display an authentication key that you will use to connect your code to your app.
Install the Python library on your machine:
pip install anvil-uplink
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.
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()
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("firstname.lastname@example.org", "MY_PASSWORD") anvil.server.connect("[YOUR UPLINK KEY]", init_session=setup) anvil.server.call("some_func")
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 “
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)