Flask

Expire session in flask in ajax context

£可爱£侵袭症+ 提交于 2021-02-10 03:27:31
问题 I am using permanent_session_lifetime to expire the session of the user after some period of inactivity. The problem is that, this request is made through ajax, so i can't redirect in ajax context with the normal behavior of the flask. http://xxxx/login?next=%2Fusers%2Fajax_step1 Instead of this, I want to redirect to my logout route in the before_request , if the flask session expire. How can i do that? @mod.before_request def make_session_permanent(): session.modified = True session

Flask

最后都变了- 提交于 2021-02-09 20:25:47
1. 像snap一样阅后即焚,在服务器端临时存储数据的地方,如显示错误信息。(也可以用session实现) 2. Flash的底层是session做的,所以要secret_key。可以看源码 3. flash()存储数据,get_flashed_messages()获得数据 例子1. flash向某个地方设置一个值,从某个地方获取设置过的所有值,并清除。 from flask import Flask,flash,get_flashed_messages app = Flask(__name__) app.secret_key = 'asdfasdf' @app.route('/get') def get(): # 从某个地方获取设置过的所有值,并清除。 data = get_flashed_messages() print(data) return 'Hello World!' @app.route('/set') def set(): # 向某个地方设置一个值 flash('大家好!') return 'Hello World!' if __name__ == '__main__': app.run() 127.0.0.1:5000/get 127.0.0.1:5000/set 127.0.0.1:5000/get 例子2. 提高传输信息安全性。和可以分类,防止数据错乱。

Generator from function prints

蓝咒 提交于 2021-02-09 11:54:08
问题 At the moment I have a little flask project that calls another python file. I'm fully aware that this way is kinda awful, and so, I want to swap it for a function call while maintaining the prints getting yelded to the website . def get_Checks(): root = request.url_root def func(): yield ("Inicio <br>") with subprocess.Popen(r"python somefile.py", stdout=subprocess.PIPE, bufsize=1, universal_newlines=True) as p: for line in p.stdout: yield (line + "<br>") return Response(func()) I've tryed to

How can I initialize the database automatically with SQLalchemy and Alembic?

心不动则不痛 提交于 2021-02-09 08:48:09
问题 Currently, I run $ flask db init $ flask db migrate -m "initialization" $ flask db upgrade if the database does not exist. I would like to run this within Python, e.g. something like app.create_db() so that I don't have to care about setting the database up. Is that possible? I use the flask-sqlalchemy and flask-migrations plugins 回答1: Obviously, you have installed flask-migrate, flask-sqlalchemy . So, you can do like this: from flask_sqlalchemy import SQLAlchemy from flask import Flask app =

Gunicorn --preload option is causing workers to hang?

白昼怎懂夜的黑 提交于 2021-02-09 08:26:23
问题 We have a flask app that uses a lot of memory for ML models, and I'm trying to reduce the memory footprint by using gunicorn's preload option, but when I add the --preload flag, and deploy that (with -w 4 , to a docker container running on GKE), it will handle just a few requests, and then hang until it times out, at which point gunicorn will start another worker to replace it and the same thing will happen. It's not clear yet how many requests each worker will process before hanging

Gunicorn --preload option is causing workers to hang?

北城以北 提交于 2021-02-09 08:25:54
问题 We have a flask app that uses a lot of memory for ML models, and I'm trying to reduce the memory footprint by using gunicorn's preload option, but when I add the --preload flag, and deploy that (with -w 4 , to a docker container running on GKE), it will handle just a few requests, and then hang until it times out, at which point gunicorn will start another worker to replace it and the same thing will happen. It's not clear yet how many requests each worker will process before hanging

Gunicorn --preload option is causing workers to hang?

北城余情 提交于 2021-02-09 08:23:27
问题 We have a flask app that uses a lot of memory for ML models, and I'm trying to reduce the memory footprint by using gunicorn's preload option, but when I add the --preload flag, and deploy that (with -w 4 , to a docker container running on GKE), it will handle just a few requests, and then hang until it times out, at which point gunicorn will start another worker to replace it and the same thing will happen. It's not clear yet how many requests each worker will process before hanging

Gunicorn --preload option is causing workers to hang?

拜拜、爱过 提交于 2021-02-09 08:20:05
问题 We have a flask app that uses a lot of memory for ML models, and I'm trying to reduce the memory footprint by using gunicorn's preload option, but when I add the --preload flag, and deploy that (with -w 4 , to a docker container running on GKE), it will handle just a few requests, and then hang until it times out, at which point gunicorn will start another worker to replace it and the same thing will happen. It's not clear yet how many requests each worker will process before hanging

Flask: AttributeError: 'UnboundField' object has no attribute '__call__'?

孤街醉人 提交于 2021-02-09 02:56:09
问题 Why do I get this error? What is the UnboundField and what do I need to know in order to fix and avoid this problem in the future? Debug output when I visit http://127.0.0.1:5000/signup: AttributeError AttributeError: 'UnboundField' object has no attribute '__call__' Traceback (most recent call last) File "C:\Users\combatmath\Envs\default\lib\site-packages\flask\app.py", line 2000, in __call__ return self.wsgi_app(environ, start_response) File "C:\Users\combatmath\Envs\default\lib\site

Jinja2 check if value exists in list of dictionaries

孤人 提交于 2021-02-09 00:26:33
问题 I am trying to check if a value exists inside a list with dictionaries. I use flask 1.0.2. See example below: person_list_dict = [ { "name": "John Doe", "email": "johndoe@mydomain.com", "rol": "admin" }, { "name": "John Smith", "email": "johnsmith@mydomain.com", "rol": "user" } ] I found two ways to solve this problem, can you tell me which is better?: First option: jinja2 built-in template filter "map" <pre>{% if "admin" in person_list_dict|map(attribute="rol") %}YES{% else %}NOPE{% endif %}