Rahkakavee Baskaran commited on
Commit
aa68450
·
1 Parent(s): fe80153

add tree map complete

Browse files
Files changed (2) hide show
  1. app.py +94 -22
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,41 +1,113 @@
 
1
  import streamlit as st
2
- import json
3
- import plotly.express as px
 
 
 
 
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  with open("taxonomy_processed_v3.json", "r") as fp:
6
  taxonomy = json.load(fp)
7
 
8
- taxonomy = taxonomy[:200]
 
 
 
 
 
 
 
 
 
 
9
 
10
  names = [""]
11
- parents = ["root"]
12
 
13
- taxonomy_group_label_mapper = {el["group"]: [] for el in taxonomy}
14
 
15
  for el in taxonomy:
16
- taxonomy_group_label_mapper[el["group"]].append(el["label"])
17
-
18
- for group, labels in taxonomy_group_label_mapper.items():
19
- names.append(group)
20
- parents.append("root")
21
- for label in labels:
22
- names.append(label)
23
- parents.append(group)
24
- if "-" in label:
25
- names.append(label.split("-")[0])
26
- parents.append(label)
27
- names.append(label.split("-")[1])
28
- parents.append(label)
 
 
 
 
 
 
 
 
 
29
 
30
  fig = px.treemap(
31
- names=names,
32
  parents=parents,
33
  )
34
- fig.update_traces(root_color="lightgrey")
35
- fig.update_layout(margin=dict(t=100, l=50, r=50, b=50))
36
 
 
 
 
 
 
 
37
 
38
- st.title('Musterdatenkatalog')
39
 
 
40
 
41
  st.plotly_chart(fig)
 
1
+ from collections import Counter
2
  import streamlit as st
3
+ import json
4
+ from itertools import islice
5
+ from typing import Generator
6
+ import plotly.graph_objects as go
7
+ from plotly.subplots import make_subplots
8
+ import httpx
9
+ from plotly import express as px
10
 
11
+
12
+ def chunks(data: dict, size=13) -> Generator:
13
+ it = iter(data)
14
+ for i in range(0, len(data), size):
15
+ yield {k: data[k] for k in islice(it, size)}
16
+
17
+
18
+ def get_tree_map_data(
19
+ data: dict,
20
+ countings_parents: dict,
21
+ countings_labels: dict,
22
+ root: str = " ",
23
+ ) -> tuple:
24
+ names: list = [""]
25
+ parents: list = [root]
26
+ values: list = ["0"]
27
+
28
+ for group, labels in data.items():
29
+ names.append(group)
30
+ parents.append(root)
31
+ if group in countings_parents:
32
+ values.append(str(countings_parents[group]))
33
+ else:
34
+ values.append("0")
35
+ for label in labels:
36
+ if "-" in label:
37
+ label = label.split("-")
38
+ label = label[0] + "<br> -" + label[1]
39
+ names.append(label)
40
+ parents.append(group)
41
+ if label in countings_labels:
42
+ values.append(str(countings_labels[label]))
43
+ else:
44
+ values.append("0")
45
+ # if "-" in label:
46
+ # names.append(label.split("-")[0])
47
+ # parents.append(label)
48
+ # names.append(label.split("-")[1])
49
+ # parents.append(label)
50
+ return parents, names, values
51
+
52
+
53
+ # Load Data
54
  with open("taxonomy_processed_v3.json", "r") as fp:
55
  taxonomy = json.load(fp)
56
 
57
+ re = httpx.get(
58
+ "https://www.bertelsmann-stiftung.de/fileadmin/files/musterdatenkatalog/Musterdatenkatalog_4.0_042023.json"
59
+ )
60
+
61
+ data = re.json()
62
+
63
+ theme_counts = dict(Counter([el["THEMA"] for el in data]))
64
+ labels_counts = dict(Counter([el["BEZEICHNUNG"] for el in data]))
65
+
66
+
67
+ taxonomy = taxonomy
68
 
69
  names = [""]
70
+ parents = ["Musterdatenkatalog"]
71
 
72
+ taxonomy_group_label_mapper: dict = {el["group"]: [] for el in taxonomy}
73
 
74
  for el in taxonomy:
75
+ if el["group"] != "Sonstiges":
76
+ taxonomy_group_label_mapper[el["group"]].append(el["label"])
77
+ else:
78
+ taxonomy_group_label_mapper[el["group"]].append("Sonstiges ")
79
+
80
+ parents, name, values = get_tree_map_data(
81
+ data=taxonomy_group_label_mapper,
82
+ countings_parents=theme_counts,
83
+ countings_labels=labels_counts,
84
+ root="Musterdatenkatalog",
85
+ )
86
+
87
+
88
+ # fig = go.Figure(
89
+ # go.Treemap(
90
+ # labels=name,
91
+ # parents=parents,
92
+ # root_color="white",
93
+ # values=values,
94
+ # # textinfo="label+value",
95
+ # ),
96
+ # )
97
 
98
  fig = px.treemap(
99
+ names=name,
100
  parents=parents,
101
  )
 
 
102
 
103
+ fig.update_layout(
104
+ margin=dict(t=50, l=25, r=25, b=25),
105
+ height=1000,
106
+ width=1000,
107
+ template="plotly",
108
+ )
109
 
 
110
 
111
+ st.title(" Musterdatenkatalog")
112
 
113
  st.plotly_chart(fig)
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- plotly-express
 
 
1
+ plotly-express
2
+ plotly