✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Output path selector like dcc.Upload

Hi all,

Is there a function for taking interactive user input for output directory or file path? In this doc, dcc.Upload lets the user select input file through the file browser. I was hoping to find something similar to that for specifying output directory.

Ping everyone …
Following up on my question.

Can you elaborate a bit on what you are trying to accomplish?

@np8, see FIXME tag:

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
from os.path import join as pjoin

app = dash.Dash(__name__)

app.layout = html.Div([

    # input file is selected from a dialogue box
    dcc.Upload(
        id='csv',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
        style={
            'width': '30%',
            'height': '40px',
            'lineHeight': '40px',
            'borderWidth': '1px',
            'borderStyle': 'dashed',
            'borderRadius': '5px',
            'textAlign': 'center',
        },
    ),

    html.Br(),
    html.Br(),
    'Enter output directory ',

    # FIXME can output directory also be selected from a dialogue box ?
    dcc.Input(id='outDir'),

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

])

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

    if not raw_contents:
        raise PreventUpdate

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

    df.to_csv(pjoin(outDir,'out.csv'))

    return 'File loaded, saving ...'

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

Are you trying to automatically write file’s to the users PC? Does one of the cases below resemble your use case?

Case 1

  • Dash app runs on PC1
  • User A uses browser on PC2 to connect to the dash application
  • User A uses dcc.Upload component to upload data. The data is uploaded to the browser memory on PC2
  • Upload process triggers a callback on the server. The data is sent from browser memory of PC2 to the server on PC1. Dash app does some computations on the data.
  • Used A wants to get the processed data.

Possible solution: Download button / download action. It is not possible to save the data automatically to the user’s computer (PC2) without some very special arrangements (usually server has zero access to user’s PC).

Case 2

  • Dash app runs on PC1
  • User A uses browser on the same computer, PC1 to access the dash app
  • User A uses dcc.Upload component to upload data
  • Some callbacks are triggered and computations are done based on the data
  • User A wants to get the processed data

Possible solutions: Download button or saving data automatically to the PC1.

1 Like

Are you trying to automatically write file’s to the users PC?

No.

But thanks for the summary @np8. I think if you slightly rephrase the last line, that would be more useful–

saving data by Python code to the PC1.

I sensed my answer from your summary.