Displaying data from JSON file using Kivy

492 views
Skip to first unread message

epou...@gmail.com

unread,
Jan 17, 2019, 9:49:11 AM1/17/19
to Kivy users support
I have a program that creates a json file based on user input. I'm trying to display the "user" information so the users know they already created a profile (this will be displayed under "Load profile"). I've tried using recycleview, listview, and even tried creating my own table to make this work. I've looked at the documentation for recycleview (very confusing) and listview (also pretty confusing). I don't have enough experience to figure out the technicalities. I've searched through a series of stackflow questions and youtube tutorials but none of them really explained how to use data from a json file. Can somebody help me figure this out?

main.py
from kivy.app import App from kivy.storage.jsonstore import JsonStore from kivy.lang import Builder from kivy.uix.screenmanager import Screen, ScreenManager from kivy.core.window import Window from tinydb import TinyDB, Query, where from kivy.properties import ListProperty Window.clearcolor = (0.02745098, 0.074509804, 0.121568627, 1) Window.size = (2000, 900) db = TinyDB('bcodb.json') class TitleScreen(Screen): pass class MainScreen(Screen): pass class CreateProfile(Screen): def __init__(self, **kwargs): super(CreateProfile, self).__init__(**kwargs) self.store = JsonStore("bcodb.json") def save(self): db.insert({'first': self.first.text, 'middle': self.middle.text, 'last': self.last.text}) def update(self): db.update({'first': self.first.text, 'middle': self.middle.text, 'last': self.last.text}) class LoadProfile(Screen): rows = ListProperty([("First", "Middle", "Last")]) def __init__(self, **kwargs): super(LoadProfile, self).__init__(**kwargs) self.store = JsonStore("bcodb.json") def load(self): if db.search(where('last') == self.last.text): print(self.rows) else: print("not found") def delete(self): db.remove({'first': self.first.text, 'middle': self.middle.text, 'last': self.last.text}) class CreatePacket(Screen): pass class ScreenManagement(ScreenManager): pass presentation = Builder.load_file("customwidget2.kv") class CustomWidgetApp(App): def build(self): return presentation if __name__ == "__main__": CustomWidgetApp().run()

.kv
#: import FadeTransition kivy.uix.screenmanager.FadeTransition #: import hex kivy.utils.get_color_from_hex #: import FocusBehaviors kivy.uix.behaviors.focus ScreenManagement: transition: FadeTransition() TitleScreen: MainScreen: CreateProfile: LoadProfile: CreatePacket: <MainLabel@Label>: font_size:50 bold: True size_hint_x: 1 size_hint_y: 1.85 color: 0.396078431, 0.803921569, 0.807843137, 1 font_name: './pirulen rg.ttf' <SubLabel@Label>: font_size: 35 bold: True halign: "center" size_hint_x: 1 size_hint_y: 1.5 color: 0.396078431, 0.803921569, 0.807843137, 1 font_name: './pirulen rg.ttf' <OtherLabel@Label>: font_size: 12 bold: True color: 0.396078431, 0.803921569, 0.807843137, 1 font_name: './pirulen rg.ttf' text_size: self.size <Button@Button>: font_size: 20 bold: True color: 0.396078431, 0.803921569, 0.807843137, 1 font_name: './pirulen rg.ttf' background_color: 0.02745098, 0.074509804, 0.121568627, .01 canvas.before: Color: rgba: 0.396078431, 0.803921569, 0.807843137, 1 Line: width: 2 rectangle: self.x, self.y, self.width, self.height on_press: self.background_color = (0.396078431, 0.803921569, 0.807843137, 1) on_release: self.background_color = (0.02745098, 0.074509804, 0.121568627, .01) # TODO: Create a focus behavior to "Tab" between widgets <TextInput@TextInput>: font_size: 12 color: 0.396078431, 0.803921569, 0.807843137, 1 font_name: './pirulen rg.ttf' padding_x: 10 padding_y: 10 focus_next: None focus_previous: None unfocus_on_touch: True background_color: 0.02745098, 0.074509804, 0.121568627, .01 canvas.before: Color: rgba: 0.396078431, 0.803921569, 0.807843137, 1 Line: width: 1 rectangle: self.x, self.y, self.width, self.height <TitleScreen>: id: "title" FloatLayout: MainLabel: text: "Easy Button" size_hint_x: 1 size_hint_y: 1.25 SubLabel: text: 'Test' size_hint_x: 1 size_hint_y: 1 Button: text: "Click Here To Continue" on_release: app.root.current = "main_screen" size_hint: (.75, .15) pos_hint: {'x': .12, 'y': .2} <MainScreen>: name: "main_screen" MainLabel: text: "Home Page" FloatLayout: Button: on_release: app.root.current = "create_profile" text: "Create Profile" size_hint: (.5, .15) pos_hint: {'x': .23, 'y': .65} Button: on_release: app.root.current = "load_profile" text: "Load Profile" size_hint: (.5, .15) pos_hint: {'x': .23, 'y': .45} Button: on_release: app.root.current = "create_packet" text: "Create Packet" size_hint: (.5, .15) pos_hint: {'x': .23, 'y': .25} <CreateProfile>: name: "create_profile" first: first middle: middle last: last AnchorLayout: anchor_x: 'center' anchor_y: 'top' MainLabel: text: "Create Profile" size_hint: (1, .15) BoxLayout: size_hint: (.95, .2) pos_hint: {'x': 0, 'y': .85} spacing: 10 padding: 10 halign: "left" OtherLabel: text: "First" OtherLabel: text: "Middle" OtherLabel: text: "Last" BoxLayout: size_hint: (.95, .07) pos_hint: {'x': 0, 'y': .8} spacing: 20 padding: 10 halign: "right" text_size: self.size TextInput: id: first TextInput: id: middle TextInput: id: last BoxLayout: Button: on_release: app.root.current = "main_screen" text: "back Home" size_hint: (.5, .15) Button: on_release: root.save() text: "Save Profile" size_hint: (.5, .15) <LoadProfile>: name: "load_profile" first: first middle: middle last: last AnchorLayout: anchor_x: 'center' anchor_y: 'top' MainLabel: text: "Load Profile" size_hint: (1, .15) BoxLayout: size_hint: (.95, .2) pos_hint: {'x': 0, 'y': .85} spacing: 10 padding: 10 halign: "left" OtherLabel: text: "First" OtherLabel: text: "Middle" OtherLabel: text: "Last" BoxLayout: size_hint: (.95, .07) pos_hint: {'x': 0, 'y': .8} spacing: 20 padding: 10 halign: "right" text_size: self.size TextInput: id: first TextInput: id: middle TextInput: id: last BoxLayout: size_hint: (.95, .2) pos_hint: {'x': .4, 'y': .75} spacing: 10 padding: 10 halign: "left" GridLayout: cols: 3 size_hint_x: None pos_hint_y: .75 OtherLabel: text: "Created Profiles" BoxLayout: Button: on_release: app.root.current = "main_screen" text: "back Home" size_hint: (.5, .15) Button: on_release: root.load() text: "Load Profile" size_hint: (.5, .15) <CreatePacket>: name: "create_packet" MainLabel: text: "Select Packet" FloatLayout: Button: on_release: app.root.current = "main_screen" text: "back Home" size_hint: (1, .15)

ZenCODE

unread,
Jan 17, 2019, 12:19:16 PM1/17/19
to Kivy users support
You can just use json.loads to get it into a python dictionary,


Unfortunately, the code you posted in not formatted in a runnable way. And how to use your python dictionary of the data depends on what you want to do with it, so perhaps try being more specific.

epou...@gmail.com

unread,
Jan 17, 2019, 2:08:52 PM1/17/19
to Kivy users support
Thank you for the response.  What I'm trying to do is create a system where I can store common bio data for an employee.  Eventually the plan is to use that information to automatically fill in PDF forms.  It'll save a lot of time inputing the same information for multiple forms. 

I'm at the step where I am able to store information into the JSON file.  Now I need to be able to recall it and have it display in a recycleview or listview so we know the employee's information is already in the system.  That's the part I need help with.  All the documentation and tutorials I find don't necessarily explain how to extract information from a file to display in a gui.  It'll need to be selectable in order to load the profile data to be inputted into a PDF form.

Here is a link to stackoverflow where I asked the same question.  The code should be runnable from there: https://stackoverflow.com/questions/54222711/displaying-data-from-json-file-using-kivy

ZenCODE

unread,
Jan 18, 2019, 2:08:26 AM1/18/19
to Kivy users support
It still needs a json file to run.

But here is a recycleview example.


Around line 92, you're just going to build your data using the dictionary you have built from json (not just using the range function).
Reply all
Reply to author
Forward
0 new messages