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

devtools/2024/9/23 23:31:10/

st.bokeh_chart

显示互动式虚化图。

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

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

Function signature[source]

st.bokeh_chart(figure, use_container_width=False)

Parameters

figure (bokeh.plotting.figure.Figure)

A Bokeh figure to plot.

use_container_width (bool)

Whether to override the figure's native width with the width of the parent container. If use_container_width is False (default), Streamlit sets the width of the chart to fit its contents according to the plotting library, up to the width of the parent container. If use_container_width is True, Streamlit sets the width of the figure to match the width of the parent container.

代码

python">import streamlit as st
from bokeh.plotting import figurex = [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

st.graphviz_chart 

使用 dagre-d3 库显示图表。

Function signature[source]

st.graphviz_chart(figure_or_dot, use_container_width=False)

Parameters

figure_or_dot (graphviz.dot.Graph, graphviz.dot.Digraph, str)

The Graphlib graph object or dot string to display

use_container_width (bool)

Whether to override the figure's native width with the width of the parent container. If use_container_width is False (default), Streamlit sets the width of the chart to fit its contents according to the plotting library, up to the width of the parent container. If use_container_width is True, Streamlit sets the width of the figure to match the width of the parent container.

代码

python">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 语言从图形中渲染图表:

python">st.graphviz_chart('''digraph {run -> intrintr -> runblrunbl -> runrun -> kernelkernel -> zombiekernel -> sleepkernel -> runmemsleep -> swapswap -> runswaprunswap -> newrunswap -> runmemnew -> runmemsleep -> runmem}
''')

 st.plotly_chart

显示交互式 Plotly 图表。

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

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

Function signature[source]

st.plotly_chart(figure_or_data, use_container_width=False, *, theme="streamlit", key=None, on_select="ignore", selection_mode=('points', 'box', 'lasso'), **kwargs)

Returns

(element or dict)

If on_select is "ignore" (default), this method returns an internal placeholder for the chart element. Otherwise, this method returns a dictionary-like object that supports both key and attribute notation. The attributes are described by the PlotlyState dictionary schema.

Parameters

figure_or_data (plotly.graph_objs.Figure, plotly.graph_objs.Data, or dict/list of plotly.graph_objs.Figure/Data)

The Plotly Figure or Data object to render. See Plotly Python Graphing Library for examples of graph descriptions.

use_container_width (bool)

Whether to override the figure's native width with the width of the parent container. If use_container_width is False (default), Streamlit sets the width of the chart to fit its contents according to the plotting library, up to the width of the parent container. If use_container_width is True, Streamlit sets the width of the figure to match the width of the parent container.

theme ("streamlit" or None)

The theme of the chart. If theme is "streamlit" (default), Streamlit uses its own design default. If theme is None, Streamlit falls back to the default behavior of the library.

key (str)

An optional string to use for giving this element a stable identity. If key is None (default), this element's ide


http://www.ppmy.cn/devtools/56091.html

相关文章

香港服务器ssh连接失败怎么处理?

当遇到香港服务器的SSH连接失败时,可能有多种原因导致,以下是一些常见的排查和处理方法: 1. 确认网络连接和服务器状态 网络连接问题: 确保本地网络正常,可以访问其他网站和服务。 使用 ping 命令检查服务器的网络连通…

使用Apache Zookeeper进行分布式协调

Apache Zookeeper是一个高可用的分布式协调服务,它为分布式应用提供了同步、配置维护、群组和命名服务等功能。Zookeeper的设计使得它能够处理大量并发请求,并且能够保证数据的一致性。本文将详细介绍如何使用Zookeeper进行分布式协调,并提供…

社交风潮塑造者:探索用户在Facebook的影响力

在当今数字化社会中,Facebook不仅是人们社交互动的主要平台,更是塑造社交风潮和文化趋势的重要力量。本文将从另一个角度深入探讨用户在Facebook上的影响力,探索其如何通过个人行为和互动,影响和改变社会的各个方面。 个人表达和内…

C++利用SIGSEGV信号处理实现发生segment fault后不崩溃的代码

C代码在内存越界或线程竞争情况下很容易出现未定义行为段错误,导致程序崩溃挂掉生成coredump,但如果想让程序遇到这种情况仍然可以继续运行,不中断服务呢?这里展示了一种方法。 C代码发生段错误后系统会抛出SIGSEGV 信号 &#x…

初学51单片机之长短键应用定时炸弹及扩展应用

51单片机RAM区域划分 51单片机的RAM分为两个部分,一块是片内RAM,一块是片外RAM。 data: 片内RAM从 0x00 ~0x7F 寻址范围(0-127) 容量共128B idata: 片外RAM从 0x00~0xFF 寻址范围(0-255) 容量共256B pdata&am…

从复用性角度阐述中台建设

目录 复用性中台定义深思中台建设产品线形态何时演变中台能力落地中台 业务中台架构总结 技术学习永不止步,最近也是看了很多关于架构设计相关的专栏,慢慢总结出来一部分知识,代入自己的思考与理解,以及结合并反思自己之前公司的架…

MCGS仿真教学3:动画的平移

目录 一、绘制图形和按钮功能二、移动动画三、添加功能变量四、添加循环脚本![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/f42c6b18e1cb4dbf8075c389878d3ae7.gif)五、完成演示 一、绘制图形和按钮功能 全部由单个矩形组成 二、移动动画 三、添加功能变量 四、…

Vue项目生产环境的打包优化

Vue项目生产环境的打包优化 前言 在这篇文章我们讨论Vue项目生产环境的打包优化,并按步骤展示实际优化过程中的修改和前后对比。 背景 刚开始的打包体积为48.71M 优化 步骤一:删除viser-vue viser-vue底层依赖antv/g2等库一并被删除,…