Created
December 9, 2019 19:43
-
-
Save GabrielSGoncalves/a65c36f0e6552601bea6f7bfe440edef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### 3 Creating the weather report\n", | |
| "\n", | |
| "On this part of the workflow we are going to use a simple HTML template to create our final report, adding information about the city chosen and the plots generated above." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 49, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# Defining start and end date for the analysis\n", | |
| "today = str(df_mean.index.min()).replace('-', '/')\n", | |
| "last_day = str(df_mean.index.max()).replace('-', '/')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 50, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# HTML template to add our data and plots\n", | |
| "report_template = f'''\n", | |
| "<!DOCTYPE html>\n", | |
| " <html>\n", | |
| " <head>\n", | |
| " <meta charset='utf-8'>\n", | |
| " <title>Weather Forecast with PyOWM</title>\n", | |
| " <link rel='stylesheet' href='report.css'>\n", | |
| " <style>\n", | |
| " h1 {{\n", | |
| " font-family: Arial;\n", | |
| " font-size: 300%;\n", | |
| " }}\n", | |
| " h2 {{\n", | |
| " font-family: Arial;\n", | |
| " font-size: 200%;\n", | |
| " }}\n", | |
| " @page {{\n", | |
| " size: 7in 9.25in;\n", | |
| " margin: 27mm 16mm 27mm 16mm;\n", | |
| " }}\n", | |
| " </style> \n", | |
| " </head>\n", | |
| " <h1 align=\"center\">Weather forecast for {city}</h1>\n", | |
| " <h2 align=\"center\">Initial date: {today}</h2>\n", | |
| " <h2 align=\"center\">Final date: {last_day}</h2>\n", | |
| " \n", | |
| " <figure>\n", | |
| " <img src=\"{temperature_plot}\" width=\"1200\" height=\"600\">\n", | |
| " </figure>\n", | |
| " <figure>\n", | |
| " <img src=\"{rain_humidity_plot}\" width=\"1200\" height=\"600\">\n", | |
| " </figure> \n", | |
| " </html>\n", | |
| "'''" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 51, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# Save HTML string to file\n", | |
| "html_report = f\"{city.split(',')[0].replace(' ','_')}_report.html\"\n", | |
| "with open(html_report, \"w\") as r:\n", | |
| " r.write(report_template)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 52, | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "libpng warning: iCCP: known incorrect sRGB profile\n", | |
| "libpng warning: iCCP: known incorrect sRGB profile\n", | |
| "Loading page (1/2)\n", | |
| "Printing pages (2/2) \n", | |
| "Done \n" | |
| ] | |
| }, | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 52, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# Use pdfkit to create the pdf report from the \n", | |
| "pdfkit.from_file(html_report, f\"{city.split(',')[0].replace(' ', '_')}_weather_report_for.pdf\") " | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "papermill", | |
| "language": "python", | |
| "name": "papermill" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.7.5" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 4 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment