Flask

Jinja: loop to create form fields with same name but the last character

随声附和 提交于 2021-01-28 03:30:25
问题 I am using Flask and I have a WTF form with 12 input fields named like sold_1, sold_2,..., sold_12. I would like to generate these fields in Jinja using a loop like: {% for r in range(1, 13) %} {{ form.sold_ }}{{ r }} {% endfor %} or a similar syntax, but it doesn't work. I solved it in a quite convoluted way as follows: {% set tmp = "sold_x" %} {% for r in range(1, 13) %} {{ form[tmp | replace('x', r)] }} {% endfor %} but I wonder whether there is a cleaner way. Thanks 回答1: You could use

Perform alembic upgrade in multiple schemas

隐身守侯 提交于 2021-01-28 02:51:48
问题 I am using flask + sqlalchemy + alembic + postgresql, and am trying to find a simple architecture to solve the following problem. I have a simple database structure, lets say two tables : -- Users -- UserItems My users are in several different domains. I would like to have several of this database structure. For that I have database schemas in SQL. I created the matching class structure using sqlalchemy decalrative_base, and end up with a MetaData object that isn't tied to a specific schema.

Flask and sys.excepthook

左心房为你撑大大i 提交于 2021-01-28 02:08:30
问题 I want to add global exception handling object to my Flask webproject. In main module, where application class is created I've added code to override sys.excepthook . Here is simple test code: import sys def my_exception_hook(exception_type, value, traceback): print "My exception handler" print " Exception type:", exception_type print " Exception instance:", value print " Traceback:", traceback sys.__excepthook__(exception_type, value, traceback) sys.excepthook = my_exception_hook from flask

Can't upload large files to Python + Flask in GCP App Engine

只愿长相守 提交于 2021-01-28 02:07:11
问题 UPDATE: (5/18/2020) Solution at the end of this post! I'm attempting to upload big CSV files (30MB - 2GB) from a browser to GCP App Engine running Python 3.7 + Flask, and then push those files to GCP Storage. This works fine on local testing with large files, but errors out immediately on GCP with a " 413 - Your client issued a request that was too large " if the file is larger than roughly 20MB. This error happens instantly on upload before it even reaches my custom Python logic (I suspect

Difference between returning render_templates and jinja templtes in flask

帅比萌擦擦* 提交于 2021-01-28 01:54:48
问题 I have seen two ways to route HTML pages in Flask. Either you declare a variable called template like so def home(): template = jinja_env.get_template('hello_form.html') return template.render() or you just return the HTML templates def home(): return render_template('home.html', posts=posts) is there a difference between the two if yes, what is it? 回答1: They're effectively the same thing and you should probably use the second one because it's more "Flask-y" and may broadcast events about

Stopping Flask initialization from blocking

旧时模样 提交于 2021-01-28 01:37:54
问题 I'm adding Flask support to a plugin-based application. On startup, the app instantiates a number of plugin classes. I thought this would be as simple as having Flask kick off when the class is initialized, but instead, the whole app hangs when it hits the Flask startup method. Consider the following example: #!/usr/bin/env python from flask import Flask class TestClass: def __init__(self): print('Initializing an instance of TestClass') self.app = Flask(__name__) self.app.run() print("Won't

Uploading file in python flask

别说谁变了你拦得住时间么 提交于 2021-01-28 01:15:51
问题 I am trying to incorporate uploading a basic text/csv file on my web app which runs flask to handle http requests. I tried to follow the baby example in flasks documentation running on localhost here. But when I try this code on my page it seems to upload but then just hangs and in fact my flask server freezes and I have to close terminal to try again...Ctrl+C doesn't even work. I execute run.py : #!/usr/bin/env python from app import app if __name__ == '__main__': app.run(host='0.0.0.0',

How can I marshal a pickled object through an API ( preferably using flask-restplus )?

女生的网名这么多〃 提交于 2021-01-28 01:11:34
问题 I have an API fully documented and finished, built in python 3.5/flask using flask-restplus. I'd like to add one chunk of functionality - returning a pickled object as part of one of my responses. General solutions not specific to flask-restplus are welcome, but as my API is fully documented and finished (other than this little bit), I'd rather hang this on rather than fundamentally altering the framework I'm using. My model schema looks like this (simplified): get_fields = api.model('get

Flask-Uploads always throwing 'UploadNotAllowed' error even with no constraints

 ̄綄美尐妖づ 提交于 2021-01-28 00:32:18
问题 Flask-uploads has something called UploadSet which is described as a "single collection of files". I can use this upload set to save my file to a predefined location. I've defined my setup: app = Flask(__name__) app.config['UPLOADS_DEFAULT_DEST'] = os.path.realpath('.') + '/uploads' app.config['UPLOADED_PHOTOS_ALLOW'] = set(['png', 'jpg', 'jpeg']) app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # setup flask-uploads photos = UploadSet('photos') configure_uploads(app, photos) @app.route('

Why there is no option to add custom url converters to blueprints like for main app?

不羁岁月 提交于 2021-01-27 21:34:49
问题 In this post and in official docs we saw how to add custom url converters for main app object. Here is short example: app = Flask(__name__) app.url_map.converters['list'] = ListConverter But how to do it for blueprints? This global (app level) custom converter is unavailable for blueprints. In source code I haven't found such posibility... 回答1: The technical reason why you can't have custom URL converters on a blueprint is that unlike applications, blueprints do not have a URL map. When you