Anvil is a powerful platform for building web apps with Python. With Anvil, you can create fully functional web apps using nothing but Python code, without having to worry about HTML, CSS, or JavaScript. One of the many use cases for Anvil is creating data dashboards to display and analyze data from various sources.
In this blog post, we will explore how to use different data sources such as CSV files, databases, and analytics APIs to create a comprehensive data dashboard using Anvil. We will include detailed explanations, code snippets, and step-by-step instructions to make it easy to follow along.
Getting Started with Anvil
Before we dive into the details of creating a data dashboard with Anvil, let’s first go over how to set up an Anvil account and create a new app.
- Go to the Anvil homepage and click on the “Get Started” button to create a new account.
- Enter your email address and choose a password to sign up.
- Once you are logged in, click on the “New App” button to create a new app.
- Choose a name for your app and select a template to get started. For this tutorial, we will use the “Blank App” template.
Now that you have set up an Anvil account and created a new app, you are ready to start building your data dashboard.
Introduction to Data Sources
In order to create a data dashboard with Anvil, we need to get data into our app. There are many different ways to do this, depending on the type of data we want to use and where it is stored.
Some common data sources that can be used in a data dashboard include CSV files, databases, and analytics APIs. In the following sections, we will explore each of these data sources in more detail and show how they can be used in an Anvil app.
Using CSV Files as a Data Source
One of the simplest ways to get data into an Anvil app is by uploading a CSV file. A CSV (Comma-Separated Values) file is a plain text file that stores tabular data (numbers and text) in a simple format. Each line of the file is a row of data, and the values within each row are separated by commas.
Anvil provides an easy-to-use FileLoader
component that allows users to upload files directly from their computer. Once the file is uploaded, we can use the anvil.media
module to read the contents of the file and convert it into a format that can be used in our app.
Here is an example of how to use a CSV file as a data source for a data dashboard in Anvil:
import anvil.media
import pandas as pd
# Use the FileLoader component to upload a CSV file
file = file_loader_1.file
# Read the contents of the CSV file into a Pandas DataFrame
with anvil.media.TempFile(file) as filename:
df = pd.read_csv(filename)
# Use the DataFrame to populate a DataGrid component
data_grid_1.items = df.to_dict('records')
Code language: PHP (php)
In this example, we use the FileLoader
component to allow the user to upload a CSV file. Once the file is uploaded, we use the anvil.media.TempFile
context manager to create a temporary file on the server that we can read from. We then use the pandas.read_csv
function to read the contents of the CSV file into a Pandas DataFrame.
Pandas is a popular Python library for working with tabular data. It provides many powerful tools for reading, manipulating, and analyzing data in various formats including CSV files. In this example, we use Pandas to read the contents of the CSV file into a DataFrame object.
Once we have the data in a DataFrame, we can use it to populate a DataGrid
component in our app. A DataGrid
is an Anvil component that displays tabular data in rows and columns. In this example, we convert the DataFrame into a list of dictionaries using the to_dict
method and set it as the items
property of the DataGrid
.
Using Databases as a Data Source
Another way to get data into an Anvil app is by connecting to an external database. A database is an organized collection of structured data stored on a computer or server. Databases are commonly used in web applications to store and retrieve large amounts of data quickly and efficiently.
Anvil provides built-in support for connecting to several popular databases such as PostgreSQL, MySQL, and Microsoft SQL Server. This makes it easy to retrieve data from an external database and use it in our app.
Here is an example of how to use a PostgreSQL database as a data source for a data dashboard in Anvil:
import anvil.postgresql
# Connect to a PostgreSQL database
db = anvil.postgresql.connect("postgresql://user:password@host/database")
# Query the database and fetch the results
results = db.query("SELECT * FROM my_table")
# Use the results to populate a DataGrid component
data_grid_1.items = list(results)
Code language: PHP (php)
In this example, we use the anvil.postgresql.connect
function to connect to a PostgreSQL database. PostgreSQL is a popular open-source relational database management system. It is widely used in web applications and provides many advanced features such as transactions, subqueries, and user-defined functions.
Once we have established a connection to the PostgreSQL database, we can use the query
method of the database connection object to execute a SQL query and fetch the results. SQL (Structured Query Language) is a standard language for managing and querying relational databases. In this example, we use a simple SELECT
statement to retrieve all rows from a table named my_table
.
Finally, we convert the results of the query into a list and set it as the items
property of the DataGrid
. This will display the data from the PostgreSQL database in our Anvil app.
Using Analytics APIs as a Data Source
Another way to get data into an Anvil app is by using analytics APIs from popular services such as Google Analytics or Mixpanel. An API (Application Programming Interface) is a set of rules and protocols that allows different software applications to communicate with each other. Analytics APIs provide a way for developers to retrieve data from analytics services programmatically.
Here is an example of how to use the Google Analytics API as a data source for a data dashboard in Anvil:
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
# Set up OAuth credentials for accessing the Google Analytics API
creds = Credentials.from_authorized_user_info(info={
'client_id': 'YOUR_CLIENT_ID',
'client_secret': 'YOUR_CLIENT_SECRET',
'refresh_token': 'YOUR_REFRESH_TOKEN',
})
# Build the Google Analytics API client
ga = build('analyticsreporting', 'v4', credentials=creds)
# Query the Google Analytics API and fetch the results
response = ga.reports().batchGet(
body={
'reportRequests': [{
'viewId': 'YOUR_VIEW_ID',
'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
'metrics': [{'expression': 'ga:sessions'}],
'dimensions': [{'name': 'ga:country'}]
}]
}
).execute()
# Extract the data from the API response
data = response['reports'][0]['data']['rows']
# Use the data to populate a DataGrid component
data_grid_1.items = [{'country': row['dimensions'][0], 'sessions': row['metrics'][0]['values'][0]} for row in data]
Code language: PHP (php)
In this example, we use OAuth credentials to authenticate with the Google Analytics API. OAuth is an open standard for authorization that allows users to grant third-party applications access to their data without sharing their login credentials. In this case, we use OAuth to grant our Anvil app access to our Google Analytics data.
Once we have set up our OAuth credentials, we can build an API client using the googleapiclient.discovery.build
function. This function takes care of all the details of communicating with the Google Analytics API, such as constructing HTTP requests and parsing JSON responses.
Next, we use our API client to query the Google Analytics API and fetch the results. In this example, we use the batchGet
method of the reports
resource to retrieve data about sessions and countries for the past 7 days. The details of how to construct an API request will vary depending on which analytics service you are using and what data you want to retrieve.
Once we have received a response from the API, we can extract the data from it and use it in our app. In this example, we extract the rows of data from the response and use them to populate a DataGrid
component in our Anvil app.
Customizing Your Data Dashboard
The examples above show how to use different data sources such as CSV files, databases, and analytics APIs to create a basic data dashboard using Anvil. However, there are many ways you can customize your dashboard to make it more powerful and user-friendly.
For example, you could add filters or search functionality to allow users to interactively explore the data. You could also add charts or maps to visualize the data in more interesting ways. You could even combine multiple data sources into a single dashboard to provide a more comprehensive view of your data.
Here are some ideas for customizing your data dashboard:
- Add filters: Allow users to filter the data by adding drop-down menus or search boxes above your
DataGrid
. You can use Anvil’s built-inDropDown
orTextBox
components for this. For example, if your data includes a column for “Country”, you could add aDropDown
component that allows users to filter the data by country. When the user selects a country from the drop-down menu, you can update theitems
property of theDataGrid
to only show rows where the “Country” column matches the selected country.
# Add a DropDown component above the DataGrid
drop_down_1 = DropDown(items=['All'] + list(df['Country'].unique()))
self.add_component(drop_down_1, above=data_grid_1)
# Define an event handler for when the user selects an item from the DropDown
def drop_down_1_change(self, **event_args):
# Get the selected country from the DropDown
selected_country = self.drop_down_1.selected_value
# Filter the data based on the selected country
if selected_country == 'All':
filtered_data = df.to_dict('records')
else:
filtered_data = df[df['Country'] == selected_country].to_dict('records')
# Update the DataGrid with the filtered data
self.data_grid_1.items = filtered_data
# Bind the event handler to the DropDown's change event
drop_down_1.set_event_handler('change', drop_down_1_change)
Code language: PHP (php)
In this example, we add a DropDown
component above our DataGrid
and populate it with a list of unique countries from our data. We also define an event handler function that is called whenever the user selects an item from the DropDown
. In this function, we get the selected country and use it to filter our data. We then update the items
property of our DataGrid
with the filtered data.
- Add charts: Visualize your data using charts such as bar charts, pie charts, or line charts. Anvil provides built-in support for creating charts using the
Plotly
library. You can add aPlot
component to your app and use itsdata
property to specify the data and chart type. For example, if you want to create a bar chart that shows the number of sessions per country, you could use code like this:
import plotly.graph_objs as go
# Get the data from the DataGrid
data = data_grid_1.items
# Group the data by country and calculate the total number of sessions per country
grouped_data = {}
for row in data:
country = row['country']
sessions = int(row['sessions'])
if country not in grouped_data:
grouped_data[country] = 0
grouped_data[country] += sessions
# Create a bar chart using Plotly
chart_data = [go.Bar(x=list(grouped_data.keys()), y=list(grouped_data.values()))]
# Add a Plot component below the DataGrid
plot_1 = Plot(data=chart_data)
self.add_component(plot_1, below=data_grid_1)
Code language: PHP (php)
In this example, we get the data from our DataGrid
and group it by country. We then calculate the total number of sessions for each country and use this data to create a bar chart using Plotly. Finally, we add a Plot
component below our DataGrid
and set its data
property to display our chart.
- Add maps: Visualize your data on a map using components such as
GoogleMap
orLeaflet
. These components allow you to display a map in your app and add markers or other overlays to represent your data. For example, if your data includes latitude and longitude coordinates, you could add aGoogleMap
component to your app and use itsadd_marker
method to add markers for each row of data.
# Add a GoogleMap component below the DataGrid
map_1 = GoogleMap()
self.add_component(map_1, below=data_grid_1)
# Get the data from the DataGrid
data = data_grid_1.items
# Add markers to the map for each row of data
for row in data:
lat = row['latitude']
lng = row['longitude']
map_1.add_marker(lat, lng)
Code language: PHP (php)
In this example, we add a GoogleMap
component below our DataGrid
and use its add_marker
method to add markers to the map for each row of data. We get the latitude and longitude coordinates from our data and use them to position the markers on the map.
- Combine multiple data sources: Create a more comprehensive dashboard by combining data from multiple sources. For example, you could use a database as your primary data source and then enrich the data with additional information from an analytics API. To do this, you would need to write custom code that retrieves the data from each source and combines it into a single dataset that can be displayed in your app.
import anvil.postgresql
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
# Connect to a PostgreSQL database
db = anvil.postgresql.connect("postgresql://user:password@host/database")
# Query the database and fetch the results
db_data = list(db.query("SELECT * FROM my_table"))
# Set up OAuth credentials for accessing the Google Analytics API
creds = Credentials.from_authorized_user_info(info={
'client_id': 'YOUR_CLIENT_ID',
'client_secret': 'YOUR_CLIENT_SECRET',
'refresh_token': 'YOUR_REFRESH_TOKEN',
})
# Build the Google Analytics API client
ga = build('analyticsreporting', 'v4', credentials=creds)
# Query the Google Analytics API and fetch the results
response = ga.reports().batchGet(
body={
'reportRequests': [{
'viewId': 'YOUR_VIEW_ID',
'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
'metrics': [{'expression': 'ga:sessions'}],
'dimensions': [{'name': 'ga:country'}]
}]
}
).execute()
# Extract the data from the API response
api_data = response['reports'][0]['data']['rows']
# Combine the data from the database and the API
combined_data = []
for db_row in db_data:
country = db_row['country']
api_row = next((row for row in api_data if row['dimensions'][0] == country), None)
if api_row is not None:
sessions = api_row['metrics'][0]['values'][0]
combined_row = dict(db_row)
combined_row['sessions'] = sessions
combined_data.append(combined_row)
# Use the combined data to populate a DataGrid component
data_grid_1.items = combined_data
Code language: PHP (php)
In this example, we retrieve data from a PostgreSQL database and a Google Analytics API and combine it into a single dataset. We use a for
loop to iterate over each row of data from the database and find the corresponding row of data from the API based on the country. We then create a new combined row of data that includes information from both sources and add it to our combined_data
list. Finally, we use this combined data to populate our DataGrid
.
These are just a few ideas for customizing your data dashboard with Anvil. With its powerful Python-based platform and flexible components, there are virtually no limits to what you can create. We encourage you to experiment with different data sources, visualization techniques, and user interactions to create a unique and engaging dashboard for your project.
Conclusion
In conclusion, Anvil provides a powerful and flexible platform for creating comprehensive data dashboards using various data sources such as CSV files, databases, and analytics APIs. By following the examples and suggestions in this blog post, you can easily create your own custom data dashboard using Anvil. We encourage you to experiment with different data sources and visualization techniques to create a unique and engaging dashboard for your project.