Checking if global variables are defined in bottle
Question: Im trying to use bottle to update information on a site fed in from commands
in a chat bot but am struggling to get information from one route to another
while checking if the variables are defined.

It works fine until I add:

    
    
       if 'area' not in globals():
           area = ''
       if 'function' not in globals():
           function = ''
       if 'user' not in globals():
           user = ''
       if 'value' not in globals():
           value =''`
    

To check if the variable has been defined. It works unless I set a value using
/in. otherwise it errors with

    
    
    Traceback (most recent call last):
    File "/usr/local/lib/python3.5/dist-packages/bottle.py", line 862, in _handle
    return route.call(**args)
    File "/usr/local/lib/python3.5/dist-packages/bottle.py", line 1732, in wrapper
    rv = callback(*a, **ka)
    File "API.py", line 43, in botOut
    return area + function + user + value
    UnboundLocalError: local variable 'area' referenced before assignment
    

Full code:

    
    
    from bottle import route, error, post, get, run, static_file, abort, redirect, response, request, template
    Head = '''<!DOCTYPE html>
        <html lang="en">
        <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="style.css">
        <script src="script.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        </head>
        '''
    
    foot = '''</body></html>'''
    
    @route('/in')
    def botIn():
       global area
       global function
       global user
       global value
       area = request.query.area
       function = request.query.function
       user =  request.query.user
       value = request.query.value
       print(area)
       return "in"
    
    
    @route('/out')
    def botOut():
       if 'area' not in globals():
           area = ''
       if 'function' not in globals():
           function = ''
       if 'user' not in globals():
           user = ''
       if 'value' not in globals():
           value =''
    return area + function + user + value
    
    run (host='0.0.0.0', port=8080)
    


Answer: Instead of using 4 globals--which you then have to qualify with the `global`
keyword in several places--simply create one dict at the module level, and
store your state in that dict; no need to declare it `global` anywhere.

E.g.,

    
    
    bot_state = {
        'area': '',
        'function': '',
        'user': '',
        'value': ''
    }
    
    
    @route('/in')
    def botIn():
        bot_state['area'] = request.query.area
        bot_state['function'] = request.query.function
        bot_state['user'] =  request.query.user
        bot_state['value'] = request.query.value
        print(area)
        return 'in'
    
    
    @route('/out')
    def botOut():
        return ''.join(
            bot_state['area'],
            bot_state['function'],
            bot_state['user'],
            bot_state['value'],
        )
    

Note that there are several more improvements I'd make to the code (e.g. each
route function should return a list of strings, not a string), but those are
the minimal changes I'd make in order to solve your immediate problem. Hope it
helps!

