forked from streamlit/streamlit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst_graphviz_chart.py
100 lines (81 loc) · 2.66 KB
/
st_graphviz_chart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import graphviz
import streamlit as st
# basic graph
hello = graphviz.Digraph("Hello World")
hello.edge("Hello", "World")
# styled graph
styled = graphviz.Graph("G", filename="g_c_n.gv")
styled.attr(bgcolor="purple:pink", label="agraph", fontcolor="white")
with styled.subgraph(name="cluster1") as c:
c.attr(
fillcolor="blue:cyan",
label="acluster",
fontcolor="white",
style="filled",
gradientangle="270",
)
c.attr(
"node", shape="box", fillcolor="red:yellow", style="filled", gradientangle="90"
)
c.node("anode")
# complex graph
engine = st.sidebar.radio(
"Select engine",
["dot", "neato", "twopi", "circo", "fdp", "osage", "patchwork"],
)
st.sidebar.write(engine)
finite = graphviz.Digraph("finite_state_machine", filename="fsm.gv", engine=engine)
finite.attr(rankdir="LR", size="8,5")
finite.attr("node", shape="doublecircle")
finite.node("LR_0")
finite.node("LR_3")
finite.node("LR_4")
finite.node("LR_8")
finite.attr("node", shape="circle")
finite.edge("LR_0", "LR_2", label="SS(B)")
finite.edge("LR_0", "LR_1", label="SS(S)")
finite.edge("LR_1", "LR_3", label="S($end)")
finite.edge("LR_2", "LR_6", label="SS(b)")
finite.edge("LR_2", "LR_5", label="SS(a)")
finite.edge("LR_2", "LR_4", label="S(A)")
finite.edge("LR_5", "LR_7", label="S(b)")
finite.edge("LR_5", "LR_5", label="S(a)")
finite.edge("LR_6", "LR_6", label="S(b)")
finite.edge("LR_6", "LR_5", label="S(a)")
finite.edge("LR_7", "LR_8", label="S(b)")
finite.edge("LR_7", "LR_5", label="S(a)")
finite.edge("LR_8", "LR_6", label="S(b)")
finite.edge("LR_8", "LR_5", label="S(a)")
# draw graphs
st.graphviz_chart(hello)
st.graphviz_chart(styled)
st.graphviz_chart(finite)
# draw graphs in columns
left_graph = graphviz.Digraph("Left")
left_graph.edge("Left", "Graph")
right_graph = graphviz.Digraph("Right")
right_graph.edge("Right", "Graph")
col1, col2 = st.columns([1, 1])
with col1:
st.graphviz_chart(left_graph)
with col2:
st.graphviz_chart(right_graph)
dot_code = """
digraph Dot {
A -> {B, C, D} -> {F}
}
"""
st.graphviz_chart(dot_code)