Black Lives Matter. Please consider donating to Black Girls Code today.

APIs on Dash server

Hello all!
I’m beginning work on an application that will have multiple REST APIs (GET and POST APIs) as well as a couple of UIs (a configuration page, a status monitor, and an output summary report of data gathered) that I think would be a great fit for Dash, but not sure how to deploy the APIs as part of the Dash framework. Does Dash have easy functionality for REST APIs that will not have any on-screen output (just JSON or sometimes only a response code), or am I going to be better off running the APIs from a vanilla Flask server and the UIs from Dash?

Dash doesn’t have this functionality. It doesn’t need it. You can use the request library in combination with a Pandas dataframe for instance.

In simple steps:

  • Create a function that calls the API by using request library
  • Process the result (when it’s a GET) with Json library and put it in a Pandas dataframe or list / dict.
  • Use the dataframe or list as the input for a dash component. For instance a graph.

Let me know if you need more help.

Greetings,

Sempah

@Sempah, I might not have been totally clear. This is to be a server-side application, so the Dash server would not be making these API requests themselves but be servicing them.

Ah, that changes everything. Sorry perhaps I misunderstood. Not my speciality but I don’t think that Dash has features for that.

1 Like

@Sempah no worries, I wasn’t entirely clear myself. From what I was seeing (both in the main documentation as well as in the searches I ran through the forum) there doesn’t seem to be support for Dash server running the server-side of programmatic APIs, but maybe @chriddyp can weigh in on this.

@atkina remember that the underlying Flask instance is still accessible. Just create normal API endpoints with the server object and run the server. This example works.

from flask import jsonify
import dash
import dash_html_components as html

app = dash.Dash(__name__,url_base_pathname='/app/')
server = app.server

app.layout = html.Div('show this content')

@server.route('/')
def route1():
    return jsonify({'message':'this is the first route'})

if __name__=='__main__':
    server.run(debug=True,port=8050)
1 Like

@russellthehippo thanks for your help! I had to update the server route from ‘/’ to ‘/api’ to get it to redirect away from the main page, but worked great from there.