Python Forum
Issue accessing data from Dictionary/List in the right format - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Issue accessing data from Dictionary/List in the right format (/thread-28570.html)



Issue accessing data from Dictionary/List in the right format - LuisSatch - Jul-24-2020

Hi all, I'm using an API to get date-time and temperature for a given coordinates and put that in a chart. Data is flowing fine from server to client, but the chart is not showing any data points. My guess is that my code on the client side is not accessing the data correctly. Any ideas would be very appreciated.


#THIS IS THE FUNCTION ON THE SERVER SIDE
@anvil.server.callable
def get_weather_data(latitude, longitude):
url = "https://api.openweathermap.org/data/2.5/forecast?lat=%s&lon=%s&units=metric&appid=XXXXXXXXXXXX" % (latitude, longitude)
resp = anvil.http.request(url,json = True)
forecast_list = resp['list']
temp = [x['main']['temp'] for x in forecast_list]
date_dtime = [x['dt_txt'] for x in forecast_list]
return { 'date_time' : date_dtime, 'temp': temp }

The server is sending data correctly in the following format:
[{'time':['2020-07-23 12:00:00', '2020-07-23 15:00:00', '2020-07-23 18:00:00'], 'temp':[8.81, 7.15, 5.83]}]

However, I'm doing something wrong when accessing the data (see code in red color)
#THIS IS THE FUNCTION ON THE CLIENT SIDE

def build_weather_graph(self):
mydata = anvil.server.call('get_weather_data', -37.814, 144.9633)
self.temp_data.append({'time':mydata['date_time'], 'temp':mydata['temp']})
self.plot_4.data = go.Scatter(x=[n['time'] for n in self.temp_data],
y=[n['temp'] for n in self.temp_data],

line=dict(color='#2196f3')
)

If instead, I put the data directly, everything works (see code in greencolor)
def build_weather_graph(self):
mydata = anvil.server.call('get_weather_data', -37.814, 144.9633)
self.temp_data.append({'time':mydata['date_time'], 'temp':mydata['temp']})
self.plot_4.data = go.Scatter(x=['2020-07-23 12:00:00', '2020-07-23 15:00:00', '2020-07-23 18:00:00'],
y=[8.81, 7.15, 5.83],

line=dict(color='#2196f3')
)

I know this is simple, but I'm confused. Hope someone can help.

(My original code has indentations that are not showing here).


RE: Issue accessing data from Dictionary/List in the right format - eismb - Jul-24-2020

Your server function get_weather_data returns a dict whose keys point to two lists, one with strings ('time') and another with numbers ('temp'). When I try to reproduce your code:

>>> temp_data = {'time':['2020-07-23 12:00:00', '2020-07-23 15:00:00', '2020-07-23 18:00:00'], 'temp':[8.81, 7.15, 5.83]}

>>> x = [n['time'] for n in temp_data]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string indices must be integers, not str

>>> [type(n) for n in temp_data]
[<type 'str'>, <type 'str'>]

>>>
Because temp_data is the one variable that links to a dict and can be invoked with keys, what happens if you change the line in red with this one:

self.plot_4.data = go.Scatter(x=[n for n in self.temp_data['time']],
y=[n for n in self.temp_data['temp']],
line=dict(color='#2196f3')



RE: Issue accessing data from Dictionary/List in the right format - LuisSatch - Jul-25-2020

When I changed the line in red with the suggested code I get another error: 'TypeError: list indices must be integers, not str'. Any ideas?