Skip to content

Commit

Permalink
Export data contract as HTML (datacontract#140)
Browse files Browse the repository at this point in the history
resolves datacontract#15

Co-authored-by: jochen <[email protected]>
  • Loading branch information
jochenchrist and jochenchrist authored Apr 16, 2024
1 parent 2216e3f commit 86f9c94
Show file tree
Hide file tree
Showing 8 changed files with 1,118 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added export format **html** (#15)
- Added descriptions as comments to `datacontract export --format sql` for Databricks dialects
- Added import of arrays in Avro import

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ $ datacontract changelog datacontract-v1.yaml datacontract-v2.yaml
# fail pipeline on breaking changes. Uses changelog internally and showing only error and warning.
$ datacontract breaking datacontract-v1.yaml datacontract-v2.yaml

# export model as jsonschema (other formats: avro, dbt, dbt-sources, dbt-staging-sql, jsonschema, odcs, rdf, sql (coming soon), sodacl, terraform)
$ datacontract export --format jsonschema datacontract.yaml
# export data contract as html (other formats: avro, dbt, dbt-sources, dbt-staging-sql, jsonschema, odcs, rdf, sql, sodacl, terraform, ...)
$ datacontract export --format html datacontract.yaml > datacontract.html

# import sql
$ datacontract import --format sql --source my_ddl.sql
Expand Down Expand Up @@ -455,6 +455,7 @@ Available export options:

| Type | Description | Status |
|----------------------|---------------------------------------------------------|--------|
| `html` | Export to HTML | ✅ |
| `jsonschema` | Export to JSON Schema | ✅ |
| `odcs` | Export to Open Data Contract Standard (ODCS) | ✅ |
| `sodacl` | Export to SodaCL quality checks in YAML format | ✅ |
Expand All @@ -470,7 +471,6 @@ Available export options:
| `great-expectations` | Export to Great Expectations Suites in JSON Format | ✅ |
| `bigquery` | Export to BigQuery Schemas | TBD |
| `pydantic` | Export to pydantic models | TBD |
| `html` | Export to HTML page | TBD |
| Missing something? | Please create an issue on GitHub | TBD |

#### Great Expectations
Expand Down
1 change: 1 addition & 0 deletions datacontract/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class ExportFormat(str, Enum):
avro_idl = "avro-idl"
sql = "sql"
sql_query = "sql-query"
html = "html"


@app.command()
Expand Down
3 changes: 3 additions & 0 deletions datacontract/data_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
to_dbt_sources_yaml, to_dbt_staging_sql
from datacontract.export.great_expectations_converter import \
to_great_expectations
from datacontract.export.html_export import to_html
from datacontract.export.jsonschema_converter import to_jsonschema_json
from datacontract.export.odcs_converter import to_odcs_yaml
from datacontract.export.protobuf_converter import to_protobuf
Expand Down Expand Up @@ -418,6 +419,8 @@ def export(self, export_format, model: str = "all", rdf_base: str = None, sql_se
return to_great_expectations(data_contract, model_name)
if export_format == "pydantic-model":
return to_pydantic_model_str(data_contract)
if export_format == "html":
return to_html(data_contract)
else:
print(f"Export format {export_format} not supported.")
return ""
Expand Down
21 changes: 21 additions & 0 deletions datacontract/export/html_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from jinja2 import Environment, PackageLoader, select_autoescape

from datacontract.model.data_contract_specification import \
DataContractSpecification


def to_html(data_contract_spec: DataContractSpecification) -> str:
# Load templates from templates folder
file_loader = PackageLoader("datacontract", "templates")
env = Environment(loader=file_loader, autoescape=select_autoescape(
enabled_extensions=('html', 'xml'),
default_for_string=True,
))

# Load the required template
template = env.get_template('datacontract.html')

# Render the template with necessary data
html_string = template.render(datacontract=data_contract_spec)

return html_string
Loading

0 comments on commit 86f9c94

Please sign in to comment.