Learn how to use Dash Bio for next-gen sequencing & quality control. 🧬 Register for the Oct 27 webinar.

How to save a file on the client side?

The app is running on a server (remote host). A client can access the server remotely. dcc.Upload() can take input from the client’s machine. After processing the client’s input, is there a function to save the processed data back to the client-side?

For a better idea about what I mean, please see the # TODO block in the simple example below:

import base64, io
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
import pandas as pd

app = dash.Dash(__name__)

app.layout = html.Div([

    dcc.Upload(
        id='csv',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
    ),

    html.Br(),
    html.Div(id='page-content')

])

@app.callback(Output('page-content','children'),
              [Input('csv','contents')])
def update_dropdown(raw_contents):

    if not raw_contents:
        raise PreventUpdate

    _, contents = raw_contents.split(',')
    decoded = base64.b64decode(contents)
    df = pd.read_csv(io.StringIO(decoded.decode('utf-8')))

    # TODO
    # I can only save on the server
    # df.to_csv('/save/on/client/side.csv')

    return 'File loaded, saving ...'

if __name__=='__main__':
    app.run_server(debug=True, port=8050, host='localhost')

Why would you want to save it clientside?

Because that is client’s and he needs the processed data back.

So you essentially need a download button? Or would the save dialog be a problem?

But that would require user confirmation, right? I was looking for something automated. In my example, df.to_csv() won’t need any user confirmation to save it on the server side. I am looking for a similar way to save it on the client side.

Where do I get a download button or save dialogue anyway?

I made a Download component, which should be easy to use,

It opens a dialogue though. It sounds like what you really want is access to the client file system, but you generally cannot get that for security reasons.

1 Like