We’re returning real-time temperature data from the API, so let’s store these values as we retrieve them.

Go back to Form1, and create a local variable in the __init__ method:

  # Initialise an empty list to store incoming temperature data
  self.temp_data = []

Next, add this function to plot the data:

def build_weather_graph(self):
  # Retrieve data from API. We'll use latitude and longitude for Cambridge for this example.
  weather_data = anvil.server.call('get_weather_data', 52.2053, 0.1218)
  # Add time and temperature data to our temp_data variable.
  self.temp_data.append({'time':weather_data['time'], 'temp':weather_data['temp']})
  # Create a Scatter plot with this data, and change the colour of the line
  self.plot_4.data = go.Scatter(y=[x['temp'] for x in self.temp_data], 
                                x=[x['time'] for x in self.temp_data], 

We want to plot this data when our app is opened, so let’s call it from the __init__ method of Form1. Your temp_data variable is used in the build_weather_graph function, so make sure you initialise the variable before you call self.build_weather_graph.

Your __init__ method should look like this:

  # Any code you write here will run when the form opens.
  self.temp_data = []