简体   繁体   中英

Data in Flask web app not updating [Python]

I have a flask app that fills a html table with data that it gets from a Pickle file in the static folder. I have an external application that updates that Pickle file once a minute.

The views.py file opens up that Pickle file and loads the data, which is then sent to an html template.

I got the app running, although no matter how many times I refresh the page, the data is the same as when I first started the app in my terminal.

I am starting it via uwsgi --socket 127.0.0.1:3031 --wsgi-file run_web.py --callable app --processes 4 --threads 2 --stats 127.0.0.1:9191 in a screen instance.

What did I forget to do to make my data refresh properly?

I am using uswgi and nginx to host a Flask app

views.py ...

import all the things    

with open('/home/metheuser/webapps/ers_portal/app/static/' + ec.dd_filename) as f:
     data = pickle.load(f)

column_names = data.pop('__column_names__')
column_names.insert(2, 'Screen Viewer')
column_names.insert(3, 'Site Page')
sites = data.keys()

for sitename in data:
    data[sitename].insert(2, ec.unit_roster[sitename]['ip_address'])
    data[sitename].insert(3, 'doesn\'tgoanywhereyet...')

dbt = da.Database_Authenticator(admin=False)

#************************************************
lm = LoginManager()
lm.init_app(app)
lm.login_view = 'login'

def authenticate(un, typed_pw, remember=False):
    """login with database"""
    my_user = dbt.authenticate_user(un, typed_pw)
    if my_user: #  user is authenticated
        return my_user
    else:
        return False

@lm.user_loader
def load_user(userid):
    print 'user id:',userid
    try:
        return dbt.get_user_by_id(userid)
    except AttributeError:
        return None

@app.before_request
def before_request():
    g.user = current_user

@app.route('/')
@app.route('/index')
def index():
    return render_template("index.html",
                           title = 'Main',)

@app.route('/login', methods = ['GET', 'POST'])
def login():
    if g.user is not None:
        if g.user.is_authenticated():
            return redirect(request.args.get('next') or 'index')
    form = LoginForm()
    if form.validate_on_submit():
        authed_user = authenticate(form.username.data, form.typed_pw.data, form.remember_me.data)
        if authed_user:
            authed_user.authenticated = True
            u = login_user(authed_user, form.remember_me.data)
        return redirect(request.args.get('next') or 'index')
    return render_template('login.html',
        title = 'Sign In',
        form = form)

@app.route('/logout')
def logout():
    logout_user()
    return redirect('index')

@app.route('/my_table')
@login_required
def my_table():
    print 'trying to access data table...'
    return render_template("my_table.html",
                           title = "Rundata Viewer",
                           sts = sites,
                           cn = column_names,
                           data = data) #  dictionary of data

First of all, did a request reach your flask-server? If yes, you should show us your views.py, if no, try to force refresh by adding some querystring: http://your/url.html?123 .

Edit: Inside your view.py you only load the data once, when the server is started. You have to update the data every time the file changes:

data_modification_time = None
data = None
def reload_data():
    global data_modification_time, data
    filename = '/home/metheuser/webapps/ers_portal/app/static/' + ec.dd_filename
    mtime = os.stat(filename).st_mtime
    if data_modification_time != mtime:
        data_modification_time = mtime
        with open(filename) as f:
            data = pickle.load(f)
    return data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM