st.bokeh_chart

显示互动式虚化图。

Bokeh 是 Python 的一个图表库。此函数的参数与 Bokeh 的 show 函数的参数非常接近。有关 Bokeh 的更多信息,请访问 https://bokeh.pydata.org。

要在 Streamlit 中显示 Bokeh 图表,请在调用 Bokeh 的 show 时调用 st.bokeh_chart。

代码

import streamlit as st
from bokeh.plotting import figure

x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

p = figure(
    title='simple line example',
    x_axis_label='x',
    y_axis_label='y')

p.line(x, y, legend_label='Trend', line_width=2)

st.bokeh_chart(p, use_container_width=True)

这段代码使用了Streamlit和Bokeh库来创建一个简单的折线图。首先,我们导入了streamlit和bokeh.plotting中的figure模块。然后,我们定义了x和y轴的数据点。接下来,我们创建了一个Bokeh图形对象p,并设置了标题、x轴标签和y轴标签。然后,我们使用p.line()方法在图形对象上绘制了折线,设置了图例标签和线宽。最后,我们使用st.bokeh_chart()方法将图形对象p显示在Streamlit应用程序中,并设置了use_container_width=True以适应容器的宽度。Bokeh

Python应用开发——30天学习Streamlit Python包进行APP的构建(11)-LMLPHP

st.graphviz_chart 

使用 dagre-d3 库显示图表。

代码

import streamlit as st
import graphviz

# Create a graphlib graph object
graph = graphviz.Digraph()
graph.edge('run', 'intr')
graph.edge('intr', 'runbl')
graph.edge('runbl', 'run')
graph.edge('run', 'kernel')
graph.edge('kernel', 'zombie')
graph.edge('kernel', 'sleep')
graph.edge('kernel', 'runmem')
graph.edge('sleep', 'swap')
graph.edge('swap', 'runswap')
graph.edge('runswap', 'new')
graph.edge('runswap', 'runmem')
graph.edge('new', 'runmem')
graph.edge('sleep', 'runmem')

st.graphviz_chart(graph)

这段代码使用了Streamlit和Graphviz库。首先,导入了streamlit和graphviz模块。然后创建了一个graphviz的有向图对象graph。接着,通过调用edge方法,向图中添加了一系列的边,构成了一个简单的图形结构。最后,使用st.graphviz_chart方法将图形显示在Streamlit应用程序中。整体来说,这段代码的作用是创建一个简单的有向图并在Streamlit应用程序中展示出来。或者,您也可以使用 GraphViz 的 Dot 语言从图形中渲染图表:

st.graphviz_chart('''
    digraph {
        run -> intr
        intr -> runbl
        runbl -> run
        run -> kernel
        kernel -> zombie
        kernel -> sleep
        kernel -> runmem
        sleep -> swap
        swap -> runswap
        runswap -> new
        runswap -> runmem
        new -> runmem
        sleep -> runmem
    }
''')

Python应用开发——30天学习Streamlit Python包进行APP的构建(11)-LMLPHP

 st.plotly_chart

显示交互式 Plotly 图表。

Plotly 是 Python 的图表库。该函数的参数与 Plotly 的 plot() 函数的参数非常接近。

要在 Streamlit 中显示 Plotly 图表,请在调用 Plotly 的 py.plot 或 py.iplot 时调用 st.plotly_chart。

06-28 19:17