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.

Creating Modules

Modules allow you to write code that exists independently of your Forms. They also allow you to write code that can be used on the server and in the browser.

You can create your own modules using the Modules section of the App Browser.

These are Python modules that you can import in your Forms. You can also import them in Server Modules.

Module code is loaded in the same environment as whatever is importing it. If you import a Module from a Form, it will be loaded in approximately Python 2.7; if you import from server code, it will be loaded in whichever Python version you have chosen.

The user of an app can see source code for your Modules. They can also manipulate how Module code behaves in the browser. (Of course, this doesn’t change how Modules behave when they’re imported by Server Modules and running on Anvil’s servers.)

Global variables

Passing values between different Forms is simple. You can create a Module that has global variables, and import it in different Forms. Changing a global variable in one Form makes it available in another.

In this example app there are two different pages, each of which is a different Form. There’s a counter on each Form to display the number of times the user has clicked a link.

This is achieved in a module named Global, which has a variable named number_of_clicks. In each Link’s click handler, number_of_clicks is incremented. The Global module can be imported into each Form, which allows the number_of_clicks to be displayed in a Label.

Click to clone the example app in the Anvil designer: