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

Create table with data from an API

Hi, I am trying to create a table from data from an API. I want to grab a parameter from the URL and make a call and plot a table. However, it does not work.

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

def dummy():
#Get parameter from request url
#make API call here with parameter but it's not working
df = request.get('https://...')
         layout = html.Div(style= { 'backgroundColor': color['background'] }, children=[
         html.H4('US Agriculture Exports (2011)'),
         generate_table(df)
     ])
     return layout

app.layout = dummy

Hi, could you use a print statement to check whether df is a valid dataframe? Also, could you post the code of generate_table so that we can check it?

Thank you for your response, I think the issue I’m having is how would I be able to grab a parameter from the URL. In this instance i see the browser is making calls to
when I call

localhost:8050/1005

then I see the browser making another call to get_layout

localhost:8050/get_layout

Here is the function:

def generate_table(dataframe, max_rows=10):
     print(dataframe.columns)
     print('hello')
     return html.Table(
         # Header
         [html.Tr([html.Th(col) for col in dataframe.columns])] +

         # Body
         [html.Tr([
             html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
         ]) for i in range(min(len(dataframe), max_rows))]
     )

however dataframe should come from an API based on for example, a userId from the parameter. But it seems like that’s being stripped when call to get_layout. Does this make sense?

So the flow is:

  1. grab userId from url
  2. make call to our endpoint with userId to get user data
  3. return generateTable with that user data.