Nathan Wailes - Blog - GitHub - LinkedIn - Patreon - Reddit - Stack Overflow - Twitter - YouTube
Flask (Python)
Table of contents
Child pages
Related pages
http://flask-appbuilder.readthedocs.org/en/latest/index.html
- The Flask Mega-Tutorial
- This is by the author of "Flask Web Development"
- Do the video tutorials! - http://shop.oreilly.com/product/0636920034797.do?cmp=af-prog-books-videos-product_cj_9781491911921_%25zp
How to take a bare-bones Flask app and gradually add onto it
It seems like the official Flask tutorial does a pretty good job of succinctly covering this topic: http://flask.pocoo.org/docs/0.12/tutorial/
- Have it use templates.
- Create a 'templates' folder in the same directory that contains the main app file.
- Create files in the folder that are named after the pages they correspond to, and have an .html extension.
- In your view functions, update the code to return "render_template('index.html')", where 'index.html' is replaced with the name of the template that should be rendered for that view function.
AWS
- You can find instructions for deploying Flask on AWS here: AWS / Amazon Web Services (Web development)
- 2013.07.14 - Gareth Dwyer - Getting a simple Flask app running on Amazon EC2
Accepting payments
- Stripe: (this is supposed to be easy)
- PayPal: (this was a nightmare with CraigslistAutorespond)
- General:
Deploying
- Flask docs - Deployment Options
- Reddit - /r/Flask - [Stupid Question] Deploying site
- Some good info in here.
Flask-SQLAlchemy
Misc
- Show all running processes on the DB: open a console (right-click the db in PyCharm and select 'Open Console') and then paste in SHOW PROCESSLIST; and hit 'Play'.
"QueuePool limit reached" error
I fixed the error by getting rid of "threaded=True" from my "app.run()" call. I posted info about it here.
Similar errors
- This may be the way to fix it, I've made the change and I'm waiting to see if it works: https://stackoverflow.com/a/34498662/4115031
- Looks especially helpful: SO - Avoiding “MySQL server has gone away” on infrequently used Python / Flask server with SQLAlchemy
- Especially helpful: SQLAlchemy and "Lost connection to MySQL server during query"
- MySql Proccesslist filled with “Sleep” Entries leading to “Too many Connections”?'
The exact same error
- Sql Alchemy QueuePool limit overflow - Stack Overflow
- You can manage pool size by adding parameters pool_size and max_overflow in function create_engine
- SQLAlchemy raises QueuePool limit of size 10 overflow 10 reached, connection timed out after some time
- The issue was sometimes database connection was going to lost state, which is causing pool size to be Exhausted after some interval.
To fix the issue I made the MySQL server configuration change for query timeout and made it 1 second.
After 1 second if the query didn't respond it will throw Exception and I added except block in code where that query was invoked(In my case it was GET query). In the Except block, I issued rollback command.
- The issue was sometimes database connection was going to lost state, which is causing pool size to be Exhausted after some interval.
- Flask-SQLAlchemy TimeoutError
This could happen if you are using debug=True in your application and you have loaded several pages or API endpoints which errored out.
The reason is that running the debug version of the app keeps a live debugger open in the error page. This live debugger keeps around all the resources from processing the request, so that you can examine them. This means that the database connection can't be recycled.
- I found myself having to explicitly close sessions in the jobs the scheduler executed, or after several hours of running I would get this error.
@app.teardown_request def checkin_db(exc): try: g.db.close() except AttributeError: pass
- Flask-SQLAlchemy does this for you. This should not be necessary if you are using Flask-SQLAlchemy
- GitHub - Connection was not be put back to QueuePool after un-caught exceptions
- GitHub - GeoHealthCheck - DB Connection pool exhaust with SQLAlchemy/PostgreSQL
- The latter commit also got rid of this error from Postgresql, due to GHC Check Runner exit without removing connections:
- Sql Alchemy connection time Out
- Whenever you create a new session in your code, make sure you close it. Just call session.close()
- Make sure you also close the sessions when exceptions fire. The sqlalchemy docs suggest using a context manager to deal with this (among other solutions)
- Google Groups - SQLAlchemy - QueuePool limit size reached, using expression API only
- close your connections after you are finished with them.
- The OP may be storing stray references somewhere, or associating them somehow to a reference cycle that takes time to be freed.
How can I check the status of the QueuePool?
- SO - How could I check the number of active sqlalchemy connections in a pool at any given time?
- The default QueuePool has a 'status' method
- SO - mysql: see all open connections to a given database?
- SHOW PROCESSLIST;
Sessions
- Summary: My impression at the moment is that sessions are basically cookies where the cookie is guaranteed against tampering.
- 2016.04.04 - Miguel Grinberg - How Secure Is The Flask User Session?
- Flask session cookies are designed to not be tampered with, but they are not designed to be hard to read.
- You can read the contents of a Flask session cookie by running
base64.urlsafe_b64decode(<session_string_before_the_period>)
- So don't put anything that should be kept secret in those cookies.
Static site generators
Tutorials
- FullStackPython - Static Site Generator
- 2012.03.04 - Nicholas Perriault - Dead easy yet powerful static website generator with Flask