yemi.JUMP
yemi.JUMP

Reputation: 323

What is causing my json file to reset after adding a kivy widget?

My kivy application uses buttons, and these buttons correspond with json elements in a separate json file.

This would be button Eight

"Eight": {
        "action": "Eight",
        "delay": 1.55975283E9,
        "seconds": 0,
        "score": 0,
        "delta": 1.55974682E9,
        "grace_sec": 6000
    }

When I press the button the score will add a value of 1 every time. However, when I add another button the score is reset to 0.

I'm really not sure what it is because at one point my code didn't do this. I must have changed something without noticing. I believe that the problem may have to do with kivy's store function but I am not sure.

I will include all code that I believe may be affecting the json file.

class MainApp(App):
    def build(self):  # build() returns an instance
        self.store = JsonStore("streak.json")  # file that stores the streaks:
        Clock.schedule_interval(self.check_streak, 1/30.) # used to call functions dynamicaly
        Clock.schedule_interval(self.score_gone, 1/30.)
        Clock.schedule_interval(self.update_high, 1/30.)

        return presentation

This function is longer but I'm only including the part that adds a score of 1.

def check_streak(self, dt):
        for child in reversed(self.root.screen_two.ids.streak_zone.children):
            name = child.id

            with open("streak.json", "r") as read_file:
                data = json.load(read_file)

            for key in data.keys():
                if key == name:
                    delay = data.get(key, {}).get('delay')  # get value of nested key 'delay'
                    self.honey = data[key]['delta']
                    float(self.honey)
                        ...

                elif delay > time.time() > self.honey:  # on time (green)
                    child.background_normal = ''
                    child.background_color = [0, 1, 0, .95]
                    child.unbind(on_press=self.early_click)
                    child.bind(on_press=self.add_score)
                    child.bind(on_press=self.display_streak)
                    child.bind(on_press=self.draw_streak)
                    child.unbind(on_press=self.late_click)

# add 1 to score and store in json file
    def add_score(self, obj):
        name = obj.id

        with open("streak.json", "r") as file:
            read = json.load(file)

        for key in read.keys():
            if key == name:
                with open("streak.json", "r+") as f:
                    data = json.load(f)
                    data[key]['score']+=1

                    grace_sec = data.get(key, {}).get('grace_sec')
                    new_delay = time.time() + grace_sec
                    data[key]['delay'] = new_delay

                    seconds = data.get(key, {}).get('seconds')
                    new_delta = time.time() + seconds
                    data[key]['delta'] = new_delta

                    f.seek(0)
                    json.dump(data, f, indent=4)
                    f.truncate()

I'm including this because its an instance of the main app and is always being called on due to the Clock function but I don't think its the cause of the problem so you can ignore if thats the case.

# changes score to 0 and stores in json file
def score_gone(self, dt):
        for child in self.root.screen_two.ids.streak_zone.children:
            name = child.id
            color = child.background_color

            with open("streak.json", "r") as file:
                read = json.load(file)

            if color == [1, 0, 0, .95]: # red

                if read[name]['score'] != 0: #stops slow down from Clock
                    with open("streak.json", "r+") as f: # fix score not reseting to 0
                            data = json.load(f)
                            data[name]['score'] = 0
                            f.seek(0)
                            json.dump(data, f, indent=4)
                            f.truncate()

                elif read[name]['score'] == 0: #stops slow down from Clock
                        pass

This is the function that creates the streak from text input in a previous page.

    # creates the Streak object
    def create(self):
          ...
            # store streak attributes inside "streak.json"
            self.store.put(self.streak.action, action=self.streak.action,
                           delay=grace, seconds=total,
                           score=0, delta=self.count, grace_sec=grace_sec)

            self.change_screen(self) # changes to screen that displays buttons


This function displays the buttons.

# display the names of the streaks in a list on PageTwo
    def display_btn(self):
        no_data = "You have no stored streaks!"
        popup_2 = Popup(title="No Streaks!", content=Label(text=no_data),
                        size_hint=(None, None), size=(300, 100))

        with open("streak.json", "r") as read_file:
            data = json.load(read_file)

        for value in data.values():
            if value['delta'] is not None:
                print(f"action={value['action']}, delta={value['delta']}, grace={value['delay']}")
                    streak_button = StreakButton(id=(value['action']), text=(value['action'] + " " + "[" + str(value['score']) + "]"),
                                                 color=(0,0,0,1), size=(400, 50),
                                                 size_hint=(None, None))

                self.root.screen_two.ids.streak_zone.add_widget(streak_button)
...

All of the button widgets are deleted when leaving the page and display_btn is called when entering the page. What's weird to me is how the program remembers what the score value is before adding a new button even though the json file updates to the new score. If I were to add a score of 1 to the button Eight then close out of the program; The application would remember that the value was 1. So if I added a value of 2 and then added a new button the value of Eight's score would reset to 1 because that was its value before I closed the application.

EDIT

This is all of the code in create it is used to gather data from text input inside of a page. The math that your seeing is simply turning the input into seconds for a later function that compares the time (I'm not sure if this necessary)

def create(self):
        obj = self.root.get_screen('one')  # get info from ScreenOne
        self.streak = Streak(obj.ids.action_entry.text, obj.ids.delay_entry.text,
                             obj.ids.day_entry.text, obj.ids.hour_entry.text,
                             obj.ids.minute_entry.text)

        empty_error = "Make sure to fill out all boxes!"  # not in use yet

        popup = Popup(title="Not filled", content=Label(text=empty_error),
                      size_hint=(None, None), size=(300, 100))

        # error handling and calculating total seconds
        parsed = False
        try:
            total = ((int(self.streak.day) * 86400) + (int(self.streak.hour) * 3600) +
                     (int(self.streak.minute) * 60))  # convert into seconds

            self.current_time = time.time()
            self.count = self.current_time + total
            grace = (int(self.streak.delay) * 60) + self.count  # aka delay
            grace_sec = (int(self.streak.delay) * 60) + total

            parsed = True

            # delete later just used to test
            print("[seconds:", total, ']', "[action:", self.streak.action, ']',
                  "[grace:", grace, ']')

            # store streak attributes inside "streak.json"
            self.store.put(self.streak.action, action=self.streak.action,
                           delay=grace, seconds=total,
                           score=0, delta=self.count, grace_sec=grace_sec)

            self.change_screen(self)
        except ValueError as error:
            popup.open()

create uses an instance of Streak

class Streak():
    def __init__(self, action, delay, day, hour, minute, score=0, delta=0):
        self.action = action
        self.delay = delay
        self.day = day
        self.hour = hour
        self.minute = minute
        self.score = score
        self.delta = delta

Upvotes: 0

Views: 98

Answers (2)

ikolim
ikolim

Reputation: 16011

The JSON file being reset because self.store was populated in the build() method with the image before the add. Add self.store = JsonStore("streak.json") into create() method.

Snippets

def create(self):
    self.store = JsonStore("streak.json") 

Upvotes: 1

Dimo jORDANOV
Dimo jORDANOV

Reputation: 13

json resets when you add new info you need to add the info before with the new one that what i think its causing the problem

Upvotes: 0

Related Questions