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).
#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).