2

I am trying to set a global variable as in:

    # -*- coding: utf8 -*-
from flask import Flask, render_template,redirect,flash, url_for, session, request
from sqlalchemy.sql import text
from sqlalchemy.orm import aliased, Query
from sqlalchemy import exc
from flask_bootstrap import Bootstrap
from .forms import *


import zlib
#from sqlalchemy.orm.query import limit, order_by
from dbmodel import *


app = Flask(__name__)
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
Bootstrap(app)

MAX_ITEMS=50
MAX_ZIPS=5
MAX_ORDERS=20


clientMaxSQL="SELECT * FROM CLIENT WHERE 1"

engine, dbsession = dbconnect() 

@app.route("/clientRegister",methods=['GET', 'POST'])
def clientRegister():
        form = ClientQuery()

        global clientMaxSQL
        flash('maxSql at start %s ' %(clientMaxSQL))
        if form.reset.data == "True":
                flash('RESET TABLE FILTERS')
                clientMaxSQL="SELECT * FROM CLIENT WHERE 1"

        sql = clientMaxSQL
        if form.validate_on_submit():
                if len(form.compareVal.data.strip()) > 0 and ( any(c.isalpha() for c in form.compareVal.data) or any(c.isdigit() for c in form.compareVal.data)):
                        sql = sql + " AND " + form.columnSelection.data + form.comparisonSelection.data + "'" +form.compareVal.data+"'"

        start=int(request.args.get('start', '0'))
        prev=start-MAX_ITEMS
        nextStart = start + MAX_ITEMS
        tmpSQL = clientMaxSQL
        clientMaxSQL = sql
        sql = sql + " LIMIT " + str(start)+","+str(nextStart)
        try:
                clients = engine.execute(sql)
                maxStart=engine.execute(clientMaxSQL).rowcount
                flash('Attempting Query: %s' %(sql))
        except:
                flash('Invalid Comparison of %s' %(form.compareVal.data))
                sql = tmpSQL + " LIMIT " + str(start)+","+str(nextStart)
                clientMaxSQL=tmpSQL
                clients = engine.execute(sql)
                maxStart=engine.execute(clientMaxSQL)
        flash('maxSql at end %s ' %(clientMaxSQL))
        return render_template("clientRegister.html",form=form,maxStart=maxStart,clients=clients,start=start, prev=prev,nextStart=nextStart)

I am getting some strange results. It seems as if this statement (declared above all of my function definitions) is executing every once in a while on its own. Is this possible? It is imperative to my web app that this only runs once otherwise I will lose the current state of my query.

Added all the code in the program that ever changes the variable. It is resetting on its own though.

6
  • Doesn't this just set a string to a variable?
    – seequ
    Commented Dec 19, 2014 at 6:44
  • show your full code. this is only a string it can not execute on it's own. Commented Dec 19, 2014 at 6:45
  • "It is imperative to my web app that this only runs once otherwise I will lose the current state of my query." ...no idea what you're trying to achieve but this sounds like a bad idea
    – Anentropic
    Commented Dec 19, 2014 at 6:49
  • @salmanwahed I posted all the code.The idea is that I am trying to maintain state of the query until the user resets it
    – Brandon
    Commented Dec 19, 2014 at 7:10
  • 2
    And what happens when you get more than one user on your site? Commented Dec 19, 2014 at 7:33

1 Answer 1

2

use the, @app.before_first_request decorator with the function to make it run only once

As in:

@app.before_first_request
def function_to_run_only_once():
     #your statement(s)

This should make sure that your said statements execute only once in the entire lifetime of your program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.