Sending JSON and status code with a Flask response [duplicate]

Use flask.jsonify(). This method takes any serializable data type. For example I have used a dictionary data in the following example. from flask import jsonify @app.route(‘/login’, methods=[‘POST’]) def login(): data = {‘name’: ‘nabin khadka’} return jsonify(data) To return a status code, return a tuple of the response and code: return jsonify(data), 200 Note that 200 … Read more

Getting first row from sqlalchemy

Use query.one() to get one, and exactly one result. In all other cases it will raise an exception you can handle: from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.orm.exc import MultipleResultsFound try: user = session.query(User).one() except MultipleResultsFound, e: print e # Deal with it except NoResultFound, e: print e # Deal with that as well There’s also … Read more

Flask app “Restarting with stat”

Check your version of Werkzeug. Version 0.10 was just released and numerous changes went into the reloader. One change is that a default polling reloader is used; the old pyinotify reloader was apparently inaccurate. If you want more efficient polling, install the watchdog package. You can see the code related to this here. When Werkzeug … Read more

Serving a front end created with create-react-app with Flask

import os from flask import Flask, send_from_directory app = Flask(__name__, static_folder=”react_app/build”) # Serve React App @app.route(“https://stackoverflow.com/”, defaults={‘path’: ”}) @app.route(‘/<path:path>’) def serve(path): if path != “” and os.path.exists(app.static_folder + “https://stackoverflow.com/” + path): return send_from_directory(app.static_folder, path) else: return send_from_directory(app.static_folder, ‘index.html’) if __name__ == ‘__main__’: app.run(use_reloader=True, port=5000, threaded=True) Thats what I ended up with. So bascially catch all … Read more

Get data from html and and pass the data back to the front end using ajax or js

You can use ajax with Jquery. You can see this doc for more details. How to proceed: Configure js scripts In your HTML file template: Load Jquery: Load Jquery preferably before any other javascript files. Either statically: <script type=text/javascript src=”https://stackoverflow.com/questions/52870184/{{url_for(“static’, filename=”jquery.js”) }}”> </script> Or using Google’s AJAX Libraries API: <script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js”></script> <script>window.jQuery || document.write(‘<script src=”https://stackoverflow.com/questions/52870184/{{url_for(“static’, … Read more

Run Flask CLI command with PyCharm debugger

PyCharm’s “Flask server” configuration only calls the flask run command, it doesn’t provide a way to call other commands. To do that, create a regular “Python” configuration that runs the flask command with the arguments you want. Create a “Python” configuration and give it a name. Select “Module name” instead of “Script path” and type … Read more

How to generate an html directory list using Python

You could separate the directory tree generation and its rendering as html. To generate the tree you could use a simple recursive function: def make_tree(path): tree = dict(name=os.path.basename(path), children=[]) try: lst = os.listdir(path) except OSError: pass #ignore errors else: for name in lst: fn = os.path.join(path, name) if os.path.isdir(fn): tree[‘children’].append(make_tree(fn)) else: tree[‘children’].append(dict(name=name)) return tree To … Read more

tech