Flet API problem. The app hangs on one line of code

49 Views Asked by At

I'm having a strange issue while creating a weather application in Flet. Specifically, after providing the location, the program fetches the coordinates of that place from the API and enters them into the weather_details class. There, the program hangs on the line of code "responses = self.openmeteo.weather_api(self.url, params=self.params)". The issue only pertains to calling this class from within the Flet framework. The API itself works in other cases, and the data for the function is also validated. Debugging hasn't helped.

This is weather_details.py:

import openmeteo_requests

import requests_cache
import pandas as pd
from retry_requests import retry
import pdb


class Weaher_details():
    def __init__(self, localisation):
        self.localisation = localisation
        self.url = "https://api.open-meteo.com/v1/forecast"
        self.cache_session = requests_cache.CachedSession('.cache', expire_after=10000)
        self.retry_session = retry(self.cache_session, retries=5, backoff_factor=2)
        self.openmeteo = openmeteo_requests.Client(session=self.retry_session)

    def current_weather(self):
        print("start current")
        self.params = {
            "latitude": self.localisation[1],
            "longitude": self.localisation[2],
            "current": ["temperature_2m", "relative_humidity_2m", "apparent_temperature", "is_day", "precipitation",
                        "rain", "showers", "snowfall", "cloud_cover", "wind_speed_10m"],
            "forecast_days": 1
        }
        print(type(self.params["latitude"]))
        try:
            print(f"{self.localisation[1], self.localisation[2]}")
"""problematic line of code--->"""  responses = self.openmeteo.weather_api(self.url, params=self.params)
            print(responses, "responses")
        except Exception as e:
            print("An error occurred:", e)

And here is main.py:

import flet
from flet import *
from location_details import get_location_details
from weather_details import Weaher_details


WINDOW_WIDTH = 412
WINDOW_HEIGHT = 732


def main(page:Page):
    page.horizontal_alignment = "center"
    page.vertical_alignment = "center"

    def _expand(e):
        _c.content.controls[0].height = WINDOW_HEIGHT * 0.8 if _c.content.controls[0].height == WINDOW_HEIGHT * 0.4 \
            else WINDOW_HEIGHT * 0.4
        _c.update()

    def _top():

        def get_location(e):
            localisation_data = get_location_details(new_location.value)
            if localisation_data == None:
                location_details.value =  "Error, cant find data about this location"
                page.update()
            if localisation_data:
                weather_details = Weaher_details(localisation_data)
                print(localisation_data)
                location_details.value = localisation_data[0]
                current_weather_details = weather_details.current_weather()
                print(current_weather_details)
                print("b")
                page.update()
            return localisation_data
...rest of code

In terminal:

['Warsaw, Masovian Voivodeship, Poland', 52.2319581, 21.0067249]
start current
<class 'float'>
(52.2319581, 21.0067249)

I attempted to convert the application to asynchronous, but it didn't work. Then, I created a small application in tkinter, and it worked. I've used the pdb, and the data just before calling the problematic line of code is of the correct type.

0

There are 0 best solutions below