At the top of your script, import the Anvil Uplink library:

# Import the Anvil Uplink library
import anvil.server

Below your import statements, run this line to connect to your script to your Anvil app. Replace <your-uplink-key> with the key shown in the Uplink dialog in step 3.

# Connect to your Anvil app
anvil.server.connect('<your uplink key>')

Add @anvil.server.callable to the line above your get_items function. This makes it possible to call this function from your Anvil app.

@anvil.server.callable
def get_items():
  # ... etc ...

Finally, add this line to the end - it keeps the script alive waiting for connections from your app:

# Wait for connections
anvil.server.wait_forever()

(Also, remove the print(get_items()) from the end of the script)

Your full script should now look like this:

import anvil.server
import psycopg2

anvil.server.connect('<your uplink key>')

conn = psycopg2.connect(
  host="35.177.174.19",
  user="your_database_id",
  password="your_database_password")
conn.set_session(autocommit=True)

cur = conn.cursor()

@anvil.server.callable
def get_items():
  cur.execute('SELECT * FROM inventory;')
  items = cur.fetchall()
  return [
    {'id': item[0], 'name': item[1], 'quantity': item[2]}
    for item in items
  ]

anvil.server.wait_forever()

Now we have the Uplink part of the system done:

We&rsquo;ve set up the database and proxied to it using the Uplink.

We’ve set up the database and proxied to it using the Uplink.