0

I learn Kivy. I have Django app with CRUD function (Books Library) and API for this (Tastypie).

How can look "algorithm" building applications with a list of all my book? What component to use to list and how to retrieve data from the API and to display them?

mysite.com/api/books/?format=json

json:

{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 8}, "objects": [{"title": "Kivy book", "description": "Cool book", "id": 1, "page_count": 155}]}

Can anyone provide the code for this simple example?

madax
  • 39
  • 1
  • 9
  • Can you be more specific? What is exactly the problem you are experiencing? What have you tried? Here is an [example](http://stackoverflow.com/questions/13921910/python-urllib2-receive-json-response-from-url) of how to transform a JSON (from an URL) to a Python dict. As for components of Kivy you might want to take a look on [ListView](http://kivy.org/docs/api-kivy.uix.listview.html). – toto_tico Sep 23 '13 at 01:06

1 Answers1

1

Here is an example from my understanding you are trying to achieve. It is based on the simplest ListView example. Please notice that I created an extended JSON version from the example you provided. Also, when you want to use the url, you have to substitute the 2 commented lines. The method for loading the json is load (for io input) and not loads (for string input).

from kivy.uix.listview import ListView
from kivy.uix.gridlayout import GridLayout
import json
import urllib2

class MainView(GridLayout):

    def __init__(self, **kwargs):
        kwargs['cols'] = 2
        super(MainView, self).__init__(**kwargs)


        the_string_json = '{"meta": {"previous": null, "total_count": 8, "offset": 0, "limit": 20, "next": null}, "objects": [{"id": 1, "page_count": 155, "description": "Cool book", "title": "Kivy book 1"}, {"id": 1, "page_count": 155, "description": "Cool book", "title": "Kivy book 2"}, {"id": 1, "page_count": 155, "description": "Cool book", "title": "Kivy book 3"}]}'
        the_dict = json.loads(the_string_json)

        # Substitute the previous two lines for this ones:
        # the_io_json = urllib2.urlopen('mysite.com/api/books/?format=json')
        # the_dict = json.load(the_io_json)

        list_view = ListView(
            item_strings=[book['title'] for book in the_dict['objects']])

        self.add_widget(list_view)


if __name__ == '__main__':
    from kivy.base import runTouchApp
    runTouchApp(MainView(width=800))
toto_tico
  • 17,977
  • 9
  • 97
  • 116