DataTables, AngularJS, and Django

Qualtrics production system is comprised of many micro-services. Visualizing the network connections among them can be a daunting task, so I decided to write an internal tool to help myself with it. What if I could list out the micro-services and their dependencies in a table, and then slice and dice the data by filtering and sorting by the columns to interact with the data? The amount of data is small, in the order of thousands of relationships, so a lightweight tool would suffice.

Well, this sounds like a perfect job for the DataTables jQuery plug-in. Out of the box, it lets you sort by multiple columns by holding down the SHIFT key, provides paginated UI, and has a nice search box for filtering. I integrated it with AngularJS, served the data from Django REST Framework, and put them inside a docker container.

This article is a mini-tutorial on how to put these open source components together. Just to illustrate what I am talking about, here is a screenshot of the internal tool.

Figure 1. Screenshot of the Node Group Dependency Tab (with fake data)

The tool finds network connections within production docker containers as well as bare metal machines, aggregates them by the source and destination micro-services, and displays connection stats between them. As you can see in the screenshot, I filtered the list to show only the dc1 datacenter and sample service only. Then, sorted the table by Source Group first, and then Short-lived Connections in descending order. This UI makes it a snap to find my dependencies, and see how many network connections I am creating from a given micro-service tier to another.

Serving Static Assets From Django

Before we get on our way to write some JavaScript code, let’s set up a lightweight web server that can serve them. I used Django for that. Getting up and running with Django is straight-forward, as described in the official Getting Started Guide.

Setting up static assets may require a few tweaks, though. You can follow the official documentation about managing static files, and this will get your static assets up and running just fine, as long as you have DEBUG = True in your file. However, before you release your application to production, you should set DEBUG = False, which enables the cache busting feature. Unless you do this, the user’s browser will cache your static assets (like JavaScript files), and may not pick up the changes when you release new versions of your application.

If you are using a web server like nginx to serve static assets, you can simply point its configuration to the static files directory that you set up. However, my internal tool is very simple, and I prefer to serve the static assets from Django itself, so that I can ship a single docker container that contains everything. The trouble is that Django will not serve the static assets directly if you set DEBUG = False. It expects an external web server (like nginx) to serve the static assets on its behalf in production mode.

You can introduce the White Noise module into your code to solve this problem. First, import it in your file, by adding the two lines annotated below.

Second, make sure that all your static files are referenced using Django’s static file mark up in your HTML template files. They should look something like:

Finally, add the following to your file.

Once you set DEBUG = False, you need to make sure that the static files are installed into the target directory, in this case /var/www. You can do that by running:

This installs all your static assets with unique names, and updates references to them in your HTML. This way, you don’t have to worry about browser cache eclipsing the newer versions of JavaScript or CSS files. Now that static assets are taken care of, let’s move on to the fun part: integrating DataTables and AngularJS!

Integrating DataTables with AngularJS

We will be using DataTables version 1.10.12, AngularJS version 1.5.7, and angular-datatables, which is a nice angular directive that allows easy integration of the two components. Incidentally, the newest documentation of angular-datatables uses Angular 2 and TypeScript to illustrate example usages. If you are still using an earlier version of AngularJS and JavaScript, you can view the archived documentation, or this blog will help you get started easily.

In case you are new to AngularJS itself, I would like to show the main entry point into our AngularJS app. Here is my main app.js file. My application is randomly named “novascotia.” I omitted some lines to fit the code into a blog format.

As you can see above, it doesn’t take much to create an AngularJS app. All the modules listed in angular.module call above should be loaded by individual <script> tags, as follows:

Here node-group-deps.js is the file that will contain our code that uses angular-datatables to load DataTables with AngularJS. Before we look at that, here is the HTML template that we will be referring to in our AngularJS directive:

This snippet of HTML is the template used by our AngularJS directive defined. It provides the <table> element that a data table will be attached to. The key attribute here is datatable, which links the DOM element to angular-datatables module. As you can see, this element is quite empty. We add the desired columns and data rows using code, as shown below:

At the very top of the file, you can see that I am loading the “datatables” module, which corresponds to angular-datatables module. Below that, we are defining the nodeGroupDepsTab directive, and the corresponding nodeGroupDepsTabController that defines the behavior. By AngularJS convention, the string nodeGroupDepsTab in .directive() call will define the new directive called node-group-deps-tab. So, I can simply add <node-group-deps-tab> tag anywhere in my AngularJS app to display the data table defined by this code.

This code defines the three attributes used by angular-datatable to configure our data table (refer to the template that we listed earlier): dtOptions, dtColumns, and dtInstance. They are instance variables of the controller object, and are accessible via the controllerAs variable named page in the template.

The DTOptionBuilder is used to define dtOptions. One nice feature here is that you can use a function that returns a promise object (in this case the queryAPI function) to populate the contents of the data table. As you can see in the code, queryAPI function uses the $http angular service to perform a GET against an API endpoint, then processes the result to produce a format suitable to render in an HTML UI. For each record in the result, we are counting the long-lived and short-lived connections, so that we can populate the columns the Long-lived Connections and Short-lived Connections as seen in the screenshot. Here is a sample of what gets returned from the API call:

The nested jQuery.each loops in the code process each item in the API response, and builds an array of “row data” objects that will be used to render the data table. The shapes of the columns are defined in the dtColumn variable. Besides .fromFnPromise, you can control other data table options such as display length or the menu of the available display lengths. Note that you can use .withOption method to set arbitrary options available on the original jQuery DataTable object. For example, the stateSave is a useful DataTables option that preserves the current sort order, pagination and filter state across site navigation.

The dtColumns is an array of column definitions defined by DTColumnBuilder objects. The code example shows how you can map the “row data” objects prepared inside the jQuery.each loops earlier to actual data table columns. You just need to pass the correct string to .newColumn() call to map the column to the desired JavaScript property. The .withTitle method allows you to set the column header, and .withClass lets you control the CSS class you want to apply to the column.

Finally, the .dtInstance gets set to an object that wraps the original jQuery data table object, so that you can perform some granular control. This code example does not need to interact with this object, but since you have a reference to the raw data table object, you could write code like the following (the following snippet is unrelated to what we have been looking at):

The example here illustrates the very basic implementation that involves angular-datatables to use jQuery DataTables in AngularJS code base. There is a rich set of API provided by angular-datatables and the underlying DataTables that enables much more advanced functionality. I hope that the coding pattern presented here can provide a good starting point for you.

Django REST Framework

Django is one of the popular web application frameworks that is also lightweight and user friendly. Django REST Framework is an extension that makes Django even easier to use when building REST API endpoints. Essentially, once you align the following components, you get the REST API endpoints very easily.

  • Django model class: In Django, you can define your database schema using model classes. In my case, there is the NodeGroupDep class in api/
  • Serializer class: This is required by the Django REST Framework. It defines how the Django model class will be transformed when serialized for or deserialized from API payload.
  • ModelViewSet class: if you want to create a standard CRUD API against a model class, Django REST Framework makes it easy. You can define a corresponding “view set” which automatically generates a set of CRUD handlers.
  • Routes: Once the three classes above are defined, you just need to hook the auto-generated handlers up with the HTTP request router, usually defined in your application’s

Here are the sample code snippets from my application. In api/, I have the NodeGroupDep model class as follows:

In api/, I have NodeGroupDepSerializer as follows:

In a serializer class, you can choose which fields from the table will be exposed via API. The __all__ string here is a special string that indicates that we will include all fields from the model in our API payloads.

In api/, I have the corresponding view set called NodeGroupDepViewSet.

This is the class that creates the standard GET, PUT, POST and DELETE API endpoints for the given resource. Note that the filter_fields defines the query string parameters that you can use to filter the result set. In this case, you could add ?src_datacenter=dc1&dst_datacenter=dc2 to the GET API URL to only retrieve the cross-datacenter dependencies from dc1 to dc2.

In novascotia/, I define a router object that forwards HTTP requests to the right handlers defined by the view set object, as follows:

That’s all you need to do. With the ~20 lines of code above you created GET, PUT, POST and DELETE REST API endpoints that allows you to read/write to a database table corresponding to the model class. As an added bonus, Django REST Framework gives you a self-documenting UI that allows you to interact with the API using HTML forms, as shown below.

Figure 2. Django REST Framework API UI


I found using DataTables to visualize and interact with a thousands of records to be very effective, and angular-datatables makes it easy to leverage the rich feature set of DataTables inside an AngularJS app (e.g. sorting by multiple columns). On the server-side, I opted to use Django REST framework, which gives you the REST API based on the Django Object-relational Mapping (ORM) layer almost for free. Once you package all these components together in a docker container, your software will be ready to deploy in production. I hope this combination can prove to be useful for your next project, or at least some information presented here could be used when working with one of the listed technologies.


Steve Jang
Principal Engineer at Qualtrics
Steve has a wide range of software development experience, from building hardware abstraction layer for an embedded OS and fault-tolerant distributed storage components to writing Ruby on Rails and JavaScript applications. He is currently working on improving data integrity and site availability at Qualtrics.

Steve graduated from the University of Washington with Computer Science degree, and has been living in the Pacific Northwest ever since. Outside work, he keeps busy with his 2 kids and wife, and enjoys cooking and the outdoor activities available in the area.

You may also like...