farukkr commited on
Commit
d0a2dc2
1 Parent(s): 1354e42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -50
app.py CHANGED
@@ -2,71 +2,78 @@ import streamlit as st
2
  from graphviz import Digraph
3
  import json
4
 
5
- def create_graph_from_json_text(json_text):
6
- # Konvertieren des JSON-Texts in ein Python Dictionary
7
  json_data = json.loads(json_text)
8
 
9
- graph = Digraph()
10
- def add_nodes_and_edges(data, parent_key=None):
11
- if isinstance(data, dict):
12
- for key, value in data.items():
13
- if parent_key is not None:
14
- graph.edge(str(parent_key), str(key))
15
- add_nodes_and_edges(value, key)
16
- elif isinstance(data, list):
17
- for i, item in enumerate(data):
18
- if parent_key is not None:
19
- graph.edge(str(parent_key), f"{parent_key}_{i}")
20
- add_nodes_and_edges(item, f"{parent_key}_{i}")
21
- else:
22
- if parent_key is not None:
23
- graph.edge(str(parent_key), str(data))
24
 
25
- add_nodes_and_edges(json_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  return graph
28
 
29
  def main():
30
  st.title("Graph Visualization with Graphviz and Streamlit")
31
 
32
- # JSON-Text in Streamlit
33
  json_text = """
34
  {
35
- "id": "urn:example:aas:asset1",
36
- "administration": {
37
- "version": "2.1",
38
- "revision": "A",
39
- "validUntil": "2050-12-31T23:59:59Z"
40
- },
41
- "canonicalModel": {
42
- "id": "urn:example:model:asset1",
43
- "version": "1.0"
44
- },
45
- "submodels": [
46
- {
47
- "id": "urn:example:submodel:1",
48
- "elements": [
49
- {
50
- "id": "property1",
51
- "propertyType": "string",
52
- "value": "Value 1"
53
- },
54
- {
55
- "id": "property2",
56
- "propertyType": "integer",
57
- "value": 25
58
- }
59
- ]
60
- }
61
- ]
62
- }
63
  """
64
 
65
- # Erstellen des Diagramms aus dem JSON-Text
66
  graph = create_graph_from_json_text(json_text)
67
 
68
- # Anzeige des Diagramms in Streamlit
69
  st.graphviz_chart(graph.source)
70
 
71
  if __name__ == "__main__":
72
- main()
 
2
  from graphviz import Digraph
3
  import json
4
 
5
+ def create_graph_from_json_text(json_text, graph=None, parent_node=None):
6
+ # Convert JSON text to a Python dictionary
7
  json_data = json.loads(json_text)
8
 
9
+ # Create a new Digraph if not provided
10
+ if graph is None:
11
+ graph = Digraph()
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ if isinstance(json_data, dict):
14
+ for key, value in json_data.items():
15
+ if isinstance(value, dict) or isinstance(value, list):
16
+ node_id = f"{parent_node}.{key}" if parent_node else key
17
+ graph.node(node_id, label=key)
18
+ if parent_node is not None:
19
+ graph.edge(parent_node, node_id)
20
+ create_graph_from_json_text(json.dumps(value), graph, node_id)
21
+ else:
22
+ node_id = f"{parent_node}.{key}" if parent_node else key
23
+ graph.node(node_id, label=f"{key}: {value}")
24
+ if parent_node is not None:
25
+ graph.edge(parent_node, node_id)
26
+ elif isinstance(json_data, list):
27
+ for index, item in enumerate(json_data):
28
+ node_id = f"{parent_node}[{index}]" if parent_node else f"[{index}]"
29
+ graph.node(node_id, label=f"[{index}]")
30
+ if parent_node is not None:
31
+ graph.edge(parent_node, node_id)
32
+ create_graph_from_json_text(json.dumps(item), graph, node_id)
33
 
34
  return graph
35
 
36
  def main():
37
  st.title("Graph Visualization with Graphviz and Streamlit")
38
 
39
+ # JSON text in Streamlit
40
  json_text = """
41
  {
42
+ "id": "urn:example:aas:asset1",
43
+ "administration": {
44
+ "version": "2.1",
45
+ "revision": "A",
46
+ "validUntil": "2050-12-31T23:59:59Z"
47
+ },
48
+ "canonicalModel": {
49
+ "id": "urn:example:model:asset1",
50
+ "version": "1.0"
51
+ },
52
+ "submodels": [
53
+ {
54
+ "id": "urn:example:submodel:1",
55
+ "elements": [
56
+ {
57
+ "id": "property1",
58
+ "propertyType": "string",
59
+ "value": "Value 1"
60
+ },
61
+ {
62
+ "id": "property2",
63
+ "propertyType": "integer",
64
+ "value": 25
65
+ }
66
+ ]
67
+ }
68
+ ]
69
+ }
70
  """
71
 
72
+ # Create the diagram from the JSON text
73
  graph = create_graph_from_json_text(json_text)
74
 
75
+ # Display the diagram in Streamlit
76
  st.graphviz_chart(graph.source)
77
 
78
  if __name__ == "__main__":
79
+ main()