streamlit
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
15import graphviz as graphviz
16
17import streamlit as st
18
19# basic graph
20hello = graphviz.Digraph("Hello World")
21hello.edge("Hello", "World")
22
23# styled graph
24styled = graphviz.Graph("G", filename="g_c_n.gv")
25styled.attr(bgcolor="purple:pink", label="agraph", fontcolor="white")
26
27with styled.subgraph(name="cluster1") as c:
28c.attr(
29fillcolor="blue:cyan",
30label="acluster",
31fontcolor="white",
32style="filled",
33gradientangle="270",
34)
35c.attr(
36"node", shape="box", fillcolor="red:yellow", style="filled", gradientangle="90"
37)
38c.node("anode")
39
40# complex graph
41engine = st.sidebar.radio(
42"Select engine",
43["dot", "neato", "twopi", "circo", "fdp", "osage", "patchwork"],
44)
45st.sidebar.write(engine)
46finite = graphviz.Digraph("finite_state_machine", filename="fsm.gv", engine=engine)
47finite.attr(rankdir="LR", size="8,5")
48
49finite.attr("node", shape="doublecircle")
50finite.node("LR_0")
51finite.node("LR_3")
52finite.node("LR_4")
53finite.node("LR_8")
54
55finite.attr("node", shape="circle")
56finite.edge("LR_0", "LR_2", label="SS(B)")
57finite.edge("LR_0", "LR_1", label="SS(S)")
58finite.edge("LR_1", "LR_3", label="S($end)")
59finite.edge("LR_2", "LR_6", label="SS(b)")
60finite.edge("LR_2", "LR_5", label="SS(a)")
61finite.edge("LR_2", "LR_4", label="S(A)")
62finite.edge("LR_5", "LR_7", label="S(b)")
63finite.edge("LR_5", "LR_5", label="S(a)")
64finite.edge("LR_6", "LR_6", label="S(b)")
65finite.edge("LR_6", "LR_5", label="S(a)")
66finite.edge("LR_7", "LR_8", label="S(b)")
67finite.edge("LR_7", "LR_5", label="S(a)")
68finite.edge("LR_8", "LR_6", label="S(b)")
69finite.edge("LR_8", "LR_5", label="S(a)")
70
71# draw graphs
72st.graphviz_chart(hello)
73
74st.graphviz_chart(styled)
75
76st.graphviz_chart(finite)
77
78# draw graphs in columns
79
80left_graph = graphviz.Digraph("Left")
81left_graph.edge("Left", "Graph")
82
83right_graph = graphviz.Digraph("Right")
84right_graph.edge("Right", "Graph")
85
86col1, col2 = st.columns([1, 1])
87
88with col1:
89st.graphviz_chart(left_graph)
90
91with col2:
92st.graphviz_chart(right_graph)
93
94
95dot_code = """
96digraph Dot {
97A -> {B, C, D} -> {F}
98}
99"""
100st.graphviz_chart(dot_code)
101