How to add RESTful API endpoints to a Dash app?

According to the source code of class Dash and the default parameter server Flask is used as default server. I guess it is possible to add RESTful API endpoints to my Dash app using plain requests, flask-restful or some other Flask extension. Is this correct?

It it possible to use another server (e.g. fastapi) instead of Flask? This point was not clear to me cause according to references in the source code the implementation of callback seems to be hard coded specific to Flask.



it’s possible to add endpoints with Dash app, this is how dash itself handles the endpoints

with the current architect, there is no easy way to decouple Flask in but there are several community projects already did something you are asking:

  2. dashR is an official example for cross-language serving, we used Fiery from R

According to Dash._add_url()s “under” this is not part of the public API, I guess.

Thanks for the hints. I Know django-plotly-dash already. However I was looking for a lightweight solution. I’m a big fan of fast-api and would love to use it with dash.

yes, _add_url() is not public, but the implementation shows that it’s just a wrapper on app.server.add_url_rule()

we have discussed the restructure of dash code internally, but the easy-switch-to-another-framework won’t be available in the short term.
another thing to point out is fast-api requires 3.6+, and now dash has to support both python 2 and 3.

again, the porting to fast-api is feasible in python3, but I’m afraid there is no easy switch right now.

Too sad!

Good point!

A kind of FlaskRestful API “hello world”:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from flask import Flask
from flask_restful import Resource, Api

server = Flask('my_app')
app = dash.Dash(server=server)
api = Api(server)

class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

api.add_resource(HelloWorld, '/hello')

app.layout = html.Div([
1 Like