One feature I have been struggling to implement in flask-admin is when the user edits a form, to constrain the value of Field 2 once Field 1 has been set. Let me give a simplified example in words (the actual use case is more convoluted). Then I will show a full gist that implements that example, minus the “constrain” feature.
Tag: flask
Not able to parse a .csv file uploaded using Flask
I am trying to upload a CSV file, work on it to produce results, and write back (download) a new CSV file containing the result. I am very new to Flask and I am not able to get a “proper” csv.reader object to iterate and work upon. Here is the code so far, The terminal output being Whereas the file
Flask validates decorator multiple fields simultaneously
I have been using the @validates decorator in sqlalchemy.orm from flask to validate fields, and all has gone well as long as all of the fields are independent of one another such as: However, now I need to do some validation that will require access to field_one and field_two simultaneously. It looks like validates accepts multiple arguments to the validates
Get the value of a checkbox in Flask
I want to get the value of a checkbox in Flask. I’ve read a similar post and tried to use the output of request.form.getlist(‘match’) and since it’s a list I use [0], but it seems I’m doing something wrong. Is this the correct way to get the output or is there a better way? Answer You don’t need to use
Show the SQL generated by Flask-SQLAlchemy
I want to get the SQL issued by Flask-SQLAlchemy queries. In Django, I can print the query attribute to get SQL. How can I get something similar in Flask? Answer Flask-SQLAlchemy records debugging information about all queries during a request. You can get the information with get_debug_queries(). Flask-Debugtoolbar, for example, uses this to offer a debug panel with query timing
Python, Flask: How to set response header for all responses
I want to set all of my http headers responses to something like this: I checked this question, but it only changes the header for one specific controller. I want to change all of my headers maybe in “before_request” function similar to the following logic. How can I do that? Answer Set the header in a @app.after_request() hook, at which
How to run a flask application?
I want to know the correct way to start a flask application. The docs show two different commands: and produce the same result and run the application correctly. What is the difference between the two and which should be used to run a Flask application? Answer The flask command is a CLI for interacting with Flask apps. The docs describe
flask socketio CORS
I’ve been trying to send data from flask over socket io. I need to access this data from a different origin, but it is giving a CORS error. I have tried using all kinds of cross origin stuff and none of it has worked. Can somebody help with this. The view that should be called thought socket io: Running the
How to send requests with JSON in unit tests
I have code within a Flask application that uses JSONs in the request, and I can get the JSON object like so: This has been working fine, however I am trying to create unit tests using Python’s unittest module and I’m having difficulty finding a way to send a JSON with the request. This gives me: Flask seems to have
WTForms-JSON not working with FormFields
Nested forms (FormFields) doesn’t get populated with data when I use WTForms-JSON. I can’t spot my mistake, see example below. I send the following JSON-request but the print after form.from_json(request.json) reveals that the address object is never populated with data (also, the “appropriate” errors are returned from the route). Print output: {‘name’: u’Alex’, ‘address’: {‘street’: u”, ‘number’: None}} I’m using