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


Messages In This Thread
Issue accessing data from Dictionary/List in the right format - by LuisSatch - Jul-24-2020, 05:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 2 658 Apr-29-2024, 04:38 PM
Last Post: Calab
  Help with to check an Input list data with a data read from an external source sacharyya 3 485 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Matching Data - Help - Dictionary manuel174102 1 432 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  List Comprehension Issue johnywhy 5 612 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  Dictionary in a list bashage 2 611 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 761 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Export data from PDF as tabular format zinho 5 783 Nov-11-2023, 08:23 AM
Last Post: Pedroski55
  How to add list to dictionary? Kull_Khan 3 1,059 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,971 Dec-12-2022, 08:22 PM
Last Post: jh67
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 3,143 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