Build a Report
In this section, we will collaboratively create a Wind Turbine Report. Together, we will:
Layout and style the report nicely.
Export the report to a static
.html
file using the.save
method.Distribute the report via email.
:::::{dropdown} Dependencies
:::::
:::::{dropdown} Code
:::::
Install the dependencies
Please ensure that Altair and Panel are installed.
::::{tab-set}
:::{tab-item} pip :sync: pip
:::
:::{tab-item} conda :sync: conda
:::
::::
Explanation
Let's break down and explain the code:
Here, we import the necessary libraries: Altair for data visualization, Pandas for data manipulation, and Panel for creating interactive web apps.
Additionally we load the "vega"
renderer, which enables rendering Altair plots, and set the sizing mode to "stretch_width"
to make sure the content fills the available horizontal space.
We define a multiline string TEXT
containing some markdown text describing wind turbines. Then, we define a function get_data()
decorated with @pn.cache
to cache the data retrieval operation. This function reads a CSV file containing turbine data from a URL and returns a Pandas DataFrame, which we store in the variable df
.
We calculate various summary statistics from the dataset, such as the total count of turbines, the total capacity, the average capacity (converted to MW), the average rotor diameter, and a list of top manufacturers by total capacity. Additionally, we create an example DataFrame (example_df
) containing a random sample of turbine data to display in a table later.
We filter the dataset to include only the turbines from the top manufacturers. Then, we create an Altair scatter plot (fig
) to visualize the capacity of turbines by manufacturer. We encode the manufacturer on the y-axis, the capacity on the x-axis, and use jitter to prevent overplotting. The tooltip shows the manufacturer and capacity when hovering over data points.
Here, we define some styling constants and create a header for the report, which consists of a Markdown title styled with the brand color and a teal background.
We create various components for the report, including a set of indicators (total capacity, average capacity, etc.), a Vega plot (plot
) displaying the capacity by manufacturer, and a DataFrame (table
) showing example turbine data.
We assemble the main content of the report (main
) as a Column layout, including the summary section, the indicators, the Markdown text, the plot, and the table.
We create a container (main_container
) to hold the main content with a maximum width and some margin styling. Then, we compose the entire report (report
) by combining the header and the main container.
Finally, we save the report as an HTML file named "report.html". This HTML file will contain the rendered content of the report, including the header, indicators, text, plot, and table.
:::{note} With .save
you can produce static reports that display content and have very limited interactivity. It is possible to add more interactivity. Check out the Embed State or Convert Panel Applications guides for more detail. :::
Run the Code
Run the code with python app.py
.
Please verify that the file report.html
has been created.
Please open the report.html
file in your browser. It should look like:
Distribute the report
Attach the report.html
file generated in the previous section to an email and send it to yourself. When you receive the email, then open the report.html
attachment.
🥳 Congrats! Together, we have successfully created and distributed an interactive report based on DataFrames and one of our favorite plotting libraries. This is an amazing achievement!
Recap
In this section we have built a Wind Turbine Report with a nice user experience, exported it to a .html
file using the .save
method and distributed it via email.