android - How to save information in json file without deleting previous information python -
i making app using python , kivy allows user make new entry glucose readings. right saves json file new entry deletes previous data. how can make each entry saves separately can access information user's history?
.py file
from kivy.app import app kivy.lang import builder kivy.uix.popup import popup kivy.uix.button import button kivy.graphics import color, rectangle kivy.uix.boxlayout import boxlayout kivy.uix.floatlayout import floatlayout kivy.uix.image import asyncimage kivy.uix.label import label kivy.properties import stringproperty, listproperty kivy.uix.behaviors import buttonbehavior kivy.uix.textinput import textinput kivy.network.urlrequest import urlrequest kivy.storage.jsonstore import jsonstore os.path import join os.path import exists kivy.compat import iteritems kivy.storage import abstractstore json import loads, dump kivy.config import config import os import errno class phone(floatlayout): def __init__(self, **kwargs): # make sure aren't overriding important functionality super(phone, self).__init__(**kwargs) self.canvas.before: color(0, 1, 0, 1) # green; colors range 0-1 instead of 0-255 self.rect = rectangle(size=self.size, pos=self.pos) self.bind(size=self._update_rect, pos=self._update_rect) if not os.path.exists('hello.json'): open('hello.json', 'wt') infile: infile.write("") else: open('hello.json') infile: try: data = phone.load(self) except keyerror: data = [] def _update_rect(self, instance, value): self.rect.pos = instance.pos self.rect.size = instance.size def product(self, instance): self.result.text = str(float(self.w.text) * 703/ (float(self.h.text) * float(self.h.text))) def save(self): store = jsonstore('hello.json') name = self.n.text gender = self.g.text dtype = self.t.text height = self.h.text weight = self.w.text bmi = self.result.text medications = self.m.text insulin = self.ti.text store.put('profile', name=name, gender=gender, dtype=dtype, height=height, weight=weight, bmi=bmi, medications=medications, insulin=insulin) def save_entry(self): time = self.gt.text glucose = self.gr.text carbs = self.c.text medications_taken = self.mt.text store.put('entry', time=time, glucose=glucose, carbs=carbs, medications_taken=medications_taken) def load(self): store = jsonstore('hello.json') profile = store.get('profile') self.n.text = profile['name'] self.g.text = profile['gender'] self.t.text = profile['dtype'] self.h.text = profile['height'] self.w.text = profile['weight'] self.result.text = profile['bmi'] self.m.text = profile['medications'] self.ti.text = profile['insulin'] presentation = builder.load_file("main.kv") class phoneapp(app): def build(self): store = jsonstore('hello.json') return phone() if __name__ == '__main__': phoneapp().run()
use small json database this keep clean. example usage:
from tinydb import tinydb, query db = tinydb('./db.json') user = query() db.insert({'name': 'john', 'age': 22}) result = db.search(user.name == 'john') print result
Comments
Post a Comment