Python Forum
Issue accessing data from Dictionary/List in the right format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue accessing data from Dictionary/List in the right format
#1
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).
Reply
#2
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')
Reply
#3
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 317 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Matching Data - Help - Dictionary manuel174102 1 353 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  List Comprehension Issue johnywhy 5 438 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  Dictionary in a list bashage 2 493 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 597 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Export data from PDF as tabular format zinho 5 645 Nov-11-2023, 08:23 AM
Last Post: Pedroski55
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  How to add list to dictionary? Kull_Khan 3 951 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,797 Dec-12-2022, 08:22 PM
Last Post: jh67
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 2,869 Dec-06-2022, 11:09 AM
Last Post: mg24

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020