« Anvil Tutorials
Anvil Uplink + Raspberry Pi
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.
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
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:
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.
from sense_hat import SenseHat
sense = SenseHat()
anvil.server.connect("<YOUR UPLINK KEY HERE>")
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."""
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
You can call it from your Python script using
# On your own machine (Rasbperry Pi, your laptop, in your server room, anywhere...)
So you can run Python anywhere and connect it to your app just by making function calls! You can:
- connect to your existing scripts to run calculations. Run them from your app, and make calls into
your app to store the results in Data Tables.
- connect to Jupyter notebooks and use an Anvil GUI to control your data pipelines.
- connect Anvil to a Python daemon on each machine in your network and monitor their memory usage by making system calls.
- connect Anvil to your IoT gateway and create an app to manage your IoT devices.
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.
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.