Using Python Packages in Client Code
You can use Python packages in your Anvil apps, but which packages you can use depends on where your code runs. Anvil apps have two kinds of code with different Python capabilities: client code, which runs in the browser, and server-side code, which runs on the server.
Client and Server-side code
Client code runs in your browser and allows you to write Python code that manipulates your app’s user interface. However, client code cannot run every Python module or use all third-party packages. Users can also see and change client code, so you should not use it for secrets or other trusted operations.
Server-side code runs on the server and provides a full Python environment. In Anvil, server-side code is written in Server Modules. Server Modules are the right place for code that needs Python packages that aren’t available in the browser.
If you need to use a package that isn’t available in client code, you can put the code that uses the package in a Server Module and call it from your Form.
For example, if you want to use pandas to calculate an average, you can put that code in a Server Module:
import anvil.server
import pandas as pd
@anvil.server.callable
def calculate_average(values):
return float(pd.Series(values).mean())If you are using a package that isn’t already available in your app, install it using the app’s Python packages settings. See Installing Python Packages for more information.
Your Form can then call that function without importing pandas in client code:
average = anvil.server.call('calculate_average', [10, 20, 30])The code that depends on the package runs on the server, while your Form can still use the result in the browser.
For more information about client and server code, see the Client Code, Server-side code, and The Python Environment documentation.
Do you still have questions?
Our Community Forum is full of helpful information and Anvil experts.