Plotly Dash 表回调

2023-11-25

我试图让滑块、用户输入和表格之间的依赖关系发挥作用。我尝试输出数据并使用回调来更新它。建议我只在回调中创建表并使用“Div”。定义其在显示中的位置。

其他信息:

  • 表是使用 dash_table 库从 pandas DataFrame 创建的。
  • 数据是字典格式。
  • 与变量threshold由用户输入(滑块或输入)调整的值

如果有人能帮助我找出为什么表格不显示的原因,我将不胜感激?

这是我的代码:


import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from dash.dependencies import Input, Output
import dash_table

threshold = 0.5
################################################################
###################### Table Data ##############################
################################################################

metrics_index = ["AUC", "Accuracy", "Kappa", "Sensitivity (Recall)", "Specificity", "Precision", "F1"]

algo_columns = ["Test-SVM+Naïve B", "RF"]

table_data = {"AUC": [threshold * 0.8, threshold * 0.83],
              "Accuracy": [threshold * 0.85, threshold * 0.86],
              "Kappa": [threshold * 0.66, threshold * 0.69],
              "Sensitivity (Recall)": [threshold * 0.82, threshold * 0.83],
              "Specificity": [threshold * 0.78, threshold * 0.79],
              "Precision": [threshold * 0.78, threshold * 0.79],
              "F1": [threshold * 0.81, threshold * 0.82]}

data = [i for i in table_data]
table = pd.DataFrame(columns=algo_columns, index=metrics_index, data=[table_data[i] for i in metrics_index])
# display(table)


################################################################
########################  Body  ################################
################################################################


body = dbc.Container(
    [
        dbc.Row(
            [
                dbc.Col(
                    [
                        html.H2("Slider + Manual entry test"),
                        dcc.Slider(
                            id='my-slider',
                            min=0,
                            max=1,
                            step=0.01,
                            marks={"0": "0", "0.5": "0.5", "1": "1"},
                            value=threshold
                        ),
                        html.Div(id='update-table')
                    ]
                ),
                dbc.Col(
                    [
                        html.Div(
                            [
                                html.Div(
                                    dcc.Input(id='input-box', type='float', max=0, min=1, step=0.01, value=threshold)
                                    ),
                                 html.Div(id='slider-output-container')
                            ]
                        )
                    ]
                )
            ]
        )
    ]
)

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

app.layout = html.Div([body])


##############################################################
######################## callbacks ###########################
##############################################################

@app.callback(
    dash.dependencies.Output('slider-output-container', 'children'),
    [dash.dependencies.Input('my-slider', 'value')]
)
def update_output(value):
    threshold = float(value)
    return threshold


# call back for slider to update based on manual input
@app.callback(
    dash.dependencies.Output(component_id='my-slider', component_property='value'),
    [dash.dependencies.Input('input-box', 'value')]
)
def update_output(value):
    threshold = float(value)
    return threshold


# call back to update table

@app.callback(
    dash.dependencies.Output('update-table', 'children'),
    [dash.dependencies.Input('my-slider', 'value')]
)
def update_output(value):
    threshold = float(value)
    table_data = {"AUC": [threshold * 0.8, threshold * 0.83],
                  "Accuracy": [threshold * 0.85, threshold * 0.86],
                  "Kappa": [threshold * 0.66, threshold * 0.69],
                  "Sensitivity (Recall)": [threshold * 0.82, threshold * 0.83],
                  "Specificity": [threshold * 0.78, threshold * 0.79],
                  "Precision": [threshold * 0.78, threshold * 0.79],
                  "F1": [threshold * 0.81, threshold * 0.82]}


    return dash_table.DataTable(
                            id='update-table',
                            data= table_data.to_dict('records'),
                            columns=[{'id': x, 'name': x} for x in table.columns]
                )


if __name__ == "__main__":
    app.run_server()


[表格实时动态编辑的屏幕截图]

import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import pandas as pd

from dash.dependencies import Input, Output

threshold = 0.5
################################################################
###################### Table Data ##############################
################################################################

metrics_index = [
    "AUC",
    "Accuracy",
    "Kappa",
    "Sensitivity (Recall)",
    "Specificity",
    "Precision",
    "F1",
]

algo_columns = ["Test-SVM+Naïve B", "RF"]

table_data = {
    "AUC": [threshold * 0.8, threshold * 0.83],
    "Accuracy": [threshold * 0.85, threshold * 0.86],
    "Kappa": [threshold * 0.66, threshold * 0.69],
    "Sensitivity (Recall)": [threshold * 0.82, threshold * 0.83],
    "Specificity": [threshold * 0.78, threshold * 0.79],
    "Precision": [threshold * 0.78, threshold * 0.79],
    "F1": [threshold * 0.81, threshold * 0.82],
}

data = [i for i in table_data]
table = pd.DataFrame(
    columns=algo_columns,
    index=metrics_index,
    data=[table_data[i] for i in metrics_index],
)
# display(table)


################################################################
########################  Body  ################################
################################################################


body = dbc.Container(
    [
        dbc.Row(
            [
                dbc.Col(
                    [
                        html.H2("Slider + Manual entry test"),
                        dcc.Slider(
                            id="my-slider",
                            min=0,
                            max=1,
                            step=0.01,
                            marks={"0": "0", "0.5": "0.5", "1": "1"},
                            value=threshold,
                        ),
                        html.Div(id="update-table"),
                    ]
                ),
                dbc.Col(
                    [
                        html.Div(
                            [
                                html.Div(
                                    dcc.Input(
                                        id="input-box",
                                        max=0,
                                        min=1,
                                        step=0.01,
                                        value=threshold,
                                    )
                                ),
                                html.Div(id="slider-output-container"),
                            ]
                        )
                    ]
                ),
            ]
        )
    ]
)

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

app.layout = html.Div([body])


##############################################################
######################## callbacks ###########################
##############################################################


@app.callback(
    dash.dependencies.Output("slider-output-container", "children"),
    [dash.dependencies.Input("my-slider", "value")],
)
def update_output(value):
    threshold = float(value)
    return threshold


# call back for slider to update based on manual input
@app.callback(
    dash.dependencies.Output(component_id="my-slider", component_property="value"),
    [dash.dependencies.Input("input-box", "value")],
)
def update_output(value):
    threshold = float(value)
    return threshold


# call back to update table


@app.callback(
    dash.dependencies.Output("update-table", "children"),
    [dash.dependencies.Input("my-slider", "value")],
)
def update_output(value):
    threshold = float(value)
    table_data = pd.DataFrame.from_dict(
        {
            "AUC": [threshold * 0.8, threshold * 0.83],
            "Accuracy": [threshold * 0.85, threshold * 0.86],
            "Kappa": [threshold * 0.66, threshold * 0.69],
            "Sensitivity (Recall)": [threshold * 0.82, threshold * 0.83],
            "Specificity": [threshold * 0.78, threshold * 0.79],
            "Precision": [threshold * 0.78, threshold * 0.79],
            "F1": [threshold * 0.81, threshold * 0.82],
        }
    )

    return html.Div(
        [
            dash_table.DataTable(
                data=table_data.to_dict("rows"),
                columns=[{"id": x, "name": x} for x in table_data.columns],
            )
        ]
    )


if __name__ == "__main__":
    app.run_server(host="0.0.0.0", port=8050, debug=True, dev_tools_hot_reload=True)

我尝试了一下,似乎可以使用上面稍微修改过的代码来工作;我必须做出的改变是:

  1. 转换字典table_data进入数据框(这允许.to_dict()方法,这是一个可以工作的 pd.DataFrame 方法!)
    table_data = pd.DataFrame.from_dict(
        {
            "AUC": [threshold * 0.8, threshold * 0.83],
            "Accuracy": [threshold * 0.85, threshold * 0.86],
            "Kappa": [threshold * 0.66, threshold * 0.69],
            "Sensitivity (Recall)": [threshold * 0.82, threshold * 0.83],
            "Specificity": [threshold * 0.78, threshold * 0.79],
            "Precision": [threshold * 0.78, threshold * 0.79],
            "F1": [threshold * 0.81, threshold * 0.82],
        }
    )
  1. Also in update_output回调函数:

    • A. 将 df .to_dict 方法调用的“记录”更改为“行”
    • B. 你有 table 而不是 table_data 作为列参数
    • C. 删除使用id这里是 Dash 参数,b/c 它已经在布局中了
    return html.Div(
        [
            dash_table.DataTable(
                data=table_data.to_dict("rows"),
                columns=[{"id": x, "name": x} for x in table_data.columns],
            )
        ]
    )

  1. 看起来您已经切换了最大和最小! (最大零不会留下很多可能的输入![实际上,没有..]);放置小数点和匹配精度也可能很重要,我添加这些是为了以防万一。
                                html.Div(
                                    dcc.Input(
                                        id="input-box",
                                        max=1.00,
                                        min=0.00,
                                        step=0.01,
                                        value=threshold,
                                        type="number"
                                    )
                                ),
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Plotly Dash 表回调 的相关文章

随机推荐

  • 将 cookie 加载到 Python 请求会话时出错

    我正在尝试从 selenium 导出的 cookie 将 cookie 加载到 Python 中的请求会话中 但是当我这样做时 它会返回以下错误 list 对象没有属性 extract cookies def load cookies fi
  • 为不同的Linux版本构建内核模块

    我是编写内核模块的新手 因此面临很少的非技术问题 由于为特定内核版本 例如 3 0 0 10 10 是补丁号 创建内核模块需要相同版本的内核头文件 因此看起来直接安装内核头文件并在那里开始开发 但修补内核版本的内核头不可用 因为我有一个来宾
  • 为什么这个嵌套 lambda 不被视为 constexpr?

    我正在尝试使用嵌套 constexpr lambda 创建一个柯里化接口 但编译器不认为它是常量表达式 namespace hana boost hana using namespace hana literals struct C1 te
  • 如何验证 CuDNN 安装?

    我搜索了很多地方 但我得到的只是如何安装它 而不是如何验证它是否已安装 我可以验证我的 NVIDIA 驱动程序是否已安装 并且 CUDA 是否已安装 但我不知道如何验证 CuDNN 是否已安装 非常感谢您的帮助 谢谢 PS 这是用于咖啡实现
  • 中心绝对定位的div [重复]

    这个问题在这里已经有答案了 我有一个 div 里面有一个按钮 我让按钮位置为absolute 及其样式代码 buy btn text align center position absolute bottom 10px 我怎样才能将其对齐到
  • Git 大提交最佳实践

    我正在使用 git 上周我在本地存储库上进行了大量工作 我喜欢进行许多小提交 例如 2 或 3 个文件 而不是大提交 但这一次由于时间压力和更改涉及许多文件 我有很多文件未暂存 我的问题是 这种情况下的最佳实践是什么 有没有什么工具可以让我
  • 如何制作一个在 IE 和 Firefox 中都有效的 file:// 超链接?

    在我的文档网页中 我经常需要提供指向存储在 Intranet 上的位置 文件和应用程序 xbap 的链接 在 IE 中 这对于格式如下的 URL 来说效果很好 a href Go to folder a a href Download fi
  • 如何为 UITextField 中的占位符文本设置可访问性特征?

    我正在检查我们的 iOS 应用程序来解决辅助功能问题 该应用程序的功能之一是 UITextField 用户可以在其中输入搜索查询 我将该字段的特征设置为 搜索字段 并且 VoiceOver 大部分时间都对该字段做得很好 当字段中有文本时 它
  • 按列而不是按行对结果进行排序

    SQL中可以按列排序而不是按行排序吗 我不需要基本的 ORDER BY 语句 我知道它们是如何工作的 即 按列 1 列 2 等排序 基本上尝试对这样的事情进行排序 column 1 column 2 column 3 1 0 3 尝试对此进
  • 如何检测用户是否启用了 Mac OS 高对比度辅助功能设置?

    我有一个 React TypeScript 项目 我试图检测用户是否启用了任何 Mac OS 的高对比度辅助功能设置 反转颜色 使用灰度 区分无颜色 增加对比度或增加的显示对比度设置 我想使用 JavaScript TypeScript 检
  • Windows 汇编堆和堆栈?

    操作系统 Windows 7 32位 所以像c 一样 有一个堆和一个堆栈 但我最近开始进行一些汇编学习 但没有看到任何类似的东西 只有一个堆栈 但它看起来就像纯粹的内存 那么堆和栈的实现是特定于 C 和其他语言的吗 或者您仍然在汇编中分配堆
  • 无法将 MyObject 类型的对象转换为 MyObject 类型

    我有这样的场景 我在 C 中使用的 Web 服务方法返回一个 Business 对象 当使用以下代码调用 Web 服务方法时 我在 reference cs 类中收到异常 无法将 ContactInfo 类型的对象转换为 ContactIn
  • Struts 2 s:选择标签动态id

    我在一个 JSP 页面和一个按钮中有多个不同类型的字段 这些字段是根据从我创建的元数据表中获取的信息生成的 由于我不知道存在多少个字段以及什么类型的字段 我给动态id给他们 我在 JSP 中使用 Struts 2 标签 问题在于
  • 如何在 gtk 中设置光标位置 -Linux、MonoDevelop

    好吧 所以我想我会进入 Linux 开发 然而 我发现你很难操纵鼠标和键盘 我的问题是 GTK 不支持这类事情 还是我错过了什么 在 Windows 中很简单 Point Pos new Point 20 20 Cursor Positio
  • LinQ 如何将 1 这样的整数更改为字符串 001

    我的数据库中有一个包含四列的表 string year string weeknr int number 在其他表中 我将这些列组合成yywwnnn作为字符串 数字列是标识列 现在我想从要与上述表连接的表中检索一些记录 就像是 from R
  • 如何在 C++ 中使用 python 库?

    我想使用nltkC 中的库 我可以使用胶水语言 机制来做到这一点吗 原因 我已经有一段时间没有用 C 进行任何认真的编程了 并且想同时修改 NLP 概念 Thanks 尽管从 python 调用 c libs 更为正常 您可以通过基本调用
  • 如何使用python从mp3文件中提取原始数据?

    我有关于使用 Python 进行音频数据分析的作业 我想知道是否有任何好的模块可供我用来从 mp3 文件中提取原始数据 我的意思是原始数据 而不是元数据 id3 标签 我知道如何使用wave要处理的模块 wav文件 我可以readframe
  • 带点的 DataTable 列名称为何不适合 WPF 的 DataGrid 控件?

    运行这个 你会感到困惑
  • 使用 PyQt4 进行核心转储

    我从 PyQt4 开始 在最简单的示例中使用 QGraphicsScene View 时面临核心转储 usr bin python import sys from PyQt4 QtCore import from PyQt4 QtGui i
  • Plotly Dash 表回调

    我试图让滑块 用户输入和表格之间的依赖关系发挥作用 我尝试输出数据并使用回调来更新它 建议我只在回调中创建表并使用 Div 定义其在显示中的位置 其他信息 表是使用 dash table 库从 pandas DataFrame 创建的 数据