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.

Using code outside Anvil
« Anvil Tutorials
3m 45s

Sometimes, you want to use code that’s not on the web, from your web app. Usually this is a bit of a pain, but Anvil makes it easy with the Anvil Uplink.

The Uplink lets you write any program outside Anvil, and talk to them from your Anvil app. (Even if your program isn’t running on a public web server!)

In this video, we use the Uplink to control a program on the Raspberry Pi from an Anvil app.

Of course, the Uplink is useful for more than just playing with the Raspberry Pi. You can connect a program you’ve already written, access files on your computer, or connect an Anvil app to code you’ve already written. You can even call into your Anvil app from uplink code - it’s a two-way API!

To learn more about the Anvil Uplink, read the Uplink section of the Anvil reference manual.

You can buy your own Raspberry Pi from the Raspberry Pi Foundation website.


Topics Covered


Constructing a UI | 0:26 - 1:07


We construct a UI where the user can enter a name to display on the Raspberry Pi.


The Uplink | 1:07 - 2:54


We enable the Uplink by clicking on Uplink in the Gear menu Gear Menu Icon.

This shows a dialog with an ‘enable’ button; clicking that button gives us a unique ID for our app:

On the Raspberry Pi, we install the Anvil Uplink library:

pip install anvil-uplink

And in a Python script on the Raspberry Pi, write we some code to print a message. The message is printed on a SenseHAT, which has an LED display that can be controlled from a simple Python library.

import anvil.server
from sense_hat import SenseHat

sense = SenseHat()

@anvil.server.callable
def show_messgage(message):
    sense.show_message(message)

anvil.server.connect("<YOUR UPLINK KEY HERE>")
anvil.server.wait_forever()

Then we run the script - the wait_forever() makes sure it stays alive waiting for function calls.


Calling the Uplink script from the app | 2:54 - 3:23


We call the show_message function from the app when the Button is clicked:

  def button_1_click(self, **event_args):
    """This method is called when the button is clicked."""
    anvil.server.call("show_message", self.name_box.text)

And that’s it! Now when you enter your name in the App:

Your name appears in scrolling lights on the Raspberry Pi:


More uses for the Uplink | 3:23 - 3:45


As well as calling from your app, you can use the Uplink to call into your app.

Let’s say you have a function in a Server Module:

# In a Server Module
def store_name(name):
  app_tables.names.add_row(name=name, when=datetime.now())

You can call it from your Python script using anvil.server.call:

# On your own machine (Rasbperry Pi, your laptop, in your server room, anywhere...)
def store_name_in_anvil(name):
    anvil.server.call("store_name", name)

So you can run Python anywhere and connect it to your app just by making function calls! You can:

Anything you can do in Python, you can integrate it into an Anvil app using the Uplink.


Try it for yourself

Build this app for yourself - Log in and follow along, or watch more tutorials in the Anvil Learning Centre.


Next up

Experiment more with the Uplink by following this step-by-step workshop. You’ll connect an app to your local machine, run unit tests, and store the results for future reference.

If you’d like to learn the basics of Anvil, start with the Hello, World! tutorial.