Flask Guide

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 10

Scroll down to view the document on your mobile browser.
Base applications All work is available either at GitHub hosted in .git format or at BitBucket hosted in .hg (Mercurial) format. Although .hg was used initially so links in README.md’s link to Bitbucket, additionally there are some .hg related files lingering in the directory. . Git links are available here for both: - The Flask application: https://github.com/podit/flawsk - The web frontend: https://github.com/podit/flawsk-web . Or through Mercurial: - Flask application: https://bitbucket.org/tomody/flawsk/src/default/ - Frontend: https://bitbucket.org/tomody/flawsk-web/src/default/ Project initialization . Create a virtual environment (venv) for python version 3.6 (2.7 or 3.3+) . Add flask to the venv with pip install flask once the venv is activated . Create ‘.gitignore’ (and if using mercurial ‘.hgignore’) containing the venv directory - AWS uses the .gitignore so see what from the directory should be uploaded (it will ignore a .hgignore) . Create ‘application.py’ as the main script Flask core (application.py) . Import flask with from flask import Flask . Implement functions to perform desired actions . Set flask name with application = Flask(__name__) - I believe that app (or anything) could potentially work in place of application although this may not work for an AWS deployment.  . Create url rule with application.add_url_rule('<url>', '<name>', (lambda: <function>)) - e.g application.add_url_rule('/hi', 'hi', (lambda: hi())) - This will call the function hi() when the server is called with /hi appended to the server address e.g. http://127.0.0.1:5000 or http://localhost:5000 for the default Flask application . Finally initialize the server checking that the name has been initialized properly if __name__ == "__main__": application.run() . To add debugging and allow the server to update itself when changes are detected add debug=True  to  application.run()  in the brackets Functions in flask . Can be in ‘application.py’ as long as they are earlier in the script than the url calls . No additional python rules to follow in the flask framework, try to keep it light, make sure not to overuse resources. A slow response holds up the entire API.
. The only thing to be wary of is the output, which is handled using return () just like any other python function. This when used in flask will return the value as a string in the response to the http request that called it. . Using the example from the section above the function called would look something like this: def hi():     return 'Hello there...'  - This would respond with the string ‘Hello there…’ . Another example to raise one number to the power of another would be: def power(num, exp):     pwr = num ** exp     return str(pwr) . Called by the url rule: application.add_url_rule('/pwr/<int:num>/<int:exp>', 'power', (lambda num, exp: power(num, exp))) - In the example of http://127.0.0.1:5000/pwr/4/4 being requested the response would be the string ‘256’ - Note that attempting to return an integer without converting it will result in an internal server error, code 500. As an integer has been returned instead of a string . See ‘example.py’ for a simple example of how to structure a flask application. I would recommend ‘httpie’ as an application with which to test the url rules before advancing to using CORS for use within a browser. Additional Flask functionality . CORS allows for the http responses from the server to be processed in a browser. Allowing for processing by frontend web applications. - Installed using: pip install flask-cors - Then it is imported using from flask_cors import CORS. And added using the function CORS(application) with ‘application’ being the application name. This should be done just below where the application name is set . Another flask feature is the ability to create a json response, this is achieved by importing ‘Flask’ and ‘jsonify’ from flask as so: from flask import Flask, jsonify Data handling in flask . Data handling in flask can be achieved in many ways, for example data can be inputted into the server and serialised into file allowing for real time, automatic data management. Flask can work with multiple types of data storage methods as well, however due to the web frontend based nature of the task I chose to use JSON. Although a .csv or any other type would be just as applicable. . The dataset I used to build the flask backend was a collection of various pieces if information pertaining to stock analysis. In particular this dataset contained various points pertaining to oil, every 4 hours from 2008 to 2018. This dataset has around 16 thousand data-points.

Navigation menu