Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Customizing a Report
00:00 In previous lessons, you saw some of the features that you can add to generate profile reports that have the right data in them. In this lesson, we will talk about some of your options to change the look and feel of these reports.
00:13 There are two categories in which we can make customizations. One is the look and feel, so the styling and formatting, colors, and so on. And the other is adding metadata about the dataset.
00:25
We’ll take a look at both of these. Over in the code, I have the necessary libraries imported and the data read into a pandas DataFrame, then create a profile report.
00:36
So profile = ProfileReport, passing in df. And after the ProfileReport object has been created, you can change some of the configuration options.
00:46
For example, profile.config.html contains the HTML-specific configurations, such as whether we’d like the report to be full-width or not. So profile.config.html.full_width, if you set that to True, the report will be full-width rather than have columns of space margins on either side of the report.
01:09
Another option you can set is the primary colors in the report. profile.config.html.style.primary_colors is a list of color codes you can use to customize the colors of the final report.
01:23
I’m going to use #FFE873, which is the official Python yellow color. Let’s save this report to file, profile.to_file(). I’ll call it report_customized.html.
01:39
I’ll run that again in the terminal, uv run report.py.
01:49 And now you can view the customized report. You can see the two changes that you’ve made. One is that the report is full-width, so there is no margin on either side of the page.
01:59 And the second one is, where previously there was a default blue color, in its place is the yellow that we specified.
02:10 Now the second kind of customization we can make to the report is by adding metadata, more information about the dataset and its various columns.
02:20
In the code, here on line 6, where you created the ProfileReport object, you can specify more options,
02:28
such as setting the title parameter to something to give the report a title. I’ll call it Flight Data Analysis.
02:37
Setting the dataset parameter, which is a dictionary of metadata about the data itself at the dataset level. So this dictionary can contain keys such as description, which is a short description of the dataset.
02:51 I’ll call it a sample, sorry, that should be a colon,
02:56
A sample dataset of flights.
03:00
In this dataset dictionary we can also set a key about copyright information. So we’ll say copyright_holder
03:09
equals, let’s say Real Python, and we can also set a copyright_year. That should also be a colon.
03:20
So we can set some metadata about the dataset itself. We can also set a parameter called variables, where we can give more metadata about individual columns.
03:31
So this is also a dictionary, and one of the keys it takes is called descriptions, which itself is also a dictionary, where each key is one of our columns and the value is a description of that column.
03:45
So I’m just going to put a few descriptions here, so the fl_date column, the date of the flight, then we’re going to explain that the origin column is the airport code of the origin,
04:02
and dest, which is short for destination, but that’s what the column is called, that’s the airport code of the destination.
04:10 By specifying this extra metadata at the dataset level and the individual column level, we can show additional information to the readers in the profile report, so they can get more context about the data.
04:23 And you’ll run the script again to view the final changes. And now that’s finished, and you can view the final report. In the final report now, there is an extra tab called Dataset, and if I click on that, it contains the metadata that we just specified, including the description and the copyright information.
04:49
And if we scroll down to the columns where we gave some extra metadata, such as fl_date, you can see the description that we wrote, as well as in the origin and destination columns.
05:06 So customizing these reports can mean changing the look and feel by changing the colors or some additional styling information, or it could mean giving more context by adding metadata at the dataset and individual column level.
05:20 And that’s it. All that’s left for us to do is to summarize what you’ve learned.
Become a Member to join the conversation.