Maynard Robert
Maynard Robert

Reputation: 47

update values with function call to Flask

I need to update realtime data with the following but index() and get_data() get called only once in the program.

How do I return values several times so that when I render template, it receives different values every time.

@app.route('/', methods=['GET'])
def index():
    value = get_data()
    print "index", value
    return render_template('index.html', session_value=value)


@app.route('/get_data', methods=['GET'])
def get_data():
    df = sqlio.read_sql(qry, conn)
    value = df['count'][0]
    print value
    return value

Upvotes: 0

Views: 1289

Answers (1)

robbrit
robbrit

Reputation: 17960

When you put @app.route as a decorator, it binds it as a route within the application. Calling it later will not have the effect you intend - it calls the decorator, not the function itself. I would change your code to something like this:

def get_data():
    df = sqlio.read_sql(qry, conn)
    value = df['count'][0]
    print value
    return value

@app.route('/', methods=['GET'])
def index():
    value = get_data()
    print "index", value
    return render_template('index.html', session_value=value)


@app.route('/get_data', methods=['GET'])
def get_data_route():
    value = get_data()
    # ... display your data somehow (HTML, JSON, etc.) ...

Upvotes: 1

Related Questions