Skip to content

Commit

Permalink
Add README, pip requirements and update command-line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nitish6174 committed Mar 29, 2020
1 parent 86918b2 commit c8a6c02
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
venv/
# Flask static file cache
flaskapp/static/.webassets-cache/
flaskapp/static/public/
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 5 additions & 5 deletions flaskapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,7 +34,7 @@
app.register_blueprint(routes_module)


# Static data files
@app.route('/datafile/<path:filename>')
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/<path:filename>')
# def downloadFile(filename):
# return send_from_directory(app.static_folder + config.DOWNLOADS_DIR, filename)
8 changes: 7 additions & 1 deletion flaskapp/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# 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"
MONGO_PORT = "27017"
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"
2 changes: 0 additions & 2 deletions flaskapp/constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
UPLOADS_FOLDER = "uploads/"
DATA_FILE_DIR = "/data_files"
23 changes: 23 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
15 changes: 13 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit c8a6c02

Please sign in to comment.