We can simplify this as the following...

    if session.get('new_keypair_name')...

Then we can simply delete the session variable rather than setting it to an empty string in the cleanup (lines 102-103)

    del session['new_keypair_name']
    del session['material']