Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from great_expectations.render.renderer import (
ExpectationSuitePageRenderer
)
from great_expectations.render.types import (
RenderedDocumentContent,
RenderedSectionContent,
RenderedComponentContent,
)
import pandas as pd
class MyCustomExpectationSuitePageRenderer(ExpectationSuitePageRenderer):
def render(self, expectations):
rendered_document_content = super().render(expectations)
content_blocks = [
self._render_data_dictionary_header(expectations),
self._render_data_dictionary(expectations)
]
rendered_document_content["sections"] = [
RenderedSectionContent(**{
"section_name": "Data Dictionary",
"content_blocks": content_blocks
})
] + rendered_document_content["sections"]
return rendered_document_content
def _render_data_dictionary_header(self, expectations):
return RenderedComponentContent(**{
"content_block_type": "header",
"header": "Data Dictionary",
"styling": {
"classes": ["col-12"],
"header": {
"classes": ["alert", "alert-secondary"]
}
}
})
def _render_data_dictionary(self, expectations):
data_dictionary_df = pd.DataFrame()
data_dictionary_df["Column Name"] = [i for i in self._get_table_columns(expectations)]
data_dictionary_df["Description"] = [i["description"] for i in self._get_table_columns(expectations).values()]
expectations_by_column = self._sort_expectations_by_column(expectations)
data_dictionary_df["Data Type"] = [i for i in self._get_column_data_types(expectations_by_column).values()]
return RenderedComponentContent(**{
"content_block_type": "table",
"header_row": ["Column Name", "Description", "Data Type"],
"header": "Data Dictionary",
"table": data_dictionary_df.values,
"styling": {
"classes": ["col-12", "table-responsive"],
"styles": {
"margin-top": "20px",
"margin-bottom": "20px"
},
"body": {
"classes": ["table", "table-sm"]
}
},
})
def _get_table_columns(self,expectations):
return expectations.get("meta").get("columns")
def _sort_expectations_by_column(self, expectations):
expectations_by_column = {}
expectations_dictionary = expectations.get("expectations")
column_names = list(self._get_table_columns(expectations).keys())
for column in column_names:
expectations_by_column[column] = list(filter(
lambda x: x.get("kwargs").get("column")==column,
expectations_dictionary))
return expectations_by_column
def _get_column_data_types(self,expectations_by_column):
column_data_type_expectations = {}
for k,v in expectations_by_column.items():
expectation = [i for i in v if i["expectation_type"] == "expect_column_values_to_be_in_type_list"]
if len(expectation)>0:
type_list = expectation[0].get("kwargs").get("type_list")
if len(type_list) > 0:
column_data_type_expectations[k] = type_list[0]
else:
column_data_type_expectations[k] = type_list
else:
column_data_type_expectations[k] = None
return column_data_type_expectations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.