Skip to content

Commit

Permalink
compose and style data dictionary HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
damonmcc committed Dec 2, 2024
1 parent 65b2420 commit 9b365ce
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 113 deletions.
23 changes: 12 additions & 11 deletions dcpy/lifecycle/package/generate_metadata_assets.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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",
Expand All @@ -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
9 changes: 0 additions & 9 deletions dcpy/lifecycle/package/resources/DCP_Logo.svg

This file was deleted.

22 changes: 0 additions & 22 deletions dcpy/lifecycle/package/resources/data_dictionary.css

This file was deleted.

69 changes: 0 additions & 69 deletions dcpy/lifecycle/package/resources/data_dictionary_template.jinja

This file was deleted.

Binary file added dcpy/lifecycle/package/resources/dcp_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div class="attributes">

<h2>Attributes</h2>

<div class="field">

<p class="field-content">
<span class="field-name">Description:</span> {{ metadata.attributes.description }}
</p>

<p class="field-content">
<span class="field-name">Each row is a:</span> {{ metadata.attributes.each_row_is_a }}
</p>

<p class="field-content">
<span class="field-name">Tags:</span> {{ metadata.attributes.tags|join(', ') }}
</p>

</div>

</div>
28 changes: 28 additions & 0 deletions dcpy/lifecycle/package/resources/document_templates/columns.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div class="attributes">

<h2>Columns</h2>

{% for column in metadata.columns %}
<div class="column">

<h3>{{ column.name }}</h3>

<p class="column-field">
<span class="field-name">Description:</span> {{ column.description }}
</p>
<p class="column-field">
<span class="field-name">Type:</span>
{% if column.custom.readme_data_type %}
{{ column.custom.readme_data_type }}
{% else %}
{{ column.data_type }}
{% endif %}
</p>
<p class="column-field">
<span class="field-name">Source:</span> {{ column.data_source }}
</p>

</div>
{% endfor %}

</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>{{ metadata.attributes.display_name }} Data Dictionary</title>
</head>

<body>
<h1>{{ metadata.attributes.display_name }} - Data Dictionary</h1>

{% include 'attributes.jinja' %}

{% include 'columns.jinja' %}
</body>

</html>
12 changes: 12 additions & 0 deletions dcpy/lifecycle/package/resources/document_templates/document.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
html {
color: #393939;
font-family: Fira Sans;
font-size: 10pt;
}

.field-name {
font-weight: bold;
}
.column-field {
text-indent: 1cm;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion products/template/build_scripts/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,5 @@ library = "dcpy.library.cli:run"
[tool.setuptools.package-data]
dcpy = [
"library/templates/*.yml",
"lifecycle/package/resources/*",
"lifecycle/package/resources/**",
]

0 comments on commit 9b365ce

Please sign in to comment.