streamlit

Форк
0
/
st_graphviz_chart.py 
100 строк · 2.7 Кб
1
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

15
import graphviz as graphviz
16

17
import streamlit as st
18

19
# basic graph
20
hello = graphviz.Digraph("Hello World")
21
hello.edge("Hello", "World")
22

23
# styled graph
24
styled = graphviz.Graph("G", filename="g_c_n.gv")
25
styled.attr(bgcolor="purple:pink", label="agraph", fontcolor="white")
26

27
with styled.subgraph(name="cluster1") as c:
28
    c.attr(
29
        fillcolor="blue:cyan",
30
        label="acluster",
31
        fontcolor="white",
32
        style="filled",
33
        gradientangle="270",
34
    )
35
    c.attr(
36
        "node", shape="box", fillcolor="red:yellow", style="filled", gradientangle="90"
37
    )
38
    c.node("anode")
39

40
# complex graph
41
engine = st.sidebar.radio(
42
    "Select engine",
43
    ["dot", "neato", "twopi", "circo", "fdp", "osage", "patchwork"],
44
)
45
st.sidebar.write(engine)
46
finite = graphviz.Digraph("finite_state_machine", filename="fsm.gv", engine=engine)
47
finite.attr(rankdir="LR", size="8,5")
48

49
finite.attr("node", shape="doublecircle")
50
finite.node("LR_0")
51
finite.node("LR_3")
52
finite.node("LR_4")
53
finite.node("LR_8")
54

55
finite.attr("node", shape="circle")
56
finite.edge("LR_0", "LR_2", label="SS(B)")
57
finite.edge("LR_0", "LR_1", label="SS(S)")
58
finite.edge("LR_1", "LR_3", label="S($end)")
59
finite.edge("LR_2", "LR_6", label="SS(b)")
60
finite.edge("LR_2", "LR_5", label="SS(a)")
61
finite.edge("LR_2", "LR_4", label="S(A)")
62
finite.edge("LR_5", "LR_7", label="S(b)")
63
finite.edge("LR_5", "LR_5", label="S(a)")
64
finite.edge("LR_6", "LR_6", label="S(b)")
65
finite.edge("LR_6", "LR_5", label="S(a)")
66
finite.edge("LR_7", "LR_8", label="S(b)")
67
finite.edge("LR_7", "LR_5", label="S(a)")
68
finite.edge("LR_8", "LR_6", label="S(b)")
69
finite.edge("LR_8", "LR_5", label="S(a)")
70

71
# draw graphs
72
st.graphviz_chart(hello)
73

74
st.graphviz_chart(styled)
75

76
st.graphviz_chart(finite)
77

78
# draw graphs in columns
79

80
left_graph = graphviz.Digraph("Left")
81
left_graph.edge("Left", "Graph")
82

83
right_graph = graphviz.Digraph("Right")
84
right_graph.edge("Right", "Graph")
85

86
col1, col2 = st.columns([1, 1])
87

88
with col1:
89
    st.graphviz_chart(left_graph)
90

91
with col2:
92
    st.graphviz_chart(right_graph)
93

94

95
dot_code = """
96
digraph Dot {
97
  A -> {B, C, D} -> {F}
98
}
99
"""
100
st.graphviz_chart(dot_code)
101

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.