From patrick.rieser at outlook.com Sat Nov 5 18:23:27 2016 From: patrick.rieser at outlook.com (Patrick Rieser) Date: Sat, 5 Nov 2016 22:23:27 +0000 Subject: [Flask] transfer login status to another flask application Message-ID: Hey everybody, I wonder if there is a way to transfer the login status of a user to another flask application. Consider the a typical login process: a) User visits login.domain and puts in his credentials b) flask application at login.domain takes this credentials and sends the user and password to the internal api with the user and password c) The api hashes the password and compares it to the database and if valid an api token that simply consists of the username is generated and sent as a response (to increase the security of the internal api) d) login application verifies the login and a login cookie is generated (all with running flask-login) and then stores the token in the flask session to easily access the api e) login application redicets to another flask application (e.g. project_0) and tells this application that the user is already logged in and passes the api token (and the login status) So a) through d) are perfectly fine, but I don't really have any clue how to accomplish e), especially how the other application (project_0) can be made aware in a secure way that the user is already logged in, i.e. somehow copy the login cookie or generate a new one. Any help, clues or pointers are greatly appreciated! With kind regards, Patrick -------------- next part -------------- An HTML attachment was scrubbed... URL: From unai at sysbible.org Sat Nov 5 20:19:05 2016 From: unai at sysbible.org (Unai Rodriguez) Date: Sun, 06 Nov 2016 08:19:05 +0800 Subject: [Flask] transfer login status to another flask application In-Reply-To: References: Message-ID: <1478391545.1265255.778617713.102CEEA1@webmail.messagingengine.com> Hi Patrick, My understanding is that you're trying to implement something similar to CAS SSO using Flask. Perhaps their documentation could help you? https://apereo.github.io/cas/4.2.x/planning/Architecture.html In fact you might just be able to integrate your Flask apps with CAS without implementing it yourself... -- unai On Sun, Nov 6, 2016, at 06:23 AM, Patrick Rieser wrote: > > Hey everybody, > > I wonder if there is a way to transfer the login status of a user to > another flask application. Consider the a typical login process: > > a) User visits login.domain and puts in his credentials > b) flask application at login.domain takes this credentials and sends > the user and password to the internal api with the user and > password > c) The api hashes the password and compares it to the database and if > valid an api token that simply consists of the username is > generated and sent as a response (to increase the security of the > internal api) > d) login application verifies the login and a login cookie is > generated (all with running flask-login) and then stores the token > in the flask session to easily access the api > e) login application redicets to another flask application (e.g. > project_0) and tells this application that the user is already > logged in and passes the api token (and the login status) > > So a) through d) are perfectly fine, but I don't really have any clue > how to accomplish e), especially how the other application (project_0) > can be made aware in a secure way that the user is already logged in, > i.e. somehow copy the login cookie or generate a new one. Any help, > clues or pointers are greatly appreciated! > > > With kind regards, > Patrick > > _________________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask -------------- next part -------------- An HTML attachment was scrubbed... URL: From aresowj at gmail.com Mon Nov 7 16:48:13 2016 From: aresowj at gmail.com (Ares Ou) Date: Mon, 7 Nov 2016 13:48:13 -0800 Subject: [Flask] transfer login status to another flask application In-Reply-To: <1478391545.1265255.778617713.102CEEA1@webmail.messagingengine.com> References: <1478391545.1265255.778617713.102CEEA1@webmail.messagingengine.com> Message-ID: Hi, As Unai suggested, you should check the SSO document to see how they transfer the credentials between applications or even domains. One more thing to note is, you should never store the password in plain text which might be convenient for you to come up with a solution. Thanks. Best regards, Ares Ou *Software Engineer / Full-Stack Python Developer* *Blog:* http://aresou.net | *Github:* https://github.com/aresowj *Stack Overflow:* http://stackoverflow.com/users/5183727/ares-ou Ares Ou On Sat, Nov 5, 2016 at 5:19 PM, Unai Rodriguez wrote: > Hi Patrick, > > My understanding is that you're trying to implement something similar to > CAS SSO using Flask. Perhaps their documentation could help you? > > https://apereo.github.io/cas/4.2.x/planning/Architecture.html > > In fact you might just be able to integrate your Flask apps with CAS > without implementing it yourself... > > -- unai > > > On Sun, Nov 6, 2016, at 06:23 AM, Patrick Rieser wrote: > > > Hey everybody, > > I wonder if there is a way to transfer the login status of a user to > another flask application. Consider the a typical login process: > > a) User visits login.domain and puts in his credentials > b) flask application at login.domain takes this credentials and sends the > user and password to the internal api with the user and password > c) The api hashes the password and compares it to the database and if > valid an api token that simply consists of the username is generated and > sent as a response (to increase the security of the internal api) > d) login application verifies the login and a login cookie is generated > (all with running flask-login) and then stores the token in the flask > session to easily access the api > e) login application redicets to another flask application (e.g. > project_0) and tells this application that the user is already logged in > and passes the api token (and the login status) > > So a) through d) are perfectly fine, but I don't really have any clue how > to accomplish e), especially how the other application (project_0) can be > made aware in a secure way that the user is already logged in, i.e. somehow > copy the login cookie or generate a new one. Any help, clues or pointers > are greatly appreciated! > > > With kind regards, > Patrick > > > *_______________________________________________* > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > > > > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From russel at winder.org.uk Sat Nov 12 09:29:37 2016 From: russel at winder.org.uk (Russel Winder) Date: Sat, 12 Nov 2016 14:29:37 +0000 Subject: [Flask] Flask applications and migrations Message-ID: <1478960977.1799.67.camel@winder.org.uk> Hi, I suspect I am just failing to find the right pages on the Web? I have a Flask application as a package rather than a module. However all the documentation more or less assumes a module. In particular everything about the flask CLI assumes a module and, what is really important for me just now, the Flask-Migration system doesn't seem to work for package application as documented. I need to migrate a live database about two weeks ago, so any help would be gratefully accepted. -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder at ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel at winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: This is a digitally signed message part URL: From colin at techdata.net.au Tue Nov 15 01:33:34 2016 From: colin at techdata.net.au (Colin Haig) Date: Tue, 15 Nov 2016 14:33:34 +0800 Subject: [Flask] File Upload In-Reply-To: References: Message-ID: <00784859-CE83-48AB-8FBB-4A715BA2B6BE@techdata.net.au> To All I am trying to upload a file using flask ? but when I upload a large file I get a server error. I have tried the following : app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 When I reduce the above setting Flask reports a 413 error which is is supposed to do Updated apache server Have added LimitRequestBody 0 to my http.conf If I upload the file though my locahost flask server there are no issues. When I submit the file upload I can see a counter counting up to 100% but then it crashes with a server error ? I have checked the apache logs but can not see any error associated with the upload. I have searched the web looking for answers but to no avail. Any help greatly appreciated. Regards Colin -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at ddipp.net Tue Nov 15 01:48:36 2016 From: d at ddipp.net (Dmitry) Date: Tue, 15 Nov 2016 11:48:36 +0500 Subject: [Flask] File Upload In-Reply-To: <00784859-CE83-48AB-8FBB-4A715BA2B6BE@techdata.net.au> References: <00784859-CE83-48AB-8FBB-4A715BA2B6BE@techdata.net.au> Message-ID: <5df88da5-1720-c31e-2fe7-a06b06490a7e@ddipp.net> Check the write permissions on the folder in which you save the file. When downloading a file passes 100% and then there is an error, then perhaps it is not connected with the limitation on size. Or show code that allows to upload files to the server. 15.11.2016 11:33, Colin Haig ?????: > > > > To All > > I am trying to upload a file using flask ? but when I upload a large > file I get a server error. > > I have tried the following : > > app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 > When I reduce the above setting Flask reports a 413 error which is is > supposed to do > > > Updated apache server > > Have added LimitRequestBody 0 to my http.conf > > If I upload the file though my locahost flask server there are no issues. > > > When I submit the file upload I can see a counter counting up to 100% > but then it crashes with a server error ? I have > checked the apache logs but can not see any error associated with the > upload. > > I have searched the web looking for answers but to no avail. > > Any help greatly appreciated. > > Regards > > Colin > > > > > > > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask -- Dmitry Podkovyrkin email: d at ddipp.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From colin at techdata.net.au Tue Nov 15 02:34:13 2016 From: colin at techdata.net.au (Colin Haig) Date: Tue, 15 Nov 2016 15:34:13 +0800 Subject: [Flask] File Upload In-Reply-To: <5df88da5-1720-c31e-2fe7-a06b06490a7e@ddipp.net> References: <00784859-CE83-48AB-8FBB-4A715BA2B6BE@techdata.net.au> <5df88da5-1720-c31e-2fe7-a06b06490a7e@ddipp.net> Message-ID: <1FDCCA2C-BF44-43F2-BF1D-7D7850FF584E@techdata.net.au> Hi Dmitry I can upload smaller files ? it is when the file is above 130k that the problem occurs. def fnGetFileArea(cDestination): cFileList = '' if sys.platform == 'darwin': cDestination = "/Volumes/documents" session['platform'] = 'mac' elif sys.platform == 'win32': session['platform'] = 'win32' cDestination = "p:\\" return(cDestination) @app.route('/docget',methods=['GET','POST']) def docget(): cDocType = '' cDestination = '' aDocs = [] cFileList = [] srv = '' if request.method == 'POST': cDestination = fnGetFileArea(cDestination) cUser = request.args.get('user') cUserType = request.args.get('usertype') cDocType = request.form['doctype'] session['user'] = cUser if 'attach' in request.files: file = request.files['attach'] filename = file.filename if filename != '': filename = filename.replace(' ','_') if session['platform'] == 'mac': cFileSaveArea = cUserType + '/' + cDocType destination = '/'.join([cDestination+'/'+cFileSaveArea,filename]) cDestination = cDestination + '/' + cUserType elif session['platform'] == 'win32': cFileSaveArea = cUserType + '\\' + cDocType destination = '\\'.join([cDestination+cFileSaveArea,filename]) cDestination = cDestination + '\\' + cUserType try: cError = file.save(destination) except: flash('could not save') cFileList = os.listdir(cDestination) return render_template('selectdoc.html',user=cUser,usertype=cUserType,aDocs=cFileList) else: cDestination = fnGetFileArea(cDestination) if 'user' in request.args: cUser = request.args.get('user') session['user'] = cUser if 'usertype' in request.args: cUserType = request.args.get('usertype') if session['platform'] == 'mac': cDestination = cDestination + '/' + cUserType if session['platform'] == 'win32': cDestination = cDestination + '\\' + cUserType cFileList = os.listdir(cDestination) if cUser .strip() != '' and cUserType.strip() != '': return render_template('selectdoc.html',user=cUser,usertype=cUserType,aDocs=cFileList) Have tested on my windows apache server and small files are uploaded but large files fail. Regards Colin From: Flask on behalf of Dmitry Date: Tuesday, 15 November 2016 at 2:48 pm To: Subject: Re: [Flask] File Upload Check the write permissions on the folder in which you save the file. When downloading a file passes 100% and then there is an error, then perhaps it is not connected with the limitation on size. Or show code that allows to upload files to the server. 15.11.2016 11:33, Colin Haig ?????: To All I am trying to upload a file using flask ? but when I upload a large file I get a server error. I have tried the following : app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 When I reduce the above setting Flask reports a 413 error which is is supposed to do Updated apache server Have added LimitRequestBody 0 to my http.conf If I upload the file though my locahost flask server there are no issues. When I submit the file upload I can see a counter counting up to 100% but then it crashes with a server error ? I have checked the apache logs but can not see any error associated with the upload. I have searched the web looking for answers but to no avail. Any help greatly appreciated. Regards Colin _______________________________________________ Flask mailing list Flask at python.orghttps://mail.python.org/mailman/listinfo/flask -- Dmitry Podkovyrkin email: d at ddipp.net _______________________________________________ Flask mailing list Flask at python.org https://mail.python.org/mailman/listinfo/flask -------------- next part -------------- An HTML attachment was scrubbed... URL: From aresowj at gmail.com Wed Nov 16 14:02:22 2016 From: aresowj at gmail.com (Ares Ou) Date: Wed, 16 Nov 2016 11:02:22 -0800 Subject: [Flask] File Upload In-Reply-To: <1FDCCA2C-BF44-43F2-BF1D-7D7850FF584E@techdata.net.au> References: <00784859-CE83-48AB-8FBB-4A715BA2B6BE@techdata.net.au> <5df88da5-1720-c31e-2fe7-a06b06490a7e@ddipp.net> <1FDCCA2C-BF44-43F2-BF1D-7D7850FF584E@techdata.net.au> Message-ID: Hi, Can you suggest your upload code and the server error message? Best regards, Ares Ou *Software Engineer / Full-Stack Python Developer* *Blog:* http://aresou.net | *Github:* https://github.com/aresowj *Stack Overflow:* http://stackoverflow.com/users/5183727/ares-ou Ares Ou On Mon, Nov 14, 2016 at 11:34 PM, Colin Haig wrote: > Hi Dmitry > > I can upload smaller files ? it is when the file is above 130k that the > problem occurs. > > def fnGetFileArea(cDestination): > cFileList = '' > if sys.platform == 'darwin': > cDestination = "/Volumes/documents" > session['platform'] = 'mac' > elif sys.platform == 'win32': > session['platform'] = 'win32' > cDestination = "p:\\" > return(cDestination) > > @app.route('/docget',methods=['GET','POST']) > def docget(): > cDocType = '' > cDestination = '' > aDocs = [] > cFileList = [] > srv = '' > if request.method == 'POST': > cDestination = fnGetFileArea(cDestination) > cUser = request.args.get('user') > cUserType = request.args.get('usertype') > cDocType = request.form['doctype'] > session['user'] = cUser > if 'attach' in request.files: > file = request.files['attach'] > filename = file.filename > if filename != '': > filename = filename.replace(' ','_') > if session['platform'] == 'mac': > cFileSaveArea = cUserType + '/' + cDocType > destination = '/'.join([cDestination+'/'+ > cFileSaveArea,filename]) > cDestination = cDestination + '/' + cUserType > elif session['platform'] == 'win32': > cFileSaveArea = cUserType + '\\' + cDocType > destination = '\\'.join([cDestination+ > cFileSaveArea,filename]) > cDestination = cDestination + '\\' + cUserType > try: > cError = file.save(destination) > except: > flash('could not save') > cFileList = os.listdir(cDestination) > return render_template('selectdoc. > html',user=cUser,usertype=cUserType,aDocs=cFileList) > else: > cDestination = fnGetFileArea(cDestination) > if 'user' in request.args: > cUser = request.args.get('user') > session['user'] = cUser > if 'usertype' in request.args: > cUserType = request.args.get('usertype') > if session['platform'] == 'mac': > cDestination = cDestination + '/' + cUserType > if session['platform'] == 'win32': > cDestination = cDestination + '\\' + cUserType > cFileList = os.listdir(cDestination) > if cUser .strip() != '' and cUserType.strip() != '': > return render_template('selectdoc.html',user=cUser,usertype= > cUserType,aDocs=cFileList) > > Have tested on my windows apache server and small files are uploaded but > large files fail. > > Regards > > Colin > > > > From: Flask on behalf of > Dmitry > Date: Tuesday, 15 November 2016 at 2:48 pm > To: > Subject: Re: [Flask] File Upload > > Check the write permissions on the folder in which you save the file. > When downloading a file passes 100% and then there is an error, then > perhaps it is not connected with the limitation on size. > Or show code that allows to upload files to the server. > > 15.11.2016 11:33, Colin Haig ?????: > > > > > To All > > I am trying to upload a file using flask ? but when I upload a large file > I get a server error. > > I have tried the following : > > app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 > When I reduce the above setting Flask reports a 413 error which is is > supposed to do > > > Updated apache server > > Have added LimitRequestBody 0 to my http.conf > > If I upload the file though my locahost flask server there are no issues. > > > When I submit the file upload I can see a counter counting up to 100% but > then it crashes with a server error ? I have > checked the apache logs but can not see any error associated with the > upload. > > I have searched the web looking for answers but to no avail. > > Any help greatly appreciated. > > Regards > > Colin > > > > > > > _______________________________________________ > Flask mailing listFlask at python.orghttps://mail.python.org/mailman/listinfo/flask > > > -- > Dmitry Podkovyrkin > email: d at ddipp.net > > _______________________________________________ Flask mailing list > Flask at python.org https://mail.python.org/mailman/listinfo/flask > > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joatgm at gmail.com Thu Nov 17 15:01:09 2016 From: joatgm at gmail.com (John Overbaugh) Date: Thu, 17 Nov 2016 13:01:09 -0700 Subject: [Flask] Templatizing HTTP Response Headers Message-ID: In a Flask web app I'm building, I have a number of pages. Each page has essentially the same HTTP response headers, such as: r = make_response(render_template('response.html' **templateData)) r.headers.set('Content-Security-Policy', "default-src 'self'; script-src 'nonce-" + str(scriptNonce) + "' 'self'") r.headers.set('X-XSS-Protection', '1; mode=block') r.headers.set('X-Content-Type-Options', 'nosniff') r.headers.set('X-Frame-Options', 'deny') r.headers['Server'] = '' # Can't remove it, so set it to nothing return r Is there a way to templatize this, so it's the same for every page? John O. -------------- next part -------------- An HTML attachment was scrubbed... URL: From canhosgor at gmail.com Thu Nov 17 15:27:17 2016 From: canhosgor at gmail.com (Can Hosgor) Date: Thu, 17 Nov 2016 20:27:17 +0000 Subject: [Flask] Templatizing HTTP Response Headers In-Reply-To: References: Message-ID: You can use @app.after_request decorator to make flask call your function (in which you can modify the response as you like) at the end of every request. 17 Kas 2016 Per, saat 23:09 tarihinde John Overbaugh ?unu yazd?: > In a Flask web app I'm building, I have a number of pages. Each page has > essentially the same HTTP response headers, such as: > r = make_response(render_template('response.html' **templateData)) > r.headers.set('Content-Security-Policy', "default-src 'self'; > script-src 'nonce-" + str(scriptNonce) + "' 'self'") > r.headers.set('X-XSS-Protection', '1; mode=block') > r.headers.set('X-Content-Type-Options', 'nosniff') > r.headers.set('X-Frame-Options', 'deny') > r.headers['Server'] = '' # Can't remove it, so set it to nothing > return r > > Is there a way to templatize this, so it's the same for every page? > > John O. > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joatgm at gmail.com Fri Nov 18 15:32:20 2016 From: joatgm at gmail.com (John Overbaugh) Date: Fri, 18 Nov 2016 13:32:20 -0700 Subject: [Flask] Passing a JSON array via **templateData in render_template Message-ID: I created a 3-item JSON object (an array of items) and want to reference it via javascript in my main.html page. I also have other template data I wish to send (a nonce, a title, etc.). - When I exclude the JSON object, everything renders fine, but as soon as I add the JSON object to templateData, I get a TypeError that render_template() takes exactly one argument. - If I render_template with just the JSON object, the page renders OK (just missing the other data I want to have available like title and script_nonce) Am I just missing something that's totally obvious, or is it not possible to pass a JSON array along with other templateData? entries = [ { 'id': 1, 'session': 1392596278493910, 'time': '201611161227', 'from': 'Device123', "Message1": "Hello, my name is Billy", }, { 'id': 2, 'session': 1392596278493910, 'time': '201611161229', 'from': 'Tom1', 'Message': 'Hello Billy, my name is Tom Riddle', }, { 'id': 3, 'session': 1392596278493910, 'time': '201611161244', 'from': 'Device124', 'Message': 'Do you know Moaning Myrtle?', }, ] @app.route('/', methods=['GET']) def hello(): # Create a nonce for this page scriptNonce = random.getrandbits(64) templateData = { 'title' : 'Tom Riddle Diary', 'script_nonce' : scriptNonce, 'entry_Collection' : entries } r = make_response(render_template('main.html', **templateData)) return r -------------- next part -------------- An HTML attachment was scrubbed... URL: From hsysinfo at gmail.com Sat Nov 19 00:37:47 2016 From: hsysinfo at gmail.com (Chukwudi Nwachukwu) Date: Sat, 19 Nov 2016 06:37:47 +0100 Subject: [Flask] Flask Digest, Vol 17, Issue 6 In-Reply-To: References: Message-ID: Hello John, In an after request, do something like this: https://gist.github.com/nwaomachux/81dcc1450cf69a2f34c4d42730a1b4c3#file-after_request-py Then just *render_template('response.html' ** templateData) *at the normal place and that would be it. *iChux*? https://ng.linkedin.com/in/ichux http://stackoverflow.com/story/nwaomachux *Behind every no entry sign, there is a door.* On 18 November 2016 at 18:00, wrote: > Send Flask mailing list submissions to > flask at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/flask > or, via email, send a message with subject or body 'help' to > flask-request at python.org > > You can reach the person managing the list at > flask-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Flask digest..." > > Today's Topics: > > 1. Templatizing HTTP Response Headers (John Overbaugh) > 2. Re: Templatizing HTTP Response Headers (Can Hosgor) > > > ---------- Forwarded message ---------- > From: John Overbaugh > To: flask at python.org > Cc: > Date: Thu, 17 Nov 2016 13:01:09 -0700 > Subject: [Flask] Templatizing HTTP Response Headers > In a Flask web app I'm building, I have a number of pages. Each page has > essentially the same HTTP response headers, such as: > r = make_response(render_template('response.html' **templateData)) > r.headers.set('Content-Security-Policy', "default-src 'self'; > script-src 'nonce-" + str(scriptNonce) + "' 'self'") > r.headers.set('X-XSS-Protection', '1; mode=block') > r.headers.set('X-Content-Type-Options', 'nosniff') > r.headers.set('X-Frame-Options', 'deny') > r.headers['Server'] = '' # Can't remove it, so set it to nothing > return r > > Is there a way to templatize this, so it's the same for every page? > > John O. > > > ---------- Forwarded message ---------- > From: Can Hosgor > To: John Overbaugh , flask at python.org > Cc: > Date: Thu, 17 Nov 2016 20:27:17 +0000 > Subject: Re: [Flask] Templatizing HTTP Response Headers > You can use @app.after_request decorator to make flask call your function > (in which you can modify the response as you like) at the end of every > request. > > 17 Kas 2016 Per, saat 23:09 tarihinde John Overbaugh > ?unu yazd?: > >> In a Flask web app I'm building, I have a number of pages. Each page has >> essentially the same HTTP response headers, such as: >> r = make_response(render_template('response.html' **templateData)) >> r.headers.set('Content-Security-Policy', "default-src 'self'; >> script-src 'nonce-" + str(scriptNonce) + "' 'self'") >> r.headers.set('X-XSS-Protection', '1; mode=block') >> r.headers.set('X-Content-Type-Options', 'nosniff') >> r.headers.set('X-Frame-Options', 'deny') >> r.headers['Server'] = '' # Can't remove it, so set it to nothing >> return r >> >> Is there a way to templatize this, so it's the same for every page? >> >> John O. >> _______________________________________________ >> Flask mailing list >> Flask at python.org >> https://mail.python.org/mailman/listinfo/flask >> > > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ub at artfacts.net Sat Nov 19 07:16:27 2016 From: ub at artfacts.net (ub at artfacts.net) Date: Sat, 19 Nov 2016 13:16:27 +0100 Subject: [Flask] Passing a JSON array via **templateData in render_template In-Reply-To: References: Message-ID: <0ade3252-27f4-3e88-14c4-69c2b49f36bb@artfacts.net> hello john, entries is not really JSON, it is a dict, so it needs need to be stringified it before being passed it into templates. use json.dumps or flask.jsonify when embedding JSON in jinja2 templates, remember to use the "safe" filter, or you will get unexpected results. cheers, ub On 18.11.2016 21:32, John Overbaugh wrote: > I created a 3-item JSON object (an array of items) and want to reference > it via javascript in my main.html page. I also have other template data > I wish to send (a nonce, a title, etc.). > > * When I exclude the JSON object, everything renders fine, but as soon > as I add the JSON object to templateData, I get a TypeError that > render_template() takes exactly one argument. > * If I render_template with just the JSON object, the page renders OK > (just missing the other data I want to have available like title and > script_nonce) > > > Am I just missing something that's totally obvious, or is it not > possible to pass a JSON array along with other templateData? > > entries = [ > { > 'id': 1, > 'session': 1392596278493910, > 'time': '201611161227', > 'from': 'Device123', > "Message1": "Hello, my name is Billy", > }, > { > 'id': 2, > 'session': 1392596278493910, > 'time': '201611161229', > 'from': 'Tom1', > 'Message': 'Hello Billy, my name is Tom Riddle', > }, > { > 'id': 3, > 'session': 1392596278493910, > 'time': '201611161244', > 'from': 'Device124', > 'Message': 'Do you know Moaning Myrtle?', > }, > ] > > @app.route('/', methods=['GET']) > def hello(): > # Create a nonce for this page > scriptNonce = random.getrandbits(64) > > templateData = { > 'title' : 'Tom Riddle Diary', > 'script_nonce' : scriptNonce, > 'entry_Collection' : entries > } > > r = make_response(render_template('main.html', **templateData)) > return r > > > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > From andreas.dorfner at stud.hs-regensburg.de Sun Nov 20 11:23:22 2016 From: andreas.dorfner at stud.hs-regensburg.de (Andreas Dorfner) Date: Sun, 20 Nov 2016 17:23:22 +0100 Subject: [Flask] Store Data in a SQLite-Database with Flask Message-ID: Hello everyone, I'm new to python and flask web development. I have to realize a data logger using flask. By using another framework called 'pymodbus', I get some data from an energy meter and save that data to a variable called 'crv_data' (see file attached). This part is working so far! The second part of the project contains the storage of 'crv_data' to a SQLite-Database. For that, I'm using flask. For creating a table with five columns, a file 'schema.sql' (also attached) is included. If the file 'logger.py' is running, a database is produced with the desired layout and some data in 'crv_data' is available. But there is no data included in the database. I think there is an error in function 'add_entry()' (starting in line 72 of logger.py file), because if I call the function in the infinite loop (line 103), the program stops running. Maybe I have to say that I using Beaglebone Black with running Debian on it. Does anybody have an idea, why the program doesn't work? Many thanks, Andreas --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus -------------- next part -------------- #!/usr/bin/python #all the imports for pymodbus import Adafruit_BBIO.UART as UART import time, serial from pymodbus.client.sync import ModbusSerialClient as ModbusClient from pymodbus.transaction import ModbusRtuFramer #all the imports for flask import sqlite3 from flask import Flask, request, session, g, redirect, url_for, \ abort, render_template, flash from contextlib import closing #---------------------------------------------------------------------------------------- #initialization of BBB's UART4 (device tree overlay and UART parameters) UART.setup("UART4") client = ModbusClient(method = 'rtu', framer = ModbusRtuFramer, port = '/dev/ttyO4', timeout = 1, baudrate = 19200, stopbits = 1, parity = 'E', bytesize = 8) #---------------------------------------------------------------------------------------- # configuration for the database DATABASE = '/root/project/logger.db' DEBUG = True SECRET_KEY = 'development key' #USERNAME = 'admin' #PASSWORD = 'default' #---------------------------------------------------------------------------------------- # create my little application app = Flask(__name__) app.config.from_object(__name__) #---------------------------------------------------------------------------------------- #connects to the specific database def connect_db(): return sqlite3.connect(app.config['DATABASE']) #---------------------------------------------------------------------------------------- #initializes the database def init_db(): with closing(connect_db()) as db: with app.open_resource('schema.sql', mode='r') as f: db.cursor().executescript(f.read()) db.commit() #---------------------------------------------------------------------------------------- #open the database before each request @app.before_request def before_request(): g.db = connect_db() #---------------------------------------------------------------------------------------- #shut down the database afterwards @app.teardown_request def teardown_request(exception): db = getattr(g, 'db', None) if db is not None: db.close() #---------------------------------------------------------------------------------------- #This view lets the user add new entries if he is logged in. There is a check #if the user is logged in as well. #Update: logged in/out function has been removed @app.route('/add', methods=['POST']) def add_entry(): # if not session.get('logged_in'): # abort(401) g.db.execute('insert into entries (URMS1, IRMS1, PRMS1, QRMS1, cosqhi1) values (?,?,?,?,?)', [request.form['rcv_data.register[0]'], request.form['rcv_data.register[1]'], request.form['rcv_data.register[2]'], request_form['rcv_data.register[3]'], request_form['rcv_data.register[4]']]) g.db.commit() flash('New entry was successfully posted') # return redirect(url_for('show_entries')) #--------------------------------------------------------------------------- #create the database init_db() #energy meter needs about 15 sec for initialization time.sleep(20) #infinite loop while 1: val = client.connect() if not val: print "Error in Client Connection" #store data to rcv_data (array[5]) rcv_data = client.read_holding_registers(35, count = 5, unit = 1) #store the data in the database add_entry() #print the stored data to screen print("Registers are") for i in range (5): #range (17) to show all the usefull data print(rcv_data.registers[i]) client.close() #data is updated every 10s time.sleep(10) -------------- next part -------------- drop table if exists entries; create table entries ( id integer primary key autoincrement, URMS1 integer not null, IRMS1 integer not null, PRMS1 integer not null, QRMS1 integer not null, cosphi1 integer not null ); From d at ddipp.net Sun Nov 20 14:23:32 2016 From: d at ddipp.net (Dmitry) Date: Mon, 21 Nov 2016 00:23:32 +0500 Subject: [Flask] Store Data in a SQLite-Database with Flask In-Reply-To: References: Message-ID: <9f2483d6-74f5-491d-f6d2-ae8a1fc89930@ddipp.net> Hello Andreas! Tell me - why do you need a flask? Flask - web framework, and you have a function, framed by "@app.route" is caused not through the web, and from the same program. Try to remove the "@ app.route" to function. Also, you will not be initialized "g.db". Think about why. 20.11.2016 21:23, Andreas Dorfner ?????: > Hello everyone, > > I'm new to python and flask web development. > I have to realize a data logger using flask. By using another > framework called 'pymodbus', I get some data from > an energy meter and save that data to a variable called 'crv_data' > (see file attached). This part is working so far! > > The second part of the project contains the storage of 'crv_data' to a > SQLite-Database. For that, I'm using flask. > For creating a table with five columns, a file 'schema.sql' (also > attached) is included. > > If the file 'logger.py' is running, a database is produced with the > desired layout and some data in 'crv_data' is available. > But there is no data included in the database. > > I think there is an error in function 'add_entry()' (starting in line > 72 of logger.py file), because if I call the function > in the infinite loop (line 103), the program stops running. > Maybe I have to say that I using Beaglebone Black with running Debian > on it. > > Does anybody have an idea, why the program doesn't work? > Many thanks, > Andreas > > > --- > Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. > https://www.avast.com/antivirus > > > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask -- Dmitry Podkovyrkin email: d at ddipp.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From ford.anthonyj at gmail.com Mon Nov 21 16:02:14 2016 From: ford.anthonyj at gmail.com (Anthony Ford) Date: Mon, 21 Nov 2016 15:02:14 -0600 Subject: [Flask] Store Data in a SQLite-Database with Flask In-Reply-To: <9f2483d6-74f5-491d-f6d2-ae8a1fc89930@ddipp.net> References: <9f2483d6-74f5-491d-f6d2-ae8a1fc89930@ddipp.net> Message-ID: I created a similar system to monitor a series of Modbus enabled GPIO units (Acromag units). Flask is not well suited to having a data logger within it. Flask is more of a User Interface, and is event based, meaning that nothing happens until the server (and therefore your Flask app) get a request from a browser. This means you can easily fetch the current state when someone wants it (i.e. a big "FETCH STATUS" button). However, if you need periodic monitoring (like I did), it is better to have a separate program that ONLY does the data logging. You can possibly get away with threading, and launching a separate thread from within your Flask app, but I personally think it's not worth the complexity. If you use a full database system (PostgreSQL is my go-to) instead of SQLite, it is easy to have the logger application dump into the database, while the Flask app reads from the Database. If you avoid using Flask-SQLAlchemy in favor of vanilla (or standard) SQLAlchemy, you can even share the database modules between the apps (via a common shared package, or just plain importing from one or the other). If desired, I can talk more about my approach, but unfortunately I can't share code, and sanitizing the code to be shareable would take a lot of work. Hope this helps! Anthony Ford, KF5IBN, ford.anthonyj at gmail.com On Sun, Nov 20, 2016 at 1:23 PM, Dmitry wrote: > Hello Andreas! > > Tell me - why do you need a flask? > Flask - web framework, and you have a function, framed by "@app.route" is > caused not through the web, and from the same program. > Try to remove the "@ app.route" to function. > Also, you will not be initialized "g.db". Think about why. > > 20.11.2016 21:23, Andreas Dorfner ?????: > > Hello everyone, > > I'm new to python and flask web development. > I have to realize a data logger using flask. By using another framework > called 'pymodbus', I get some data from > an energy meter and save that data to a variable called 'crv_data' (see > file attached). This part is working so far! > > The second part of the project contains the storage of 'crv_data' to a > SQLite-Database. For that, I'm using flask. > For creating a table with five columns, a file 'schema.sql' (also > attached) is included. > > If the file 'logger.py' is running, a database is produced with the > desired layout and some data in 'crv_data' is available. > But there is no data included in the database. > > I think there is an error in function 'add_entry()' (starting in line 72 > of logger.py file), because if I call the function > in the infinite loop (line 103), the program stops running. > Maybe I have to say that I using Beaglebone Black with running Debian on > it. > > Does anybody have an idea, why the program doesn't work? > Many thanks, > Andreas > > > --- > Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. > https://www.avast.com/antivirus > > > _______________________________________________ > Flask mailing listFlask at python.orghttps://mail.python.org/mailman/listinfo/flask > > > -- > Dmitry Podkovyrkin > email: d at ddipp.net > > > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From coreybrett at gmail.com Mon Nov 21 16:16:19 2016 From: coreybrett at gmail.com (Corey Boyle) Date: Mon, 21 Nov 2016 16:16:19 -0500 Subject: [Flask] Store Data in a SQLite-Database with Flask In-Reply-To: References: <9f2483d6-74f5-491d-f6d2-ae8a1fc89930@ddipp.net> Message-ID: This is not related to Flask, but maybe you could use Zabbix Sender to send your data to a Zabbix server for collection. https://www.zabbix.com/documentation/3.0/manual/concepts/sender On Nov 21, 2016 4:10 PM, "Anthony Ford" wrote: > I created a similar system to monitor a series of Modbus enabled GPIO > units (Acromag > units). > Flask is not well suited to having a data logger within it. > > Flask is more of a User Interface, and is event based, meaning that > nothing happens until the server (and therefore your Flask app) get a > request from a browser. This means you can easily fetch the current state > when someone wants it (i.e. a big "FETCH STATUS" button). However, if you > need periodic monitoring (like I did), it is better to have a separate > program that ONLY does the data logging. > > You can possibly get away with threading, and launching a separate thread > from within your Flask app, but I personally think it's not worth the > complexity. > > If you use a full database system (PostgreSQL is my go-to) instead of > SQLite, it is easy to have the logger application dump into the database, > while the Flask app reads from the Database. > > If you avoid using Flask-SQLAlchemy in favor of vanilla (or standard) > SQLAlchemy, you can even share the database modules between the apps (via a > common shared package, or just plain importing from one or the other). > > If desired, I can talk more about my approach, but unfortunately I can't > share code, and sanitizing the code to be shareable would take a lot of > work. > > Hope this helps! > > Anthony Ford, > KF5IBN, > ford.anthonyj at gmail.com > > On Sun, Nov 20, 2016 at 1:23 PM, Dmitry wrote: > >> Hello Andreas! >> >> Tell me - why do you need a flask? >> Flask - web framework, and you have a function, framed by "@app.route" is >> caused not through the web, and from the same program. >> Try to remove the "@ app.route" to function. >> Also, you will not be initialized "g.db". Think about why. >> >> 20.11.2016 21:23, Andreas Dorfner ?????: >> >> Hello everyone, >> >> I'm new to python and flask web development. >> I have to realize a data logger using flask. By using another framework >> called 'pymodbus', I get some data from >> an energy meter and save that data to a variable called 'crv_data' (see >> file attached). This part is working so far! >> >> The second part of the project contains the storage of 'crv_data' to a >> SQLite-Database. For that, I'm using flask. >> For creating a table with five columns, a file 'schema.sql' (also >> attached) is included. >> >> If the file 'logger.py' is running, a database is produced with the >> desired layout and some data in 'crv_data' is available. >> But there is no data included in the database. >> >> I think there is an error in function 'add_entry()' (starting in line 72 >> of logger.py file), because if I call the function >> in the infinite loop (line 103), the program stops running. >> Maybe I have to say that I using Beaglebone Black with running Debian on >> it. >> >> Does anybody have an idea, why the program doesn't work? >> Many thanks, >> Andreas >> >> >> --- >> Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. >> https://www.avast.com/antivirus >> >> >> _______________________________________________ >> Flask mailing listFlask at python.orghttps://mail.python.org/mailman/listinfo/flask >> >> >> -- >> Dmitry Podkovyrkin >> email: d at ddipp.net >> >> >> _______________________________________________ >> Flask mailing list >> Flask at python.org >> https://mail.python.org/mailman/listinfo/flask >> >> > > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > > -------------- next part -------------- An HTML attachment was scrubbed... URL: