Things you might have missed

Hi folks!

While I was digging through the reference docs I noted down a few useful features that you may not have spotted. Here they are:

Uplink setup function

The Uplink can run setup code when connecting. Set the init_session argument to a function to call immediately after the Uplink connects:

def setup():
  anvil.users.login_with_email("my_user@example.com", "MY_PASSWORD")

anvil.server.connect("[YOUR UPLINK KEY]", init_session=setup)

You can also suppress the connection message - normally the anvil.server.connect prints something like this:

Connecting to wss://anvil.works/uplink
Anvil websocket open
Authenticated OK

You can suppress it using the quiet kwarg:

# Connect without printing to console
anvil.server.connect("[YOUR UPLINK KEY]", quiet=True)

This is the section of the docs that this comes from.

Allowing your users to connect via Uplink

You’re not restricted to using the Uplink with your own apps. If somebody else gives you their Uplink key, you can connect to their app (use Client mode for security).

Similarly, if you want your users to connect using a script rather than via a web GUI, you can give them a script containing a Client uplink key and an anvil.server.connect() call.

Registering your server function under a different name

Server functions don’t have to take the name of the function they’re decorating. You can pass a string into the decorator to anvil.server.call them by a different name.

@anvil.server.callable("my_func")
def foo():
  return 42

This can be calculated at runtime, so if you have multiple Uplink connections, you can append an ID based on which instance of the Uplink you’re connecting from.

import uuid
UPLINK_ID = uuid.uuid4()

@anvil.server.callable("my_func%s" % UPLINK_ID)
def foo():
  return 42

This is the section of the docs that this comes from.

What can session and cookies store?

anvil.server.session stores data that persists for this browser session. anvil.server.cookies stores data between sessions.

Session and cookies can store anything a Server Function can return, except for Media Objects. This includes Data Tables Rows.

See Sessions and Cookies in the docs for more info.

Google Maps

You can do a lot with Google Maps, including arbitrarily shaped overlays. Here’s an app that draws a shaded area around a popular Cambridge landmark:

https://anvil.works/build#clone:I5IFEEDK7QNMTYHN=FPJCIMSCBTI5BU3BOTBF77YJ

Check out our Google Maps example app for more inspiration:

https://map-examples.anvil.app

The reference docs section on the Google Map component, and the API Docs sections on anvil.GoogleMap and anvil.GoogleMap.Data can help you here (you might find Google’s API Docs useful too.)

Users Service Single-Sign-On

By sharing a single Users table between many apps, you can get single-sign-on between your apps. A user signed into one app is signed in to any others that use the same Users table.

(Also, you can use any table to store your user data, it doesn’t have to be the one that’s auto-generated for you when you create the Users Service.)

Defining your own error handling

You don’t have to have the default error popup box when an uncaught exception occurs.

image

You can define a custom default error handler using

set_default_error_handling(my_function)

It gets passed the Exception object of any uncaught exception as its only argument.

See the Error Reporting section of the docs for more info.

Using notifications in a with block

If you use a notification in a with block, it will be displayed as long as
the body of the block is still running. It will then disappear.

with Notification("Please wait..."):
  # ... do something slow ...

See Notifications in the reference docs for more about Notifications.

Adding components to Data Grid columns in code

You can add components to Data Grid columns programmatically by specifing the column’s unique id as the column= argument to add_component:

  grid.columns = [
    { "id": "A", "title": "Name", "data_key": "name" },
    { "id": "B", "title": "Address", "data_key": "address" },
  ]
  for person in data:
    row = DataRowPanel(item=person)
    row.add_component(TextBox(text=person['name']), column="A")
    grid.add_component(row)

See the Data Grids section of the reference docs for much more on Data Grids.


That’s it from me

I hope you can use some of these tips to improve what you’re working on! Does anybody have any other tips like this they’d like to share?

12 Likes