Python Bottle Cart Response Cycle

68 Views Asked by At

Python Bottle framework form tutorials always dead end by spitting out raw text of data obtained from a POST method. But that’s not how web sites work. I want to cycle right back to the same HTML/URL page to interactively display the number of items just added to a cart then let the user add more items as needed. I tried global variables, a dictionary, or Bottle environmental variables to keep track of my cart item count between page reloads, and all work fine on a localhost server but act sporadically when placed on Namecheap. I redirect back to the original URL either by an HTML meta refresh, by the Bottle redirect command, or simply to again call the Python function Bottle routes to that URL. For all of these variations Namecheap with a cPanel Python 3.7 installation the cart item count display jumps around up and down as I try to add more items in an interactive cycle. At least one of the versions does keep a running tally but most results jump back down to a small number or just 1 again. The code below is my passenger_wsgi.py file.

I just changed from Bottle to Flask with essentially the same code but in Flask flavor and it’s doing the exact same thing from cPanel-based Python on Namecheap.

Are multiple threads being used that can’t contain the same global or even system environmental variable values over time?

import bottle
from bottle import route, request, post, debug

cart_count = 0

@route('/ItemA')
def add():
  global cart_count
  debug(True)
  return f'''
<html>
<head>
<title>Velkommen</title>
</head>
<body>
<center>
<p>{cart_count} ITEMS</p>
<form action="/ItemA" method="post">
<input name="item_count" type="text" size="1" value="1"/>
$120
<input type="submit">
</form>
</center>
</body>
</html>
'''

@post('/ItemA')
def do_add():
  global cart_count
  debug(True)
  item_count = int(request.forms.get('item_count'))
  cart_count += item_count
  return add()

# passenger hook
def application(environ, start_response):
  return bottle.app().wsgi(environ,start_response)

UPDATE: the Flask debugger won’t load any page on iPhone browsers so I returned to Bottle and got it to work via cookies as long as my returning to the same item addition page was done not by merely calling the internal Python function that creates that page but by using a Bottle redirect for otherwise the cookies lagged one user step behind meaning it lost the first step altogether.

Screenshot


import bottle
from bottle import route, run, static_file, template, request, get, post, debug, error, response, redirect
bugs = True

@route('/ItemA')
def addA():
    debug(bugs)
    if not request.cookies.a_count:
        a_count = 0
        response.set_cookie('a_count', str(a_count))
    else:
        a_count = request.cookies.get('a_count')
    return f'''
<p style="color:#047CFC;">USA ONLY •  {str(a_count)} ITEMS $325
<a href="http://macpebius.com/cart"><button class='button'>VIEW CART</button></a>
FREE SHIPPING
<img src = "IMG_4248.JPG" alt = "ALT" height = "500" width = "500"/>
<form action='/ItemA' method="post">
<input name="item_count" type="text" size="1" value="1"/>
$120
<input class='button' value="ADD TO CART" type="submit"/>
'''

@post('/ItemA')
def do_add():
    debug(bugs)
    a_count = int(request.cookies.get('a_count'))
    a_count += int(request.forms.get('item_count'))
    response.set_cookie('a_count', str(a_count))
    return redirect('/ItemA')

# passenger hook
def application(environ, start_response):
    return bottle.app().wsgi(environ,start_response)

0

There are 0 best solutions below