How to create dashboard with ability to import data with url request?

Hello,

right now I have a python script where I use the request library to fetch some data from an URL, the response is JSOn data and I put that into a pandas dataframe and then to csv. There is some input needed, start and end date and sensor name.

Know I want to have some user interface where I can have a list with the sensors and a date picker where I can select the start and end date. After I entered them I will hit a „download“ button and have the data fetched from the URL and saved it to csv and show a summary on the page.

My question, is that possible with dash? Datepicker is what I already saw, but how would I connect a button to fire up the request to fetch the data from the url?

Would appreciate in what direction I have to look and use.

Hello @Gobrel,

Yes, this is possible with Dash.

The first way, and possibly the easiest way is to have the server itself send the request on the backend and send the csv info to the front end.

Something like this:

resp = requests.post(url, headers=headers, body=body).json() # if encoded in json
df = pd.DataFrame(resp)
df.to_csv(file)
return 'imported data'
1 Like

What I can‘t grasp my head on is the interface. Say I have the date picker on the page and I select the dates. What is next? What component do I need to fire up the request? Maybe it is simple and yet I can‘t see it.:slight_smile:

Sure, you can setup an input based upon the date value:

@app.callback(Output('outputDiv','children'), Input('myDate','value'))
def import(date):
     #function from above

As easy as this is, I think it’d be better to use a button to submit or requery the info. To keep from wrong dates trying to be imported.

@app.callback(Output('outputDiv','children'), Input('button', 'n_clicks'), State('myDate','value'))
def import(n1, date):
     if n1:
         #function from above
1 Like

It seems to be more complex than I thought. I need at least three seperate callbacks as far as I can see. One for date input where I also have some logic for the initial data, one for the URL request+data storage which I can trigger. And the third one for the graph generation.

How do the callbacks work, when I fire up the dash server does it immediately try to do a request or does it wait to select a date?

It depends upon how you have it setup.

All callbacks, when first loaded will try to fire, this can cause quite a bit of requests to go to the server upon the client loading. However, for each callback that you do not want to fire, you can pass prevent_initial_call=True to disable this feature.

As far as the callbacks, I am thinking you’ll only need to use one as far as I can tell. :slight_smile:

You can read hear to find out more about callbacks, then once you are comfortable you can expand into more advanced callbacks as you need. :smiley: