plotly / Dash.jl

Dash for Julia - A Julia interface to the Dash ecosystem for creating analytic web applications in Julia. No JavaScript required.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for user-defined routes

rpkyle opened this issue · comments

As described in https://dash.plot.ly/integrating-dash and seen in code examples posted to our community forum, Dash for Python permits app developers to leverage Flask support for adding new routes.

Dash.jl currently lacks this functionality; sample code in Dash for Python looks like this:

@app.callback(Output('my-link', 'href'), [Input('my-dropdown', 'value')])
def update_link(value):
    return '/dash/urlToDownload?value={}'.format(value)

@app.server.route('/dash/urlToDownload') 
def download_csv():
    value = flask.request.args.get('value')
    # create a dynamic csv or file here using `StringIO` 
    # (instead of writing to the file system)
    strIO = StringIO.StringIO()
    strIO.write('You have selected {}'.format(value)) 
    strIO.seek(0)    
    return send_file(strIO,
                     mimetype='text/csv',
                     attachment_filename='downloadFile.csv',
                     as_attachment=True)

User-defined routes are helpful generally, but particularly for documentation and applications where URL redirects are required.

The HTTP package does not provide the "server" entity. The "server" in HTTP is a function that processes incoming requests. The most appropriate HTTP approach is to make make_handler part of the external API, then the user can make their own handler look like:

dash_handler = make_handler(app)
function my_handler(request::HTTP.Request)
     #do my staff
     ......
     #if my staff don't return anything
    HTTP.handle(dash_handler, request)
end

Theoretically, I can make the internal Dash router public, but then we need to include its documentation in the Dash documentation, because it is not part of HTTP. And this can confuse the user.
And even in this case, providing the make_handler to the user should remain, because in addition to routing, the user may need to pre / or post processing of each request. For example for authorization

The HTTP package does not provide the "server" entity. The "server" in HTTP is a function that processes incoming requests.

This isn’t a critical feature to implement now, just noting here for parity with Dash for Python.

Support for this feature in Dash for R was added via plotly/dashR#225; related improvements will be made in Dash for Python as described in plotly/dash#1370.