You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python Shiny中为Plotly图形对象添加配置(无show()方法)

在Python版Shiny中配置Plotly图形

在Python Shiny中无法直接调用Plotly图形对象的show()方法设置配置,但可以通过以下两种方式实现相同需求:

方法1:在render.plotly中传入config参数

定义渲染函数时,直接将配置字典作为config参数传递给render.plotly装饰器:

from shiny import App, render, ui
import plotly.express as px

app_ui = ui.page_fluid(
    ui.output_plotly("my_plot")
)

def server(input, output, session):
    @output
    @render.plotly(config={
        'modeBarButtonsToRemove': [
            'autoScale2d', 'select2d', 'lasso2d', 'pan2d', 'zoom2d', 'zoomOut2d', 
            'hoverClosestCartesian', 'hoverCompareCartesian',
            'toggleSpikelines', 'sendDataToCloud', 'editInChartStudio'
        ],
        'displaylogo': False
    })
    def my_plot():
        # 生成示例Plotly图形
        df = px.data.iris()
        fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
        return fig

app = App(app_ui, server)

方法2:直接修改Plotly图形对象的config属性

创建完图形对象后,直接为其config属性赋值配置字典:

from shiny import App, render, ui
import plotly.express as px

app_ui = ui.page_fluid(
    ui.output_plotly("my_plot")
)

def server(input, output, session):
    @output
    @render.plotly
    def my_plot():
        df = px.data.iris()
        fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
        # 直接设置图形的config属性
        fig.config = {
            'modeBarButtonsToRemove': [
                'autoScale2d', 'select2d', 'lasso2d', 'pan2d', 'zoom2d', 'zoomOut2d', 
                'hoverClosestCartesian', 'hoverCompareCartesian',
                'toggleSpikelines', 'sendDataToCloud', 'editInChartStudio'
            ],
            'displaylogo': False
        }
        return fig

app = App(app_ui, server)

两种方式都能实现与fig.show(config=...)完全一致的效果,包括隐藏Plotly logo、移除指定工具栏按钮等配置。

内容的提问来源于stack exchange,提问作者lambruscoAcido

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 20:04:53