-1

I noticed that changes in global variables are not reflected by my flask app. E.g. here a minimal app:

import flask

message = 'Hello World'
app = flask.Flask(import_name=__name__)

@app.route('/')
def print_message():
    return message

app.run()

message = 'Hello New World'

This results in 'Hello World' as return value even I set the variable "message" finally to 'Hello New World'.

Is it possible to reset the app or maybe more elegant, to reset a variable that is used by the app?

THX Lars

Lazloo Xp
  • 858
  • 1
  • 11
  • 36

1 Answers1

-1

I'm not quite sure if this is what you wanted. I change a variable by iterating through a global list.

import flask

global messages
messages = iter(['Hello World', 'Hello World 1', 'Hello World 2'])
app = flask.Flask(import_name=__name__)

@app.route('/')
def print_message():
    try:
        return next(messages)
    except Exception as e:
        return 'End :('


if __name__ == '__main__':
    app.run()
N. Arunoprayoch
  • 922
  • 12
  • 20
  • Unfortunately not, I would like that changes in variables are reflected in the running app – Lazloo Xp May 13 '20 at 08:29
  • Maybe it helps to understand the real-life application. Every day a data set is updated. I would like to update also the app that is based on this data set. – Lazloo Xp May 13 '20 at 08:45
  • @LazlooXp How large, roughly is the data you need to load in, and of what type? – v25 May 13 '20 at 09:03
  • @v25 Data is a network file. The associated graphml file has a size of roughly 200 MB. – Lazloo Xp May 13 '20 at 09:12
  • @LazlooXp Are you trying to store a reference to the file in the variable (Then have the view function load the file from a filesystem and return it for example) or are you storing this actual data in memory? – v25 May 13 '20 at 09:45
  • I storing the data in the actual memory – Lazloo Xp May 13 '20 at 09:55