javascript - Using Flask Session with new EventSource -
sorry poorly worded title.
my goal set flask page using ajax can add dynamic content page python program running.
i using flask_oauthlib access token rest api, , querying rest api paginated content.
i want show "progress" repeat rest call different pages of content api. rest api access token stored in user's session variable.
i have followed these steps basic dynamic page , running. however, when start modifying "progress" function, add access session variable, starts fail.
i error: runtimeerror: working outside of request context.
tells me not in right session context.
my guess because endpoint being called javascript using new eventsource
... how able access session context in way?
am going down right path, or there better option available?
here various code blocks have:
<!doctype html> <html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> <script> var source = new eventsource("/progress"); source.onmessage = function(event) { $('.text').text(event.data); } </script> </head> <body> <p class="text"></p> </body> </html>
@app.route('/sentmail') def sentmail(): return render_template('progress.html') @app.route('/progress') def progress(): def generate(): #code using trying use session here return response(generate(), mimetype= 'text/event-stream')
once web server started processing response can't access request context thankfully flask comes stream decorator @stream_with_context
can use access request context in streaming...
modify code below...
from flask import stream_with_context @app.route('/progress') def progress(): @stream_with_context def generate(): ... remaining code ...
Comments
Post a Comment