Python / Kivy Results - UrlRequest

I have been struggling with this for some time and cannot find a solution.

So, I studied Python and Kivy with "Building Applications in Kivy" by Dusty Phillips. This is a simple weather application, and when I try to get data from openweathermap.com, the UrlRequest function does not work properly. I am new to kivy and python, but as I see it, the function should call the "found_location" method with two arguments: request and result (a list obtained from the URL). If I access the url from my browser, I get the correct results, but back to python, the "results" appeared as NONE.

Here is the code with some fingerprints for debugging:

from kivy.app import App
#kivy.require("1.9.1")
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest

class AddLocationForm(BoxLayout):
    search_input = ObjectProperty()
    search_results = ObjectProperty()
    def search_location(self):

        search_template = "api.openweathermap.org/data/2.5/forecast/daily?APPID=ef4f6b76310abad083b96a45a6f547be&q=" + "{}"
        search_url = search_template.format(self.search_input.text)
        print search_url
        request = UrlRequest(search_url, self.found_location)
        print request
        print "Result: ", request.result

    def found_location(self, request, data):
        print request
        print data
        data = json.loads(data.decode()) if not isinstance(data, dict) else data
        cities = ["{} ({})".format(d['name'], d['sys']['country'])
            for d in data['list']]
        print   cities
        self.search_results.item_strings = cities
        print "DONE"

class WeatherApp(App):
    pass


if __name__ == '__main__':
    WeatherApp().run()

And here is the console:

[INFO   ] [OSC         ] using <multiprocessing> for socket
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
api.openweathermap.org/data/2.5/forecast/daily?APPID=ef4f6b76310abad083b96a45a6f547be&q=London
<UrlRequest(Thread-1, started daemon 139654193755904)>
Result:  None

, URL-, , "found_location" , python request.results = None

?

, , , . .

+4
1

, .

"http://" infront .

, URL . UrlRequest

UrlRequest . , XHR Javascript.

on_success UrlRequest

.

from kivy.app import App
#kivy.require("1.9.1")
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest

class MyWidget(BoxLayout):
    def __init__(self,**kwargs):
        super(MyWidget,self).__init__(**kwargs)
        search_url = "http://api.openweathermap.org/data/2.5/forecast/daily?APPID=ef4f6b76310abad083b96a45a6f547be&q=new%20york"
        print search_url
        self.request = UrlRequest(search_url, self.res)
        print self.request
        print "Result: before success", self.request.result,"\n"


    def res(self,*args):
        print "Result: after success", self.request.result


class MyApp(App):
    def build(self):
        return MyWidget()


if __name__ == '__main__':
    MyApp().run()
+1

Source: https://habr.com/ru/post/1648449/


All Articles