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

Uplink: Code outside Anvil

You can connect code anywhere on the Internet to your Anvil app using the Anvil Uplink - check the Quickstart to get up and running.

There are two kinds of Uplink:

  • Server Uplink
  • Client Uplink

Code connected to your app with a Server Uplink has the same privileges as Server Module code.

Code connected to your app with a Client Uplink has the same privileges as Forms code.

Sometimes you want to write some server-side code that can’t run on our servers. Perhaps it needs resources that are only available on your company network, or on your computer.

The Server Uplink makes any Python code behave like a Server Module. You can define functions in your uplink code and then call them from your app using anvil.server.call.

The connection goes both ways: you can anvil.server.call into your app from the Server Uplink too.

Anything you can do in a Server Module can also be done from the Uplink. Your uplink code can access data tables, log users in, and register new server functions with @anvil.server.callable. For example, it can use anvil.users.get_user() to check who’s logged in.

Use the Server Uplink key for trusted code. Because of the privileges it gives your code, your Server Uplink key should be kept secure. For instance, avoid sharing scripts that contain your Server Uplink key.

It’s common to use the Uplink to connect to a Jupyter Notebook to create an Anvil front-end for your Data Science models. Here’s a tutorial video where we do just that.

Here’s a script that you can run on your own machine and connect to your app.

import anvil.server

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

@anvil.server.callable
def get_data(name):
  print(f"Hello from your own machine, {name}!")
  return [1, 2, 4, 8]

anvil.server.wait_forever()

To run this, you need to pip install anvil-uplink and enable the Uplink in the Anvil Editor - see Setting Up for complete instructions.

Sometimes you want to connect a system to your app, but you don’t want to trust that system with your data. It might be an IoT device that could be easily compromised, or it might be a script running on a customer’s machine. So you can use a special Client Uplink key that gives your script the more restricted privileges of client-side code. Client-side code needs to have its privileges locked-down because the user can control the code that runs in their browser. The same logic applies to untrusted Client Uplink code.

For example, it would be wise to use the Client Uplink to connect a weather station running on Raspberry Pi to your Anvil app. Because it sits outside where anybody could access it, you don’t want the weather station to have full read/write access to all of your app’s tables.

The Client Uplink gives code the same privileges as your Forms and Modules.

Code using the Client Uplink cannot access Data Tables unless they have been set to client-accessible, and it cannot register server functions. It can still call server functions. This means you can write a server function for every operation that you’re happy to expose. It will not be able to call anything else. This is the security best-practice that applies to Form code as well.

Here’s a script that you can run on your own machine and connect to your app.

import anvil.server

anvil.server.connect("<your Client Uplink key>")
anvil.server.call('my_exposed_server_function')

To run this, you need to pip install anvil-uplink and enable the Uplink in the Anvil Editor - see Setting Up. Then run this script in the command line with python my_script.py. It will connect to your app, call the server function, then exit.

Can you use both?

Yes!

The uplink dialog allows you to view both server and client authentication keys:

Part of the Uplink dialog, where you can select Server or Client versions of the Uplink.

The Uplink and Publish dialogs allow you to create both server and client authentication keys, for each Deployment Environment that you run.

Uplink dialog

This screenshot shows the Uplink dialog. You can also find these options in the Publish dialog, under Advanced Settings.

You can use both Server and Client Uplink keys at the same time from different local scripts.


Do you still have questions?

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