False KeyError

I have my file written as below

global context

colours = ["red", "orange","yellow","green","blue", "indigo","violet"]

context = {

    "current_trial" : trialsNum,
    "city": city,
    "cityData" : cityData,
    "day_1" : dates[0],
    "day_2" : dates[1],
    "day_3" : dates[2],
    "day_4" : dates[3],
    "day_5" : dates[4],
    "day_6" : dates[5],
    "day_7" : dates[6],
    "high_1" : highs[0],
    "high_2" : highs[1],
    "high_3" : highs[2],
    "high_4" : highs[3],
    "high_5" : highs[4],
    "high_6" : highs[5],
    "high_7" : highs[6],
    "low_1" : lows[0],
    "low_2" : lows[1],
    "low_3" : lows[2],
    "low_4" : lows[3],
    "low_5" : lows[4],
    "low_6" : lows[5],
    "low_7" : lows[6],
    "trial_num" : (trialsNum/52*100),
    "trials_passed" : trialsPassed,
    "trials_left" : trialsLeft,
    "week" : rowNum,
    "row" : [],
    "current_trial" : 0


return template('trials.html', **context)

@route('/trials', method=['POST'])
def do_trials():

global context

if (context['current_trial'] == 52):
    #do something

    colours = ["red", "orange","yellow","green","blue", "indigo","violet"]


    context["colour"] = colours[context['current_trial']/8]
    context["page_name"] = "Trials"
    context["day_1"] = dates[0]
    context["day_2"] = dates[1]
    context["day_3"] = dates[2]
    context["day_4"] = dates[3]
    context["day_5"] = dates[4]
    context["day_6"] = dates[5]
    context["day_7"] = dates[6]
    context["high_1"] = highs[0]
    context["high_2"] = highs[1]
    context["high_3"] = highs[2]
    context["high_4"] = highs[3]
    context["high_5"] = highs[4]
    context["high_6"] = highs[5]
    context["high_7"] = highs[6]
    context["low_1"] = lows[0]
    context["low_2"] = lows[1]
    context["low_3"] = lows[2]
    context["low_4"] = lows[3]
    context["low_5"] = lows[4]
    context["low_6"] = lows[5]
    context["low_7"] = lows[6]
    context["trial_num"] = round(context['current_trial']/52.0*100)
    context["row"] = context['row'] + [rowNum,normal,confidence]
    context['current_trial'] += 1

return template('trials.html', **context)

But almost every time I run my app eventually (not always from the start) I get a key error

2017-08-17 19:22:28,933:   File "/home/StephenBroomell/YourDecisions/", line 107, in do_trials
2017-08-17 19:12:46,634: Traceback (most recent call last):
2017-08-17 19:12:46,634:   File "/usr/local/lib/python2.7/dist-packages/", line 862, in _handle
2017-08-17 19:12:46,634:     return**args)
2017-08-17 19:12:46,634:   File "/usr/local/lib/python2.7/dist-packages/", line 1732, in wrapper
2017-08-17 19:12:46,635:     rv = callback(*a, **ka)
2017-08-17 19:12:46,635:   File "/home/StephenBroomell/YourDecisions/", line 107, in do_trials
2017-08-17 19:12:46,635:     if (context['current_trial'] == 52):

On the occasion that it doesn't bring up the error, it seems to reset the value of context['current_trial'] in some way. I know this because as the number goes up the colour of the page changes. It starts with red and sometimes when i'm on a page in green or blue it switches to red.

Can anyone help figure the issue?

NB: Places with comments like "stuff" have actual code but not relevant to this problem

Are you assuming that your code always runs in one process? Websites can be handled by multiple processes; each request is sent to one depending on which process is least busy. So if your code assumes that successive requests are handled by the same process, you'll get unpredictable behaviour -- sometimes they will be handled by the same process, but sometimes they'll be handled by different ones.

So how do I write my code so it accounts for multiple processing?

The normal technique would be to use a database to store any changeable data that your code depends on. We have a tutorial on writing a simple database-backed website using Flask -- doing it in Bottle would be similar.