Update pages/DATA CATALOG.py
Browse files- pages/DATA CATALOG.py +79 -78
pages/DATA CATALOG.py
CHANGED
@@ -59,89 +59,90 @@ st.markdown("""
|
|
59 |
}
|
60 |
</style>
|
61 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
######
|
63 |
def main():
|
64 |
# st.title('PAGE TITLE') # Change this for each page
|
65 |
sidebar()
|
66 |
########
|
67 |
-
def clear_cache():
|
68 |
-
if 'rdf' in st.session_state:
|
69 |
-
st.session_state.pop('rdf')
|
70 |
-
|
71 |
-
def create_er_diagram(df):
|
72 |
-
G = nx.DiGraph() # Directed graph
|
73 |
-
|
74 |
-
# Dictionary to hold table columns
|
75 |
-
table_columns = {}
|
76 |
-
|
77 |
-
# Add nodes and edges to the graph
|
78 |
-
for _, row in df.iterrows():
|
79 |
-
parent_table = row['PARENT TABLE']
|
80 |
-
child_table = row['CHILD TABLE']
|
81 |
-
parent_pk = row['PARENT TABLE RELATIONSHIP COLUMN']
|
82 |
-
child_fk = row['CHILD TABLE RELATIONSHIP COLUMN']
|
83 |
-
cardinality = row.get('CARDINALITY', '1:N')
|
84 |
-
|
85 |
-
# Add columns to tables
|
86 |
-
if parent_table not in table_columns:
|
87 |
-
table_columns[parent_table] = []
|
88 |
-
table_columns[parent_table].append(parent_pk)
|
89 |
-
|
90 |
-
if child_table not in table_columns:
|
91 |
-
table_columns[child_table] = []
|
92 |
-
table_columns[child_table].append(child_fk)
|
93 |
-
|
94 |
-
# Add nodes and edges
|
95 |
-
G.add_node(parent_table)
|
96 |
-
G.add_node(child_table)
|
97 |
-
G.add_edge(parent_table, child_table, label=f'{parent_pk} -> {child_fk}\n{cardinality}')
|
98 |
-
|
99 |
-
return G, table_columns
|
100 |
-
|
101 |
-
def draw_er_diagram(G, table_columns):
|
102 |
-
pos = nx.spring_layout(G, k=1.5, iterations=50) # Use a layout that spreads out nodes
|
103 |
-
|
104 |
-
plt.figure(figsize=(8, 8))
|
105 |
-
nx.draw(G, pos, with_labels=False, node_size=2500, node_color='lightblue', edge_color='gray', font_size=8, font_weight='bold', arrows=True)
|
106 |
-
|
107 |
-
# Draw node labels (table names in bold)
|
108 |
-
for node, (x, y) in pos.items():
|
109 |
-
plt.text(x, y + 0.13, node, fontsize=7, fontweight='bold', ha='center', va='center')
|
110 |
-
|
111 |
-
# Draw column names
|
112 |
-
for node, columns in table_columns.items():
|
113 |
-
x, y = pos[node]
|
114 |
-
column_text = '\n'.join(columns)
|
115 |
-
plt.text(x, y, column_text, fontsize=6, ha='center', va='center')
|
116 |
-
|
117 |
-
# Draw edge labels
|
118 |
-
edge_labels = nx.get_edge_attributes(G, 'label')
|
119 |
-
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=6)
|
120 |
-
st.subheader("Schematic Representation")
|
121 |
-
with st.container(border=True, height= 350):
|
122 |
-
st.pyplot(plt)
|
123 |
-
img_bytes = BytesIO()
|
124 |
-
plt.savefig(img_bytes, format='png')
|
125 |
-
img_bytes.seek(0)
|
126 |
-
return img_bytes
|
127 |
-
|
128 |
-
def cardinality(parent_df, child_df, parent_column, child_column):
|
129 |
-
# Check uniqueness of parent primary key
|
130 |
-
is_parent_unique = parent_df[parent_column].is_unique
|
131 |
-
|
132 |
-
# Check uniqueness of child foreign key
|
133 |
-
is_child_unique = child_df[child_column].is_unique
|
134 |
-
|
135 |
-
# Determine cardinality
|
136 |
-
if is_parent_unique and is_child_unique:
|
137 |
-
return '1:1'
|
138 |
-
elif is_parent_unique and not is_child_unique:
|
139 |
-
return '1:N'
|
140 |
-
elif not is_parent_unique and is_child_unique:
|
141 |
-
return 'N:1'
|
142 |
-
else:
|
143 |
-
return 'N:N'
|
144 |
-
|
145 |
#st.title('AUTOMATED DATA CATALOGUE')
|
146 |
st.subheader('SELECT SOURCE')
|
147 |
selectcol11, selectcol12 = st.columns(2)
|
|
|
59 |
}
|
60 |
</style>
|
61 |
""", unsafe_allow_html=True)
|
62 |
+
|
63 |
+
def clear_cache():
|
64 |
+
if 'rdf' in st.session_state:
|
65 |
+
st.session_state.pop('rdf')
|
66 |
+
|
67 |
+
def create_er_diagram(df):
|
68 |
+
G = nx.DiGraph() # Directed graph
|
69 |
+
|
70 |
+
# Dictionary to hold table columns
|
71 |
+
table_columns = {}
|
72 |
+
|
73 |
+
# Add nodes and edges to the graph
|
74 |
+
for _, row in df.iterrows():
|
75 |
+
parent_table = row['PARENT TABLE']
|
76 |
+
child_table = row['CHILD TABLE']
|
77 |
+
parent_pk = row['PARENT TABLE RELATIONSHIP COLUMN']
|
78 |
+
child_fk = row['CHILD TABLE RELATIONSHIP COLUMN']
|
79 |
+
cardinality = row.get('CARDINALITY', '1:N')
|
80 |
+
|
81 |
+
# Add columns to tables
|
82 |
+
if parent_table not in table_columns:
|
83 |
+
table_columns[parent_table] = []
|
84 |
+
table_columns[parent_table].append(parent_pk)
|
85 |
+
|
86 |
+
if child_table not in table_columns:
|
87 |
+
table_columns[child_table] = []
|
88 |
+
table_columns[child_table].append(child_fk)
|
89 |
+
|
90 |
+
# Add nodes and edges
|
91 |
+
G.add_node(parent_table)
|
92 |
+
G.add_node(child_table)
|
93 |
+
G.add_edge(parent_table, child_table, label=f'{parent_pk} -> {child_fk}\n{cardinality}')
|
94 |
+
|
95 |
+
return G, table_columns
|
96 |
+
|
97 |
+
def draw_er_diagram(G, table_columns):
|
98 |
+
pos = nx.spring_layout(G, k=1.5, iterations=50) # Use a layout that spreads out nodes
|
99 |
+
|
100 |
+
plt.figure(figsize=(8, 8))
|
101 |
+
nx.draw(G, pos, with_labels=False, node_size=2500, node_color='lightblue', edge_color='gray', font_size=8, font_weight='bold', arrows=True)
|
102 |
+
|
103 |
+
# Draw node labels (table names in bold)
|
104 |
+
for node, (x, y) in pos.items():
|
105 |
+
plt.text(x, y + 0.13, node, fontsize=7, fontweight='bold', ha='center', va='center')
|
106 |
+
|
107 |
+
# Draw column names
|
108 |
+
for node, columns in table_columns.items():
|
109 |
+
x, y = pos[node]
|
110 |
+
column_text = '\n'.join(columns)
|
111 |
+
plt.text(x, y, column_text, fontsize=6, ha='center', va='center')
|
112 |
+
|
113 |
+
# Draw edge labels
|
114 |
+
edge_labels = nx.get_edge_attributes(G, 'label')
|
115 |
+
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=6)
|
116 |
+
st.subheader("Schematic Representation")
|
117 |
+
with st.container(border=True, height= 350):
|
118 |
+
st.pyplot(plt)
|
119 |
+
img_bytes = BytesIO()
|
120 |
+
plt.savefig(img_bytes, format='png')
|
121 |
+
img_bytes.seek(0)
|
122 |
+
return img_bytes
|
123 |
+
|
124 |
+
def cardinality(parent_df, child_df, parent_column, child_column):
|
125 |
+
# Check uniqueness of parent primary key
|
126 |
+
is_parent_unique = parent_df[parent_column].is_unique
|
127 |
+
|
128 |
+
# Check uniqueness of child foreign key
|
129 |
+
is_child_unique = child_df[child_column].is_unique
|
130 |
+
|
131 |
+
# Determine cardinality
|
132 |
+
if is_parent_unique and is_child_unique:
|
133 |
+
return '1:1'
|
134 |
+
elif is_parent_unique and not is_child_unique:
|
135 |
+
return '1:N'
|
136 |
+
elif not is_parent_unique and is_child_unique:
|
137 |
+
return 'N:1'
|
138 |
+
else:
|
139 |
+
return 'N:N'
|
140 |
+
|
141 |
######
|
142 |
def main():
|
143 |
# st.title('PAGE TITLE') # Change this for each page
|
144 |
sidebar()
|
145 |
########
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
#st.title('AUTOMATED DATA CATALOGUE')
|
147 |
st.subheader('SELECT SOURCE')
|
148 |
selectcol11, selectcol12 = st.columns(2)
|