From c8a6c023beb038803cbdc706a72957f06e7dbbf0 Mon Sep 17 00:00:00 2001 From: Nitish Garg Date: Wed, 6 Nov 2019 04:25:26 +0530 Subject: [PATCH] Add README, pip requirements and update command-line interface --- .gitignore | 1 + README.md | 63 +++++++++++++++++++++++++++++++++++++++++++ flaskapp/app.py | 10 +++---- flaskapp/config.py | 8 +++++- flaskapp/constants.py | 2 -- requirements.txt | 23 ++++++++++++++++ run.py | 15 +++++++++-- 7 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 README.md create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index 2b92049..399d81d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ venv/ # Flask static file cache flaskapp/static/.webassets-cache/ +flaskapp/static/public/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..9ddc84b --- /dev/null +++ b/README.md @@ -0,0 +1,63 @@ +# Starter flask app + +This repo serves as the base setup for developing your flask application, with some of the best practices and easy-to-scale code structure. + + +### Features +* Create views with re-usable Jinja templates +* CSS & JS bundles defined to enable loading only the required files page-wise +* Easy to configure/enable the following: + * MongoDB connection and querying + * Uploading & downloading of static files +* Gzip compression +* CSS, JS minification + +**Note** : Tested with Python 3.6 + + +## Setting up project + +1. Clone the repo + +2. Create virtual environment for the project and install pip dependencies + ```bash + cd starter_flask_app + virtualenv -p python3 venv + # Source the virtual env + # zsh/fish users need to use activate.zsh/activate.fish files instead + source venv/bin/activate + pip install -r requirements.txt + ``` + + +## Running the server + +```bash +python run.py +``` + +Supported arguments: +* ```--host``` : To set the app host (defaults to 0.0.0.0) +* ```--port``` : To set the app port (defaults to 5000) +* ```--debug``` : Pass this flag to enable debugging + +Now open ```http://localhost:5000``` in your browser to view the app. + + +## Developing your app from the base code + +* Enabling mongo: + * Set the appropriate constants in ```flaskapp/config.py``` + * Uncomment the mentioned line in ```flaskapp/app.py``` +* Enabling static file downloading: + * Set the variable ```DOWNLOADS_DIR``` in ```flaskapp/config.py``` + * Uncomment the mentioned function in ```flaskapp/app.py``` + * Modify the route in this function as appropriate. +* Define templates for your pages in ```flaskapp/templates/``` dir. + See ```home.html``` for reference which inherits the basic structure from ```layout.html``` + and just defines the required parts. +* Define route for the pages in ```flaskapp/routes/view_routes.py``` +* Define API related routes in ```flaskapp/routes/api_routes.py``` +* Add the CSS & JS files in ```flaskapp/static/``` in appropriate subdir. + Then, define the suitable bundle page-wise in ```flaskapp/assets.py```. + Refer the existing bundles in the file. diff --git a/flaskapp/app.py b/flaskapp/app.py index 0ec62e4..21b855c 100644 --- a/flaskapp/app.py +++ b/flaskapp/app.py @@ -16,7 +16,7 @@ # App configuration app.config['SEND_FILE_MAX_AGE_DEFAULT'] = config.STATIC_FILE_MAX_AGE -app.config['UPLOAD_FOLDER'] = C.UPLOADS_FOLDER +app.config['UPLOAD_FOLDER'] = config.UPLOADS_DIR # MongoDB app.config["MONGO_HOST"] = config.MONGO_HOST @@ -34,7 +34,7 @@ app.register_blueprint(routes_module) -# Static data files -@app.route('/datafile/') -def loadDataFile(filename): - return send_from_directory(app.static_folder + C.DATA_FILE_DIR, filename) +# Uncomment and configure below function to enable downloading static files +# @app.route('/datafile/') +# def downloadFile(filename): +# return send_from_directory(app.static_folder + config.DOWNLOADS_DIR, filename) diff --git a/flaskapp/config.py b/flaskapp/config.py index 3cb6c78..9c6acfd 100644 --- a/flaskapp/config.py +++ b/flaskapp/config.py @@ -1,5 +1,5 @@ # Used to set : app.config['SEND_FILE_MAX_AGE_DEFAULT'] -STATIC_FILE_MAX_AGE = 3600 * 24 * 7 +STATIC_FILE_MAX_AGE = 3600 * 24 * 7 # 1 week # MongoDB variables MONGO_HOST = "localhost" @@ -7,3 +7,9 @@ MONGO_NAME = "dbname" MONGO_USER = "" MONGO_PASS = "" + +# Directory for uploading files to server +UPLOADS_DIR = "uploads/" + +# Directory for downloading files from server +DOWNLOADS_DIR = "/data_files" diff --git a/flaskapp/constants.py b/flaskapp/constants.py index 635874a..e69de29 100644 --- a/flaskapp/constants.py +++ b/flaskapp/constants.py @@ -1,2 +0,0 @@ -UPLOADS_FOLDER = "uploads/" -DATA_FILE_DIR = "/data_files" diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..591c5ce --- /dev/null +++ b/requirements.txt @@ -0,0 +1,23 @@ +certifi==2019.9.11 +chardet==3.0.4 +Click==7.0 +cssmin==0.2.0 +Flask==1.1.1 +Flask-Assets==0.12 +Flask-Compress==1.4.0 +Flask-PyMongo==2.3.0 +get==2019.4.13 +idna==2.8 +itsdangerous==1.1.0 +Jinja2==2.10.3 +jsmin==2.2.2 +MarkupSafe==1.1.1 +post==2019.4.13 +public==2019.4.13 +pymongo==3.9.0 +query-string==2019.4.13 +request==2019.4.13 +requests==2.22.0 +urllib3==1.25.6 +webassets==0.12.1 +Werkzeug==0.16.0 diff --git a/run.py b/run.py index 69c4c4e..987b5f0 100644 --- a/run.py +++ b/run.py @@ -10,11 +10,22 @@ def get_args(): Get command-line arguments """ parser = argparse.ArgumentParser() - parser.add_argument('--debug', action='store_true', help='Run in debug mode') + parser.add_argument('--host', + type=str, + default='0.0.0.0', + help='Sets the host on which the app is to be run') + parser.add_argument('--port', + type=int, + default=5000, + help='Sets the port on which the app is to be run') + parser.add_argument('--debug', + action='store_true', + help='Run in debug mode') + args = parser.parse_args() return args if __name__ == "__main__": args = get_args() - app.run(host='0.0.0.0', debug=args.debug) + app.run(host=args.host, port=args.port, debug=args.debug)