0

In my dash I have a callback that create a pd.to_dict object that is stored with dcc.Store, in order to be used for further plots.

I am trying to create a download button to download this created data frame.

This is part of my code (removed other import or layout options) :

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)

But when running the app.py, I get 'dash_core_components' has no attribute 'send dataframe', even though it has it.

I have dash version 2.0.0.

2
  • You haven't specifically imported send_data_frame, so you should be using dcc.send_data_frame. Commented Jan 3, 2022 at 13:50
  • I’ve mistakenly pasted the wrong version of my code. I’ve edited it. I in fact already use ‘dcc.send_data_frame’ (and PyCharm suggest ‘send_data_frame’ when I start typing it), so I don’t understand the error. Commented Jan 3, 2022 at 16:07

1 Answer 1

1

I have dash version 2.0.0

Replace how you import dash core components from this

import dash_core_components as dcc

to this

from dash import dcc

As of Dash 2, the development of dash-core-components has been moved to the main Dash repo

quote source

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, but now I have this issue when clicking the download button : Error: Failed component prop type: Invalid component prop data` key mime_type supplied to Download. Bad object: { "content": "IiINCg==", "filename": "mydf.csv", "mime_type": null, "base64": true } Valid keys: [ "filename", "content", "base64", "type" ]`
I'm not able to reproduce that one. Can you share the value of data?
It's all good, there was another missing "dcc." in my code. Thanks a lot.
Glad to help. html should also by imported from dash by the way.
Yes, I've changed it all. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.