【问题标题】:Dash error : 'dash_core_components' has no attribute 'send dataframe'破折号错误:“dash_core_components”没有属性“发送数据框”
【发布时间】:2022-01-03 12:04:24
【问题描述】:

在我的破折号中,我有一个回调,它创建了一个 pd.to_dict 对象,该对象与 dcc.Store 一起存储,以便用于进一步的绘图。

我正在尝试创建一个下载按钮来下载这个创建的数据框。

这是我的代码的一部分(删除了其他导入或布局选项):

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State


app = dash.Dash(__name__)

app.layout = html.Div([
        html.H2('Enter a text query'),
        dcc.Store(id='memory'),
        html.Button('Submit', id='button', n_clicks=0),
        html.Button("Download CSV", id="csv"),
        dcc.Download(id="download-df")])

@app.callback(
    Output('memory', 'data'),
    [Input('button', 'n_clicks')],
    [State('searchterm', 'value')]

)

def create_df_func(n_clicks, searchterm):
    #some code to create df
        return df.to_dict(orient='records')


@app.callback(
    Output('download-df', 'data'),
    [Input('csv', 'n_clicks')],
    [State('memory', 'data')],
    prevent_initial_call=True,
)
def download_csv(n_clicks, df):
    data = pd.DataFrame(df)
    return dcc.send_data_frame(data.to_csv, "mydf.csv")


if __name__ == '__main__':
    app.run_server(debug=True)

但是在运行 app.py 时,我得到了'dash_core_components' has no attribute 'send dataframe',即使它有它。

我有 dash 版本 2.0.0。

【问题讨论】:

  • 你没有专门导入send_data_frame,所以你应该使用dcc.send_data_frame
  • 我错误地粘贴了错误的代码版本。我已经编辑过了。我实际上已经使用了“dcc.send_data_frame”(当我开始输入时 PyCharm 建议使用“send_data_frame”),所以我不明白这个错误。

标签: python plotly-dash


【解决方案1】:

我有 dash 版本 2.0.0

替换你从这里导入dash核心组件的方式

import dash_core_components as dcc

到这里

from dash import dcc

从 Dash 2 开始,dash-core-components 的开发已移至 Dash 主仓库

quote source

【讨论】:

  • 谢谢,但现在我在单击下载按钮时遇到了这个问题:Error: Failed component prop type: Invalid component prop data` key mime_type 提供给下载。错误对象:{“content”:“IiINCg==”,“filename”:“mydf.csv”,“mime_type”:null,“base64”:true } 有效键:[“filename”,“content”,“base64 ", "类型" ]`
  • 我无法复制那个。可以分享data的价值吗?
  • 一切都好,又少了一个“dcc”。在我的代码中。非常感谢。
  • 很高兴为您提供帮助。顺便说一下,html 也应该从dash 导入。
  • 是的,我都改了。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多