Spaces:
Sleeping
Sleeping
from collections import Counter | |
import streamlit as st | |
import json | |
from itertools import islice | |
from typing import Generator | |
from plotly import express as px | |
def chunks(data: dict, size=13) -> Generator: | |
it = iter(data) | |
for i in range(0, len(data), size): | |
yield {k: data[k] for k in islice(it, size)} | |
def get_tree_map_data( | |
data: dict, | |
countings_parents: dict, | |
countings_labels: dict, | |
root: str = " ", | |
) -> tuple: | |
names: list = [""] | |
parents: list = [root] | |
values: list = ["0"] | |
for group, labels in data.items(): | |
names.append(group) | |
parents.append(root) | |
if group in countings_parents: | |
values.append(str(countings_parents[group])) | |
else: | |
values.append("0") | |
for label in labels: | |
if "-" in label: | |
label = label.split("-") | |
label = label[0] + "<br> -" + label[1] | |
names.append(label) | |
parents.append(group) | |
if label in countings_labels: | |
values.append(str(countings_labels[label])) | |
else: | |
values.append("0") | |
# if "-" in label: | |
# names.append(label.split("-")[0]) | |
# parents.append(label) | |
# names.append(label.split("-")[1]) | |
# parents.append(label) | |
return parents, names, values | |
def load_json(path: str) -> dict: | |
with open(path, "r") as fp: | |
return json.load(fp) | |
# Load Data | |
data = load_json("data.json") | |
taxonomy = load_json("taxonomy_processed_v3.json") | |
theme_counts = dict(Counter([el["THEMA"] for el in data])) | |
labels_counts = dict(Counter([el["BEZEICHNUNG"] for el in data])) | |
taxonomy = taxonomy | |
names = [""] | |
parents = ["Musterdatenkatalog"] | |
taxonomy_group_label_mapper: dict = {el["group"]: [] for el in taxonomy} | |
for el in taxonomy: | |
if el["group"] != "Sonstiges": | |
taxonomy_group_label_mapper[el["group"]].append(el["label"]) | |
else: | |
taxonomy_group_label_mapper[el["group"]].append("Sonstiges ") | |
parents, name, values = get_tree_map_data( | |
data=taxonomy_group_label_mapper, | |
countings_parents=theme_counts, | |
countings_labels=labels_counts, | |
root="Musterdatenkatalog", | |
) | |
# fig = go.Figure( | |
# go.Treemap( | |
# labels=name, | |
# parents=parents, | |
# root_color="white", | |
# values=values, | |
# # textinfo="label+value", | |
# ), | |
# ) | |
fig = px.treemap( | |
names=name, | |
parents=parents, | |
) | |
fig.update_layout( | |
margin=dict(t=50, l=25, r=25, b=25), | |
height=1000, | |
width=1000, | |
template="plotly", | |
) | |
st.title("Musterdatenkatalog") | |
st.plotly_chart(fig) | |