Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from graphviz import Digraph | |
| import json | |
| def create_graph_from_json_text(json_text, graph=None, parent_node=None): | |
| # Convert JSON text to a Python dictionary | |
| json_data = json.loads(json_text) | |
| # Create a new Digraph if not provided | |
| if graph is None: | |
| graph = Digraph() | |
| if isinstance(json_data, dict): | |
| for key, value in json_data.items(): | |
| if isinstance(value, dict) or isinstance(value, list): | |
| node_id = f"{parent_node}.{key}" if parent_node else key | |
| graph.node(node_id, label=key) | |
| if parent_node is not None: | |
| graph.edge(parent_node, node_id) | |
| create_graph_from_json_text(json.dumps(value), graph, node_id) | |
| else: | |
| node_id = f"{parent_node}.{key}" if parent_node else key | |
| graph.node(node_id, label=f"{key}: {value}") | |
| if parent_node is not None: | |
| graph.edge(parent_node, node_id) | |
| elif isinstance(json_data, list): | |
| for index, item in enumerate(json_data): | |
| node_id = f"{parent_node}[{index}]" if parent_node else f"[{index}]" | |
| graph.node(node_id, label=f"[{index}]") | |
| if parent_node is not None: | |
| graph.edge(parent_node, node_id) | |
| create_graph_from_json_text(json.dumps(item), graph, node_id) | |
| return graph | |
| def main(): | |
| st.title("Graph Visualization with Graphviz and Streamlit") | |
| # JSON text in Streamlit | |
| json_text = """ | |
| { | |
| "id": "urn:example:aas:asset1", | |
| "administration": { | |
| "version": "2.1", | |
| "revision": "A", | |
| "validUntil": "2050-12-31T23:59:59Z" | |
| }, | |
| "canonicalModel": { | |
| "id": "urn:example:model:asset1", | |
| "version": "1.0" | |
| }, | |
| "submodels": [ | |
| { | |
| "id": "urn:example:submodel:1", | |
| "elements": [ | |
| { | |
| "id": "property1", | |
| "propertyType": "string", | |
| "value": "Value 1" | |
| }, | |
| { | |
| "id": "property2", | |
| "propertyType": "integer", | |
| "value": 25 | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| """ | |
| # Create the diagram from the JSON text | |
| graph = create_graph_from_json_text(json_text) | |
| # Display the diagram in Streamlit | |
| st.graphviz_chart(graph.source) | |
| if __name__ == "__main__": | |
| main() | |