diff --git a/dcpy/lifecycle/package/generate_metadata_assets.py b/dcpy/lifecycle/package/generate_metadata_assets.py index b76dd5966..0093b6dd0 100644 --- a/dcpy/lifecycle/package/generate_metadata_assets.py +++ b/dcpy/lifecycle/package/generate_metadata_assets.py @@ -1,5 +1,5 @@ from pathlib import Path -from jinja2 import Template, Environment, FileSystemLoader +from jinja2 import Environment, FileSystemLoader import css_inline from bs4 import BeautifulSoup import subprocess @@ -9,9 +9,12 @@ from . import RESOURCES_PATH DEFAULT_DATA_DICTIONARY_TEMPLATE_PATH = ( - RESOURCES_PATH / "data_dictionary_template.jinja" + RESOURCES_PATH / "document_templates" / "data_dictionary.jinja" ) -DEFAULT_DATA_DICTIONARY_STYLESHEET_PATH = RESOURCES_PATH / "data_dictionary.css" +DEFAULT_DATA_DICTIONARY_STYLESHEET_PATH = ( + RESOURCES_PATH / "document_templates" / "document.css" +) +DEFAULT_PDF_STYLESHEET_PATH = RESOURCES_PATH / "document_templates" / "paged_media.css" def _format_html(html: str) -> str: @@ -48,10 +51,9 @@ def _style_html_document(html: str, stylesheet_path: Path) -> str: def generate_pdf_from_html( html_path: Path, output_path: Path, - stylesheet_path: Path, + stylesheet_path: Path = DEFAULT_PDF_STYLESHEET_PATH, ) -> Path: logger.info(f"Saving DCP PDF to {output_path}") - # TODO style HTML before converting to PDF subprocess.run( [ "weasyprint", @@ -69,17 +71,16 @@ def generate_html_from_yaml( metadata_path: Path, output_path: Path, html_template_path: Path, + stylesheet_path: Path, ) -> Path: - metadata = Metadata.from_path(metadata_path, template_vars={"var1": "value1"}) + metadata = Metadata.from_path(metadata_path) - # TODO use _render_html_template and _compose_html_document - with open(html_template_path, "r") as f: - template_text = f.read() - rendered_template = Template(template_text).render({"metadata": metadata}) + html = _render_html_template(html_template_path, {"metadata": metadata}) + styled_html = _style_html_document(html, stylesheet_path) output_path.parent.mkdir(parents=True, exist_ok=True) logger.info(f"Saving DCP HTML to {output_path}") with open(output_path, "w") as f: - f.write(rendered_template) + f.write(styled_html) return output_path diff --git a/dcpy/lifecycle/package/resources/DCP_Logo.svg b/dcpy/lifecycle/package/resources/DCP_Logo.svg deleted file mode 100644 index bab3856f5..000000000 --- a/dcpy/lifecycle/package/resources/DCP_Logo.svg +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/dcpy/lifecycle/package/resources/data_dictionary.css b/dcpy/lifecycle/package/resources/data_dictionary.css deleted file mode 100644 index e3500423e..000000000 --- a/dcpy/lifecycle/package/resources/data_dictionary.css +++ /dev/null @@ -1,22 +0,0 @@ -html { - color: #393939; - font-family: Fira Sans; - font-size: 11pt; - font-weight: 300; - line-height: 1.5; -} -h1 { - string-set: top-right-text content(); -} -@page { - @top-left { - content: url("DCP_Logo.svg"); - } - @top-right { - content: string(top-right-text); - } - @bottom-right-corner { - content: counter(page); - text-align: center; - } -} diff --git a/dcpy/lifecycle/package/resources/data_dictionary_template.jinja b/dcpy/lifecycle/package/resources/data_dictionary_template.jinja deleted file mode 100644 index 29b752949..000000000 --- a/dcpy/lifecycle/package/resources/data_dictionary_template.jinja +++ /dev/null @@ -1,69 +0,0 @@ - - - - - {{ metadata.attributes.display_name }} - - - -

Data Dictionary

-

Description

-

{{ metadata.attributes.description }}

- -

Tags

- - -

Each row is a

-

{{ metadata.attributes.each_row_is_a }}

- -

Columns

- - - - - diff --git a/dcpy/lifecycle/package/resources/dcp_logo.png b/dcpy/lifecycle/package/resources/dcp_logo.png new file mode 100644 index 000000000..4c2b3334f Binary files /dev/null and b/dcpy/lifecycle/package/resources/dcp_logo.png differ diff --git a/dcpy/lifecycle/package/resources/document_templates/attributes.jinja b/dcpy/lifecycle/package/resources/document_templates/attributes.jinja new file mode 100644 index 000000000..5cac709dd --- /dev/null +++ b/dcpy/lifecycle/package/resources/document_templates/attributes.jinja @@ -0,0 +1,21 @@ +
+ +

Attributes

+ +
+ +

+ Description: {{ metadata.attributes.description }} +

+ +

+ Each row is a: {{ metadata.attributes.each_row_is_a }} +

+ +

+ Tags: {{ metadata.attributes.tags|join(', ') }} +

+ +
+ +
\ No newline at end of file diff --git a/dcpy/lifecycle/package/resources/document_templates/columns.jinja b/dcpy/lifecycle/package/resources/document_templates/columns.jinja new file mode 100644 index 000000000..e2854c410 --- /dev/null +++ b/dcpy/lifecycle/package/resources/document_templates/columns.jinja @@ -0,0 +1,28 @@ +
+ +

Columns

+ + {% for column in metadata.columns %} +
+ +

{{ column.name }}

+ +

+ Description: {{ column.description }} +

+

+ Type: + {% if column.custom.readme_data_type %} + {{ column.custom.readme_data_type }} + {% else %} + {{ column.data_type }} + {% endif %} +

+

+ Source: {{ column.data_source }} +

+ +
+ {% endfor %} + +
\ No newline at end of file diff --git a/dcpy/lifecycle/package/resources/document_templates/data_dictionary.jinja b/dcpy/lifecycle/package/resources/document_templates/data_dictionary.jinja new file mode 100644 index 000000000..2f88225ad --- /dev/null +++ b/dcpy/lifecycle/package/resources/document_templates/data_dictionary.jinja @@ -0,0 +1,17 @@ + + + + + + {{ metadata.attributes.display_name }} Data Dictionary + + + +

{{ metadata.attributes.display_name }} - Data Dictionary

+ + {% include 'attributes.jinja' %} + + {% include 'columns.jinja' %} + + + \ No newline at end of file diff --git a/dcpy/lifecycle/package/resources/document_templates/document.css b/dcpy/lifecycle/package/resources/document_templates/document.css new file mode 100644 index 000000000..ac42af58f --- /dev/null +++ b/dcpy/lifecycle/package/resources/document_templates/document.css @@ -0,0 +1,12 @@ +html { + color: #393939; + font-family: Fira Sans; + font-size: 10pt; +} + +.field-name { + font-weight: bold; +} +.column-field { + text-indent: 1cm; +} diff --git a/dcpy/lifecycle/package/resources/document_templates/paged_media.css b/dcpy/lifecycle/package/resources/document_templates/paged_media.css new file mode 100644 index 000000000..31c69d363 --- /dev/null +++ b/dcpy/lifecycle/package/resources/document_templates/paged_media.css @@ -0,0 +1,12 @@ +@page { + @top-left { + content: url("../dcp_logo.png"); + } + @top-right { + content: string(top-right-text); + } + @bottom-right-corner { + content: counter(page); + text-align: center; + } +} diff --git a/dcpy/test/lifecycle/package/test_generate_data_dictionary.py b/dcpy/test/lifecycle/package/test_generate_data_dictionary.py index c2962878b..ea9fd66fa 100644 --- a/dcpy/test/lifecycle/package/test_generate_data_dictionary.py +++ b/dcpy/test/lifecycle/package/test_generate_data_dictionary.py @@ -77,6 +77,7 @@ def test_generate_pdf_from_yaml(self): metadata_path=self.yaml_path, output_path=self.html_path, html_template_path=generate_metadata_assets.DEFAULT_DATA_DICTIONARY_TEMPLATE_PATH, + stylesheet_path=generate_metadata_assets.DEFAULT_DATA_DICTIONARY_STYLESHEET_PATH, ) pdf_path = generate_metadata_assets.generate_pdf_from_html( html_path=html_path, diff --git a/products/template/build_scripts/export.py b/products/template/build_scripts/export.py index 93fcc354f..d41a88319 100644 --- a/products/template/build_scripts/export.py +++ b/products/template/build_scripts/export.py @@ -31,11 +31,11 @@ def generate_metadata(): dataset_metadata_yml, PRODUCT_PATH / "data_dictionary.html", generate_metadata_assets.DEFAULT_DATA_DICTIONARY_TEMPLATE_PATH, + generate_metadata_assets.DEFAULT_DATA_DICTIONARY_STYLESHEET_PATH, ) generate_metadata_assets.generate_pdf_from_html( html_path, PRODUCT_PATH / "data_dictionary.pdf", - generate_metadata_assets.DEFAULT_DATA_DICTIONARY_STYLESHEET_PATH, ) oti_xlsx.write_oti_xlsx( dataset=md.Metadata.from_path(dataset_metadata_yml).dataset, diff --git a/pyproject.toml b/pyproject.toml index 2d12a8039..ae65e3780 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -150,5 +150,5 @@ library = "dcpy.library.cli:run" [tool.setuptools.package-data] dcpy = [ "library/templates/*.yml", - "lifecycle/package/resources/*", + "lifecycle/package/resources/**", ]