Metabase + Django: Practical and Secure Data Visualization

Thiago Ferreira
WhatsGood Dev
Published in
4 min readNov 4, 2021

Introduction

We know that the world’s most valuable resource is data for both companies and customers. But having a ton of data is not enough if you can’t properly visualize and analyze it.

An effective data visualization tool is essential to generate value: We need the right information, for the right people, at the right time 🎯

The Dilemma

There are many tools available for data visualization, ranging from free to paid solutions and even “build your own” from scratch.

Building your own solution in a way that is flexible and functional is a challenging task. Additionally, there are maintenance and long-term costs, which should be considered when building a solution from scratch.

In this article, we will cover an approach using two open-source technologies (Metabase + Django) to create a data visualization platform that is powerful, flexible, and fully customizable.

Part 1 — Hello, Metabase

Metabase is an open-source tool for business intelligence and data visualization and it allows us to ask “questions” (or create reports) regarding our data and display the answers in formats that make sense, being charts or detailed tables.

It’s possible to connect several data sources to Metabase (such as PostgreSQL, MySQL, Big Query, MongoDB, etc) and create reports directly from a web interface. Those reports can be written in plain SQL or created directly from the interface report building tools, without writing any code.

Metabase is an incredible tool by itself and the part we will cover in this article will be the Embedded Reports. Check out the full documentation here.

Embedded reports can be added or embedded to any application outside of Metabase, making them a great option to include in your own custom applications without having to build them from scratch.

Part 2 — Preparing Django to Integrate with Metabase

Django is a very flexible python web framework and allows us to create Rest APIs and/or web pages very easily.

Since we can embed Metabase reports into our applications, we just need a way to connect the dots.

To do so, we’ll use Django to save and display Metabase report links.

Required Models

We’ll initially need these two database entities:

  • ReportEngine: Represents the tool responsible for creating the report links. At this moment, it will only be Metabase, but in the future other tools can be added painlessly.
  • EmbeddedReport: The Metabase report itself. Here we need to know the report type i.e. if the report is a single Metabase question or a dashboard.

Creating report links with the correct permissions and filters

After setting up our ReportEngine and EmbeddedReport models, we need to build a method to create the report link. The important part here is to implement all the needed filters in a way users won’t see data they are not allowed to (see line 21): 🚨

Exposing the report links via APIs

Now it’s easy: all we need to do is to create an API listing the available reports. Another approach could be creating a web page containing all the reports links inside iframes. Since, in this case, we have a separated backend and frontend, we preferred using a Rest API:

Is it secure?

Yes! But be very careful with a few things🚨:

Metabase uses JWT authentication tokens to display the embedded reports.

When we create a link for a report, we must pass all the required parameters and filters needed to ensure the user will only see data they are allowed to. This is done through blocked filters on Metabase.

In the image below, we have three filters:

  • organization_id — blocked: This means only our backend can control this filter, so the users won’t be able to change this.
  • Market Date and Market — Editable: The end-user will be able to change these filters as they want ;)

Conclusion

Here at WhatsGood, we were able to organize a data visualization dashboard in less than 2 weeks (worth noting we already had Metabase up and running).

Now we are able to display meaningful data to our customers in a fast, simple, and flexible way and this is a great deal for the opportunity costs and time to market.

Another huge advantage is that we can download the report results in the formats CSV, XLSX, or JSON, which ensures a lot of flexibility for the end-user.

Huge thanks to the Metabase team for creating this amazing solution and making it available for all! 🙌

--

--