In this step-by-step guide, I’ll show you how to build a business dashboard in just a few minutes, using nothing but Python. We’ll plot data from four different sources, and highlight key statistics from each of these plots.

The finished dashboard will look something like this:

Click the following link to clone the finished app and explore it yourself, or read on as we take a step-by-step guide to creating your own business dashboard with nothing but Python.

Table of contents:


Building your UI

Plotting data:

  1. Anvil’s built-in database

  2. An external SQL database

  3. A private SQL database


Styling your dashboard

Adding numerical statistics


A simple dashboard is a great way to measure how your business is performing in real time. Monitoring KPIs like revenue growth can help you to stay on track, and presenting a few key metrics in data visualisations helps you focus on the data that matters the most.

Building a business dashboard can be a complex and lengthy process. Data comes in a range of shapes and sizes, and from a number of different sources. You might have data stored in an SQL database somewhere, being aggregated by a script that’s only available inside your company network and coming from an external analytics API. Aggregating this data and displaying it in a simple, effective way takes time using traditional web development tools.

With Anvil, it takes minutes to build a business dashboard with Python and display it on the web. Anvil gives us a range of tools for accessing and aggregating data from a range of sources to create comprehensive and informative data visualisations. The visual designer makes it easy to build your UI display your data on the web.

Keep reading to learn how to create your own business dashboard with nothing but Python.

Building your UI

Open the Anvil Editor to get started.

Click the ‘Create New App’ button and select the Material Design theme. You are now in the Anvil Editor.

First, name the app. Click on the name at the top of the screen and give it a name.

The toolbox on the right contains components that you can drag-and-drop onto the Design view in the centre.

Drop a Label into the blue bar at the top, where it says ‘Drop title here’. In the Properties panel on the right, enter a title into the text section.

Gif dropping label onto navbar

Next, drag and drop a Card onto the page. In the Properties pane on the right hand side, click the ‘MORE’ button to the right of ‘Container Properties’, and select full_width_row. This will make our card stretch to fill the width of the page.

Gif dropping card onto app

Next, drop a plot component inside the card.

Add three more cards and plots to your app in a 2 x 2 grid-style layout, and make sure all of your cards have the full_width_row property. You can resize your plots by dragging the resizer.

Resizing plots in Design view

Plotting the data

We’ve built the basic structure of our UI. Let’s go ahead and plot some data.

We’ll use Plotly to create the plots. Click on the ‘Code’ tab to go to Code view. This is the Python class that describes the Form you’ve just created.

You’ll notice that Plotly is already imported:

import plotly.graph_objects as go

Let’s write a Python function to plot our first Bar chart. Edit your code view to look like this (make sure your build_revenue_graph function isn’t inside the __init__ function):

from anvil import *
import plotly.graph_objects as go

class Form1(Form1Template):

  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    # Any code you write here will run when the form opens.
  def build_revenue_graph(self): = go.Bar(y=[100,400,200,300,500])

We want to show this graph when our app first opens, so let’s call the build_revenue_graph function in the __init__ method of our form:

def __init__(self, **properties):
  # Set Form properties and Data Bindings.

  # Any code you write here will run when the form opens.
  self.build_revenue_graph() #<-------------- ADD THIS LINE

Click Run and you’ll see that you’ve plotted your first Bar Chart.

We’ve seen how to construct the UI and plot some data. Now let’s look at plotting real data from a range of data sources.

We’ll plot data from four different sources:

  1. Anvil’s built-in database
  2. An external SQL database (PostgreSQL in this example)
  3. A private SQL database (Say, for example, you have a database that can only be accessed from within your corporate network)
  4. An external REST API

Using data from:

1. Anvil’s built-in database

Let’s say one of our main KPIs is revenue growth – and our revenue data is stored in Anvil’s built-in database. I’ll show you how to create a table, add some data to it, and display this on your dashboard.

Create the database table

Click the ‘+’ next to ‘Services’ in the panel on the left.

Adding new service

Click on ‘Data Tables’, then click ‘Add a table’ in the light blue box at the top, then ‘Create new table’. You’ll be prompted to give your table a name - let’s call it ‘Revenue’ for this example. Then add a ‘number’ column - let’s call this ‘amount’, and a ‘date’ column - let’s call this ‘date’.

Adding columns to data table

Double click the cells to add some data to your table - it should look something like this:

Screenshot of data added to data table

Get the data

Click on the + next to ‘Server Modules’ in the panel on the left. You’ll see some code with a yellow background. This is the Python code that runs on the server. We’ll define a function that requests data from the ‘Revenue’ data table we just created. Copy and paste this into the bottom of your Server Module:

def get_revenue():

We’ve marked the function @anvil.server.callable. That’s all we need to make it accessible from client-side code when we build our web page. (No web server required!)

Plot the data

All that’s left is to display this data as a graph in our web app. When the app opens, we call the query function we’ve just defined, and construct a bar graph with the data.

Go back to Form1 and change your build_revenue_graph function to look like this:

def build_revenue_graph(self):
  db_data ='get_revenue') = go.Bar(x = [x['date'] for x in db_data],
                            y = [x['amount'] for x in db_data],

Click the ‘Run’ button and you’ll see that your bar graph is now being populated with data from Anvil’s database.

Next, I’ll show you how to populate a graph with data from an external SQL database.

2. Data from an External SQL database

Let’s say one of our primary business concerns is user acquisition. But our user records live in an SQL database. We want to connect to this database, query it, and show this data on our dashboard.

Get the data

Anvil apps are just Python, so we can use the standard Python tools to query our database!

In this case, our database is PostgreSQL, so we’ll use the standard Psycopg2 library. Add this line to the top of your Server Module:

import psycopg2
Using psycopg2 requires an individual plan or higher

Now write a function in your Server Module to query the database, and return the results. Here’s an example query you might perform to track user acquisition, if your database has a users table with a signup_date field:

def get_user_signups():
  # Connect and retrieve data from your existing database - example code below
  conn = psycopg2.connect(" dbname=my_app user=postgres password=secret")
  cur = conn.cursor()
     SELECT COUNT(${html`*`}), DATE_TRUNC('week', signup_date) AS d
          FROM users
          WHERE signup_date > NOW() - INTERVAL '3 months'
          GROUP BY DATE_TRUNC('week', signup_date)
          ORDER BY d;
  return list(cur)

For more information on using SQL databases with Anvil, click here.

If you don’t have an SQL database available, you can use some dummy data for now. Remove the import psycopg2 statement, and copy and paste this into the bottom of your Server Module:

def get_user_signups():
# Here's some dummy data that you might return from your database, as an example
  return [{'signups': 120, 'date':datetime(2019, 6, 10, 0, 0)}, 
          {'signups': 180, 'date':datetime(2019, 6, 3, 0, 0)}, 
          {'signups': 150, 'date':datetime(2019, 5, 27, 0, 0)}]

We’re returning datetime objects so you’ll also need to import datetime in your Server Module:

from datetime import datetime

Plot the data

All that’s left is to display this data as a graph in our web app.

Go to Form1 and add a build_signups_graph function which looks like this:

def build_signups_graph(self):
  signups ='get_user_signups')
  scatter = go.Scatter(x = [x['date'] for x in signups],
                       y = [x['signups'] for x in signups],
                       fill = 'tozeroy',
                       line=dict(color='#2196f3')) = scatter

Add this to the __init__ method of Form1:


Click run and your dashboard should look something like this:

Screenshot of dashboard with two graphs

Next, I’ll show you how to populate a graph with data from a private SQL database.

3. Private SQL database

Let’s imagine we’ve been testing some marketing strategies, and have collected data on the number of visits to our page for each strategy. But this data is held in a private SQL database that can only be accessed from within our company network.

You can connect code running anywhere to your Anvil app, using the Anvil Uplink. You can write code to query your private database, run it inside your company network, then use the Uplink to connect to your app – so now you can call it from the cloud!

Here’s how it works:

Diagram of Anvil uplink

Start by selecting the Anvil Uplink in your app menu.

Screenshot of choosing uplink in gear menu

Click ‘Enable the Anvil Server Uplink for this app’, and you’ll see an authentication key that you will use to connect your code to your app:

A modal from the IDE showing a randomised key you can use to connect to your Anvil app from any Python process.

To use the Anvil uplink library in your Python project, first run pip install anvil-uplink on your machine.

Then create a file called containing these lines:

import anvil.server

(Remember to replace 'your-uplink-key' with the Uplink key for your app!)

Run the script (type python in a Terminal window). You’ll see the following lines in your Terminal window if the connection has been successful:

Connecting to wss://
Anvil websocket open
Authenticated OK

That’s all you need to do to connect your machine to your Anvil app.

Now that we’ve connected our app to our local machine, we can write a function to return data to be plotted in our dashboard.

In your script, define a function called get_marketing_data, and decorate it with @anvil.server.callable to make it available inside your Anvil app. We’ll start by making it return some test data:

def get_marketing_data():
    # access data on your local machine and return as a Python list
    return [{'strategy':'Strategy A', 'count':200}, 
            {'strategy':'Strategy B', 'count':185}, 
            {'strategy':'Strategy C', 'count':175}]

Stop and re-run your script to apply this change.

Plot the data

All that’s left is to display this data in our dashboard. We’ll call the get_marketing_data function from client code and put the data into a plot just as we did in Chapter 3.

Go to Form1’s ‘Code’ view and add this function:

def build_marketing_graph(self):
  # We’re calling a function on your local machine from the web!
  # Get the data and store it as 'marketing_data'
  marketing_data ='get_marketing_data')
  # Create a Line plot with this data, and change the colour of the line = go.Scatter(x = [x['strategy'] for x in marketing_data],
                             y = [x['count'] for x in marketing_data],

We want to plot this data when our app runs, so let’s call it from the __init__ method of Form1:


Run your app

Click run and you’ll see we’ve populated three of our four graphs.

Next, I’ll show you how to populate a graph with data from a REST API.


Let’s say you’re receiving real-time data from an external analytics provider via their REST API. Keeping track of this data is something you’d want to do with your dashboard.

You can integrate against REST APIs using Anvil’s http module. To make a GET request, you can run:


Alternatively, we could use the standard python requests library from our Server Module, like so:

import requests


Note: A paid plan is required to use the requests module

Get the data

For illustrative purposes, we’ll get our data from the OpenWeatherMap API. This gives real-time weather data in JSON format.

First, let’s import Anvil’s http module. Add this line to the top of your Server Module:

import anvil.http

Add this function to your Server Module. It takes in latitude and longitude parameters and returns data from the API:

def get_weather_data(latitude, longitude):
  resp = anvil.http.request(",daily&appid=%s&units=imperial" 
        % (latitude, longitude, "43eda1fbcf0b07043eeeac93ae54e1e0"),
  # convert timestamp to datetime object
  time = datetime.fromtimestamp(resp['current']['dt'])
  # return time and temperature data
  return {

Plot the data

Our api is providing real-time temperature data, so we’ll want to store these values as we retrieve them. Go back to Form1, and create a local variable in the __init__ method:

# Initialise empty list to store real-time temperature data
self.temp_data = []

Next, add a function to Form1 to plot the data:

def build_weather_graph(self):
  # Retrieve data from api. We'll use latitude and longitude for Cambridge for this example.
  weather_data ='get_weather_data', 52.2053, 0.1218)
  # Add time and temperature data from api to our temp_data variable.
  self.temp_data.append({'time':weather_data['time'], 'temp':weather_data['temp']}) = go.Scatter(y=[x['temp'] for x in self.temp_data], 
                                x=[x['time'] for x in self.temp_data], 

Then, let’s build the graph when the app is opened by adding build_weather_graph it to the __init__ method of Form1. Your temp_data variable is used in the build_weather_graph function, so make sure you initialise the variable before you call self.build_weather_graph.

Your __init__ method should look like this:

# Any code you write here will run when the form opens.
self.temp_data = []

Make it real-time

We want to update our weather graph in real time, to see how the temperature is changing during the day. To do this, go to the Design view of your Form and add a ‘Timer’ component to the page.

We can set the ‘tick’ interval of our timer – that’s how often we’ll run the function that builds our temperature graph. The default interval is 0.5 seconds – let’s slow that down to 2 seconds. Click on the timer, and in the Properties panel on the right, change the interval from 0.5 to 2 seconds.

Gif of adding timer to app

Next, we need to refresh our temperature graph when the timer ticks. Double click the timer to access the Python code that runs when the timer ‘ticks’, and change it to look like this:

def timer_1_tick(self, **event_args):
  """This method is called every [interval] seconds. Does not trigger if [interval] is 0."""
  with anvil.server.no_loading_indicator:

Click the Run button, and you’ll see that your temperature graph is displaying real-time data from OpenWeatherMap’s REST API.

We’ve built a dashboard with data from four different sources and displayed on the web, all with nothing but Python.

Styling your dashboard

Anvil lets you customise the visual appearance of your app completely. Let’s tweak the styling of our dashboard a little bit.

We can style our Plotly graphs by modifying their Layout property. We do this by setting it to a dictionary describing the layout of the plot. Go back to Form1 and add this function:

def style_plot(self, plot):
  plot.layout = go.Layout(
                          # expand the graphs
                              l=50, #left margin
                              r=50, #right margin
                              b=50, #bottom margin
                              t=50, #top margin
                          font=dict(family='Noto Sans', size=10),
                          # Format x-axis
                                family='Noto Sans',
                          # Format y-axis
                                  family='Noto Sans',

We’ve specified a nonstandard font (Noto Sans), so we’ll need to import it from Google Fonts. Scroll down to ‘Theme’ in the panel on the left, and click on ‘Assets’. In the dropdown at the top, select theme.css, and add this line to the top of your css:

@import url('');

While we’re at it, let’s use Noto Sans for all of our app’s text, not just the plots. Scroll down to the ‘Typography’ section of theme.css (around line 83), and modify the css that styles the body of our app to look like this:

body {
  font-family: 'Noto Sans', sans-serif;
  font-size: 14px;
  line-height: 1.4286;
  background-color: #fafafa;

Finally, we need to pass each of our four graphs to the style_plot function to apply our changes. We’ll also add some titles to our graphs at the same time.

For example, to style our revenue graph and add a title, we add two lines to the end of our build_revenue_graph function so that it looks like this:

def build_revenue_graph(self):
  revenue_data = [x['amount'] for x in'get_revenue')]
  date_data = [x['date'] for x in'get_revenue')] = go.Bar(y=revenue_data,
  self.style_plot(self.plot_1) # <----------------- ADD THIS LINE
  self.plot_1.layout.title = "REVENUE GROWTH" # <---ADD THIS LINE

Do this for all four graphs and your dashboard will look something like this:

Screenshot of styled dashboard with four working graphs

Adding numerical statistics

Finally, we’ll add some headline numerical statistics to the top of our dashboard.

Drag and drop four cards along the top of your dashboard, and set the container property of each of these to full_width_row.

Drop a label into each of the four cards. Add text to each of the labels using the Properties panel on the right hand side, and make each of them bold. Your dashboard should look something like this:

Screenshot of dashboard with labels for headline statistics

Then, drop another label into each card, and give each label a name. We’ll call them revenue_label, signups_label, marketing_label, and weather_label for our example:

Screenshot of naming labels in cards at top of dashboard

Now, let’s populate those labels with our headline numerical statistics.

Let’s amend our graph functions to also populate our headline statistics.

First, add these lines to your build_revenue_graph function:

max_revenue = sorted(db_data, key=lambda x: x['amount'], reverse=True)[0]
self.revenue_label.text = f"{max_revenue['date']:%d %b %Y}, {max_revenue['amount']:,}"

Add these lines to your build_signups_graph function:

max_signups = sorted(signups, key=lambda x: x[0], reverse=True)[0]
self.signups_label.text = f"{max_signups[1]:%d %b %Y}, {max_signups[0]}"

Add these lines to your build_marketing_graph function:

max_hits = sorted(marketing_data, key=lambda x: x[1], reverse=True)[0]
self.marketing_label.text = f"{max_hits[0]}, {max_hits[1]} hits"

And finally, add these lines to your build_weather_graph function:

max_temp = sorted(self.temp_data, key=lambda x: x[1], reverse=True)[0]
self.weather_label.text = max_temp[1]

Click run and you’ll see your headline numerical statistics populated along the top of your dashboard!


And that’s it. You have a working business dashboard built with nothing but Python. It’s fetching data from Anvil’s built-in database, from an external SQL database, from a private database, and from a REST API.

Your dashboard is already live on the internet. Go to Publish App in the Gear Menu gear icon for details.

You can also use the following link to clone the finished app and explore it yourself:

Happy dashboarding!

Want to dig into how this app works? Our tutorials show you all the pieces these apps are made from, and more.

What next?

Learn how to do something new with Anvil

The Data Dashboard tutorial is a great follow on from this tutorial!

Would you like to see some examples of dashboards built with Anvil? Check out our dashboard gallery: