Update README instructions

master
James Curtin 4 years ago committed by James Curtin
parent 3e53bdb824
commit 1ba71a7279
  1. 216
      README.md
  2. 253
      README.rst
  3. 1
      hooks/post_gen_project.py
  4. 1
      tasks.py
  5. 231
      {{cookiecutter.app_name}}/README.md
  6. 193
      {{cookiecutter.app_name}}/README.rst

@ -0,0 +1,216 @@
# cookiecutter-flask
A Flask template for [cookiecutter](https://github.com/audreyr/cookiecutter). (Supports Python ≥ 3.6)
[![Build Status](https://travis-ci.org/cookiecutter-flask/cookiecutter-flask.svg?branch=master)](https://travis-ci.org/cookiecutter-flask/cookiecutter-flask)
[![CalVer](https://img.shields.io/badge/calver-YY.MINOR.MICRO-22bfda.svg)](http://calver.org)
## Use it now
### Docker **(This is the preferred method for creating a new project)**
```bash
# Basic usage (You will be prompted to provide basic information about your application)
$ ./cookiecutter-docker.sh
full_name [Steven Loria]:
# Additional arguments are available
$ ./cookiecutter-docker.sh --help
Usage: ./cookiecutter-docker.sh [OPTIONS]
Options:
-b, --build Build Docker image before running cookiecutter
-t, --template Specify custom cookiecutter template via a URI to a git repo
e.g. https://github.com/cookiecutter-flask/cookiecutter-flask.git
Defaults to template in current working directory
-h, --help Show this message and exit
```
### Standard
If using standard instructions, Python ≥ 3.6 is required. It's recommended that you use a virtual environment (like `virtualenv`).
```bash
pip3 install cookiecutter
cookiecutter https://github.com/cookiecutter-flask/cookiecutter-flask.git
```
You will be asked about your basic info (name, project name, app name, etc.). This info will be used in your new project.
## Configure and Run
After you have generated the project code, there are a few more steps you must take before your new app will run. The README of the generated project shows you how to configure and run the application. (You can see the [template README here](https://github.com/cookiecutter-flask/cookiecutter-flask/blob/master/%7B%7Bcookiecutter.app_name%7D%7D/README.md)).
## Features
- Bootstrap 4 and Font Awesome 4 with starter templates
- Flask-SQLAlchemy with basic User model
- Easy database migrations with Flask-Migrate
- Configuration in environment variables, as per [The Twelve-Factor App](https://12factor.net/config)
- Flask-WTForms with login and registration forms
- Flask-Login for authentication
- Flask-Bcrypt for password hashing
- Procfile for deploying to a PaaS (e.g. Heroku)
- pytest and Factory-Boy for testing (example tests included)
- Flask's Click CLI configured with simple commands
- CSS and JS minification using webpack
- npm support for frontend package management
- Caching using Flask-Cache
- Useful debug toolbar
- Utilizes best practices: [Blueprints](http://flask.pocoo.org/docs/blueprints/) and [Application Factory](http://flask.pocoo.org/docs/patterns/appfactories/) patterns
## Screenshots
![Home page](https://user-images.githubusercontent.com/2379650/45271508-917f1c00-b475-11e8-9153-7f7385707a8b.png "Home page")
![Home page](https://user-images.githubusercontent.com/2379650/45271517-a9ef3680-b475-11e8-8de6-fbf3d9cab199.png "Registration form")
## Inspiration
- [Structuring Flask Apps](http://charlesleifer.com/blog/structuring-flask-apps-a-how-to-for-those-coming-from-django/)
- [Flask-Foundation](https://github.com/JackStouffer/Flask-Foundation) by [@JackStouffer](https://github.com/JackStouffer)
- [flask-bones](https://github.com/cburmeister/flask-bones) by [@cburmeister](https://github.com/cburmeister)
- [flask-basic-registration](https://github.com/mjhea0/flask-basic-registration) by [@mjhea0](https://github.com/mjhea0)
- [Flask Official Documentation](http://flask.pocoo.org/docs/)
## License
MIT licensed.
## Changelog
### 18.0.0 (09/09/2018)
- Use CalVer (`YY.MINOR.MICRO`).
- Upgrade to Bootstrap 4. Thanks [@adawalli](https://github.com/adawalli) and [@Hiyorim](https://github.com/Hiyorimi).
- Use environment variables for configuration.
- Add support for Pipenv.
- Upgrade Python and Node dependencies.
### 0.13.0 (06/25/2017)
- Use webpack for building front-end assets. Front-end dependencies are
installed with NPM. Remove Flask-Assets and bower.json. Thanks
[@wroberts](https://github.com/wroberts).
### 0.12.0 (11/06/2016)
- Update Python dependencies.
### 0.11.1 (11/06/2016)
- Correctly pass first parameter to `Flask` according to the 0.11 [docs](http://flask.pocoo.org/docs/0.11/api/#application-object). Thanks [@aliavni](https://github.com/aliavni).
- Remove setuptools and wheel as dependencies to fix deployment on Heroku. Thanks [@Cabalist](https://github.com/Cabalist).
- Make User.password a Binary field for compatibility with new versions of bcrypt. Thanks again [@Cabalist](https://github.com/Cabalist).
### 0.11.0 (09/10/2016)
- Use the FLASK_DEBUG system environment variable, instead of MYFLASKAPP_ENV, to control different configs for development and production environments
### 0.10.1 (08/28/2016)
- Fix `invoke test` command.
### 0.10.0 (08/28/2016)
- Update to Flask 0.11.
- Use Click instead of Flask-Script for CLI commands.
### 0.9.0 (03/06/2016)
- Update stale requirements.
- Add CSRF protection.
- Run `lint` command on Travis builds.
- Test against Python 3.5.
### 0.8.0 (11/09/2015)
- Update stale requirements.
- Add `lint`, `clean`, and `urls` management commands.
- Add isort.
Thanks @andreoliw for these contributions.
### 0.7.0 (04/14/2015)
- Update extension import style to flask_* as per [mitsuhiko/flask#1135](https://github.com/mitsuhiko/flask/issues/1135).
- Update stale requirements (Werkzeug, Flask-WTF, WTForms, Flask-Bcrypt, Flask-DebugToolbar, Flask-Migrate, Bootstrap, jQuery). Thanks @bsmithgall for notifying me of the critical patch to Flask-Migrate.
### 0.6.0 (12/01/2014)
- Test the cookiecutter on Travis. Thanks @joshfriend.
- Update stale requirements (Flask-WTF, Flask-Migrate, Flask-DebugToolbar)
### 0.5.0 (09/29/2014)
- Fix .travis.yml.
- Update stale requirements (Flask-WTF, WTForms, Flask-SQLAlchemy, jquery, Bootstrap)
### 0.4.3 (07/27/2014)
- Add `BaseFactory` class.
- Add compat.py module.
- Tests pass on Python 3.
### 0.4.2 (07/27/2014)
- Update factories to factory-boy >= 2.4.0 syntax.
- Update stale requirements.
### 0.4.1 (06/07/2014)
- Update stale requirements (Werkzeug 0.9.6, WTForms 2.0)
- Fix unmatched div tag in home.html (thanks [@level09](https://github.com/level09))
### 0.4.0 (04/19/2014)
- Add ReferenceCol for less verbose foreign key columns.
- Add SurrogatePK mixin for adding integer primary key to a model.
- Add base Model class that has CRUD convenience methods.
- Fix setting BCrypt encryption complexity. Tests are much faster.
- Add Role model to show ReferenceCol usage.
- Switch to pytest.
- Upgrade all out-of-date requirements.
- More test examples.
- Remove "year" from cookiecutter.json (just change LICENSE if necessary).
### 0.3.2 (02/26/2014)
- Fix static assets.
### 0.3.1 (02/20/2014)
- Update default year in cookiecutter.json. Thanks @Omeryl
- Correct testing of redirects in webtests. Thanks @Widdershin
- Fix POST action in nav form. Thanks @Widdershin.
- Update Bootstrap (3.1.1) and jQuery (2.1.0)
- Optional support for bower.
- Minified assets aren't used in dev environment.
### 0.3.0 (12/08/2013)
- More modular organization: each blueprint contains its own view, models, and forms in a directory. There is still a single directory for templates and static assets.
- Use Flask-Bcrypt for password hashing.
- Flask-Login for authentication.
- Simple test setup. Just create a subclass of `DbTestCase`.
- Flask-Testing support.
- Use Factory-Boy for test factories.
- Use WebTest for functional testing.
- Add Flask-Debugtoolbar.
- Migrations using Flask-Migrate.
- Caching using Flask-Cache.
- Add error page templates (404, 401, 500)
- Add Font Awesome 4.0.3 for icons.
### 0.2.0 (09/21/2013)
- Add manage.py script
- Add Flask-Assets for CSS and JS bundling+minification
- Use different configs for development and production environments, controlled by the MYFLASKAPP_ENV system environment variable
- Use Blueprints and application factory pattern. The `simple` branch does not use these.
### 0.1.0 (08/20/2013)
- First iteration
- Bootstrap 3 final
- Working User model and registration

@ -1,253 +0,0 @@
cookiecutter-flask
==================
A Flask template for cookiecutter_.
.. _cookiecutter: https://github.com/audreyr/cookiecutter
.. image:: https://travis-ci.org/cookiecutter-flask/cookiecutter-flask.svg?branch=master
:target: https://travis-ci.org/cookiecutter-flask/cookiecutter-flask
:alt: Build Status
.. image:: https://img.shields.io/badge/calver-YY.MINOR.MICRO-22bfda.svg
:target: http://calver.org
:alt: CalVer
Use it now
----------
Docker
******
.. code-block:: bash
$ ./cookiecutter-docker.sh --help
Usage: ./cookiecutter-docker.sh [OPTIONS]
Options:
-b, --build Build Docker image before running cookiecutter
-t, --template Specify custom cookiecutter template via a URI to a git repo
e.g. https://github.com/cookiecutter-flask/cookiecutter-flask.git
Defaults to template in current working directory
-h, --help Show this message and exit
Standard
********
.. code-block:: bash
$ pip install cookiecutter
$ cookiecutter https://github.com/cookiecutter-flask/cookiecutter-flask.git
You will be asked about your basic info (name, project name, app name, etc.). This info will be used in your new project.
Configure and Run
-----------------
After you have generated the project code, there are a few more steps you must take before your new app will run. The README of the generated project shows you how to configure and run the application. (You can see the `template README here <https://github.com/cookiecutter-flask/cookiecutter-flask/blob/master/%7B%7Bcookiecutter.app_name%7D%7D/README.rst>`_.)
Features
--------
- Bootstrap 4 and Font Awesome 4 with starter templates
- Flask-SQLAlchemy with basic User model
- Easy database migrations with Flask-Migrate
- Configuration in environment variables, as per `The Twelve-Factor App <https://12factor.net/config>`_
- Flask-WTForms with login and registration forms
- Flask-Login for authentication
- Flask-Bcrypt for password hashing
- Procfile for deploying to a PaaS (e.g. Heroku)
- pytest and Factory-Boy for testing (example tests included)
- Flask's Click CLI configured with simple commands
- CSS and JS minification using webpack
- npm support for frontend package management
- Caching using Flask-Cache
- Useful debug toolbar
- Utilizes best practices: `Blueprints <http://flask.pocoo.org/docs/blueprints/>`_ and `Application Factory <http://flask.pocoo.org/docs/patterns/appfactories/>`_ patterns
Screenshots
-----------
.. image:: https://user-images.githubusercontent.com/2379650/45271508-917f1c00-b475-11e8-9153-7f7385707a8b.png
:alt: Home page
.. image:: https://user-images.githubusercontent.com/2379650/45271517-a9ef3680-b475-11e8-8de6-fbf3d9cab199.png
:alt: Registration form
Inspiration
-----------
- `Structuring Flask Apps <http://charlesleifer.com/blog/structuring-flask-apps-a-how-to-for-those-coming-from-django/>`_
- `Flask-Foundation <https://github.com/JackStouffer/Flask-Foundation>`_ by `@JackStouffer <https://github.com/JackStouffer>`_
- `flask-bones <https://github.com/cburmeister/flask-bones>`_ by `@cburmeister <https://github.com/cburmeister>`_
- `flask-basic-registration <https://github.com/mjhea0/flask-basic-registration>`_ by `@mjhea0 <https://github.com/mjhea0>`_
- `Flask Official Documentation <http://flask.pocoo.org/docs/>`_
License
-------
MIT licensed.
Changelog
---------
18.0.0 (09/09/2018)
*******************
- Use CalVer (``YY.MINOR.MICRO``).
- Upgrade to Bootstrap 4. Thanks `@adawalli <https://github.com/adawalli>`_ and `@Hiyorimi <https://github.com/Hiyorimi>`_.
- Use environment variables for configuration.
- Add support for Pipenv.
- Upgrade Python and Node dependencies.
0.13.0 (06/25/2017)
*******************
- Use webpack for building front-end assets. Front-end dependencies are
installed with NPM. Remove Flask-Assets and bower.json. Thanks
`@wroberts <https://github.com/wroberts>`_.
0.12.0 (11/06/2016)
*******************
- Update Python dependencies.
0.11.1 (11/06/2016)
*******************
- Correctly pass first parameter to ``Flask`` according to the 0.11 `docs <http://flask.pocoo.org/docs/0.11/api/#application-object>`_. Thanks `@aliavni <https://github.com/aliavni>`_.
- Remove setuptools and wheel as dependencies to fix deployment on Heroku. Thanks `@Cabalist <https://github.com/Cabalist>`_.
- Make User.password a Binary field for compatibility with new versions of bcrypt. Thanks again `@Cabalist <https://github.com/Cabalist>`_.
0.11.0 (09/10/2016)
*******************
- Use the FLASK_DEBUG system environment variable, instead of MYFLASKAPP_ENV, to control different configs for development and production environments
0.10.1 (08/28/2016)
*******************
- Fix ``invoke test`` command.
0.10.0 (08/28/2016)
*******************
- Update to Flask 0.11.
- Use Click instead of Flask-Script for CLI commands.
0.9.0 (03/06/2016)
******************
- Update stale requirements.
- Add CSRF protection.
- Run ``lint`` command on Travis builds.
- Test against Python 3.5.
0.8.0 (11/09/2015)
******************
- Update stale requirements.
- Add ``lint``, ``clean``, and ``urls`` management commands.
- Add isort.
Thanks @andreoliw for these contributions.
0.7.0 (04/14/2015)
******************
- Update extension import style to flask_* as per `mitsuhiko/flask#1135 <https://github.com/mitsuhiko/flask/issues/1135>`_
- Update stale requirements (Werkzeug, Flask-WTF, WTForms, Flask-Bcrypt, Flask-DebugToolbar, Flask-Migrate, Bootstrap, jQuery). Thanks @bsmithgall for notifying me of the critical patch to Flask-Migrate.
0.6.0 (12/01/2014)
******************
- Test the cookiecutter on Travis. Thanks @joshfriend.
- Update stale requirements (Flask-WTF, Flask-Migrate, Flask-DebugToolbar)
0.5.0 (09/29/2014)
******************
- Fix .travis.yml.
- Update stale requirements (Flask-WTF, WTForms, Flask-SQLAlchemy, jquery, Bootstrap)
0.4.3 (07/27/2014)
******************
- Add ``BaseFactory`` class.
- Add compat.py module.
- Tests pass on Python 3.
0.4.2 (07/27/2014)
******************
- Update factories to factory-boy >= 2.4.0 syntax.
- Update stale requirements.
0.4.1 (06/07/2014)
******************
- Update stale requirements (Werkzeug 0.9.6, WTForms 2.0)
- Fix unmatched div tag in home.html (thanks `@level09 <https://github.com/level09>`_ )
0.4.0 (04/19/2014)
******************
- Add ReferenceCol for less verbose foreign key columns.
- Add SurrogatePK mixin for adding integer primary key to a model.
- Add base Model class that has CRUD convenience methods.
- Fix setting BCrypt encryption complexity. Tests are much faster.
- Add Role model to show ReferenceCol usage.
- Switch to pytest.
- Upgrade all out-of-date requirements.
- More test examples.
- Remove "year" from cookiecutter.json (just change LICENSE if necessary).
0.3.2 (02/26/2014)
******************
- Fix static assets.
0.3.1 (02/20/2014)
******************
- Update default year in cookiecutter.json. Thanks @Omeryl
- Correct testing of redirects in webtests. Thanks @Widdershin
- Fix POST action in nav form. Thanks @Widdershin.
- Update Bootstrap (3.1.1) and jQuery (2.1.0)
- Optional support for bower.
- Minified assets aren't used in dev environment.
0.3.0 (12/08/2013)
******************
- More modular organization: each blueprint contains its own view, models, and forms in a directory. There is still a single directory for templates and static assets.
- Use Flask-Bcrypt for password hashing.
- Flask-Login for authentication.
- Simple test setup. Just create a subclass of ``DbTestCase``.
- Flask-Testing support.
- Use Factory-Boy for test factories.
- Use WebTest for functional testing.
- Add Flask-Debugtoolbar.
- Migrations using Flask-Migrate.
- Caching using Flask-Cache.
- Add error page templates (404, 401, 500)
- Add Font Awesome 4.0.3 for icons.
0.2.0 (09/21/2013)
******************
- Add manage.py script
- Add Flask-Assets for CSS and JS bundling+minification
- Use different configs for development and production environments, controlled by the MYFLASKAPP_ENV system environment variable
- Use Blueprints and application factory pattern. The ``simple`` branch does not use these.
0.1.0 (08/20/2013)
******************
- First iteration
- Bootstrap 3 final
- Working User model and registration

@ -30,6 +30,7 @@ def clean_extra_package_management_files():
os.remove(file_or_dir)
else:
shutil.rmtree(file_or_dir)
shutil.copy(".env.example", ".env")
except OSError as e:
LOGGER.warning("While attempting to remove file(s) an error occurred")
LOGGER.warning(f"Error: {e}")

@ -51,7 +51,6 @@ def test(ctx):
ctx.run(f"pip install -r {REQUIREMENTS} --ignore-installed", echo=True)
_run_npm_command(ctx, "run lint")
os.chdir(COOKIE)
shutil.copyfile(os.path.join(COOKIE, ".env.example"), os.path.join(COOKIE, ".env"))
os.environ["FLASK_ENV"] = "production"
os.environ["FLASK_DEBUG"] = "0"
_run_flask_command(ctx, "lint", "--check")

@ -0,0 +1,231 @@
# {{ cookiecutter.project_name }}
{{ cookiecutter.project_short_description}}
## Docker Quickstart
This app can be run completely using `Docker` and `docker-compose`. **Using Docker is recommended, as it guarantees the application is run using compatible versions of Python and Node**.
There are three main services:
To run the development version of the app
```bash
docker-compose up flask-dev
```
To run the production version of the app
```bash
docker-compose up flask-prod
```
The list of `environment:` variables in the `docker-compose.yml` file takes precedence over any variables specified in `.env`.
To run any commands using the `Flask CLI`
```bash
docker-compose run --rm manage <<COMMAND>>
```
Therefore, to initialize a database you would run
```bash
docker-compose run --rm manage db init
docker-compose run --rm manage db migrate
docker-compose run --rm manage db upgrade
```
A docker volume `node-modules` is created to store NPM packages and is reused across the dev and prod versions of the application. For the purposes of DB testing with `sqlite`, the file `dev.db` is mounted to all containers. This volume mount should be removed from `docker-compose.yml` if a production DB server is used.
### Running locally
Run the following commands to bootstrap your environment if you are unable to run the application using Docker
```bash
cd {{cookiecutter.app_name}}
{%- if cookiecutter.use_pipenv == "yes" %}
pipenv install --dev
pipenv shell
{%- else %}
pip install -r requirements/dev.txt
{%- endif %}
npm install
npm start # run the webpack dev server and flask server using concurrently
```
You will see a pretty welcome screen.
#### Database Initialization (locally)
Once you have installed your DBMS, run the following to create your app's
database tables and perform the initial migration
```bash
flask db init
flask db migrate
flask db upgrade
```
## Deployment
When using Docker, reasonable production defaults are set in `docker-compose.yml`
```text
FLASK_ENV=production
FLASK_DEBUG=0
```
Therefore, starting the app in "production" mode is as simple as
```bash
docker-compose up flask-prod
```
If running without Docker
```bash
export FLASK_ENV=production
export FLASK_DEBUG=0
export DATABASE_URL="<YOUR DATABASE URL>"
npm run build # build assets with webpack
flask run # start the flask server
```
## Shell
To open the interactive shell, run
```bash
docker-compose run --rm manage db shell
flask shell # If running locally without Docker
```
By default, you will have access to the flask `app`.
## Running Tests/Linter
To run all tests, run
```bash
docker-compose run --rm manage test
flask test # If running locally without Docker
```
To run the linter, run
```bash
docker-compose run --rm manage lint
flask lint # If running locally without Docker
```
The `lint` command will attempt to fix any linting/style errors in the code. If you only want to know if the code will pass CI and do not wish for the linter to make changes, add the `--check` argument.
## Migrations
Whenever a database migration needs to be made. Run the following commands
```bash
docker-compose run --rm manage db migrate
flask db migrate # If running locally without Docker
```
This will generate a new migration script. Then run
```bash
docker-compose run --rm manage db upgrade
flask db upgrade # If running locally without Docker
```
To apply the migration.
For a full migration command reference, run `docker-compose run --rm manage db --help`.
If you will deploy your application remotely (e.g on Heroku) you should add the `migrations` folder to version control.
You can do this after `flask db migrate` by running the following commands
```bash
git add migrations/*
git commit -m "Add migrations"
```
Make sure folder `migrations/versions` is not empty.
## Asset Management
Files placed inside the `assets` directory and its subdirectories
(excluding `js` and `css`) will be copied by webpack's
`file-loader` into the `static/build` directory. In production, the plugin
`Flask-Static-Digest` zips the webpack content and tags them with a MD5 hash.
As a result, you must use the `static_url_for` function when including static content,
as it resolves the correct file name, including the MD5 hash.
For example
```html
<link rel="shortcut icon" href="{{ "{{" }}static_url_for('static', filename='build/img/favicon.ico') {{ "}}" }}">
```
If all of your static files are managed this way, then their filenames will change whenever their
contents do, and you can ask Flask to tell web browsers that they
should cache all your assets forever by including the following line
in ``.env``:
```text
SEND_FILE_MAX_AGE_DEFAULT=31556926 # one year
```
{%- if cookiecutter.use_heroku == "yes" %}
## Heroku
Before deploying to Heroku you should be familiar with the basic concepts of [Git](https://git-scm.com/) and [Heroku](https://heroku.com/).
Remember to add migrations to your repository. Please check `Migrations`_ section.
Since the filesystem on Heroku is ephemeral, non-version controlled files (like a SQLite database) will be lost at least once every 24 hours. Therefore, a persistent, standalone database like PostgreSQL is recommended. This application will work with any database backend that is compatible with SQLAlchemy, but we provide specific instructions for Postgres, (including the required library `psycopg2-binary`).
**Note:** `psycopg2-binary` package is a practical choice for development and testing but in production it is advised to use the package built from sources. Read more in the [psycopg2 documentation](http://initd.org/psycopg/docs/install.html?highlight=production%20advised%20use%20package%20built%20from%20sources#binary-install-from-pypi).
If you keep your project on GitHub you can use 'Deploy to Heroku' button thanks to which the deployment can be done in web browser with minimal configuration required.
The configuration used by the button is stored in `app.json` file.
<a href="https://heroku.com/deploy" style="display: block"><img src="https://www.herokucdn.com/deploy/button.svg" title="Deploy" alt="Deploy"></a>
<br>
Deployment by using [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli):
* Create Heroku App. You can leave your app name, change it, or leave it blank (random name will be generated)
```bash
heroku create {{cookiecutter.app_name}}
```
* Add buildpacks
```bash
heroku buildpacks:add --index=1 heroku/nodejs
heroku buildpacks:add --index=1 heroku/python
```
* Add database addon which creates a persistent PostgresSQL database. These instructions assume you're using the free [hobby-dev](https://elements.heroku.com/addons/heroku-postgresql#hobby-dev) plan. This command also sets a `DATABASE_URL` environmental variable that your app will use to communicate with the DB.
```bash
heroku addons:create heroku-postgresql:hobby-dev --version=11
```
* Set environmental variables (change `SECRET_KEY` value)
```bash
heroku config:set SECRET_KEY=not-so-secret
heroku config:set FLASK_APP=autoapp.py
```
* Please check `.env.example` to see which environmental variables are used in the project and also need to be set. The exception is `DATABASE_URL`, which Heroku sets automatically.
* Deploy on Heroku by pushing to the `heroku` branch
```bash
git push heroku master
```
{%- endif %}

@ -1,193 +0,0 @@
===============================
{{ cookiecutter.project_name }}
===============================
{{ cookiecutter.project_short_description}}
Quickstart
----------
Run the following commands to bootstrap your environment ::
git clone https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.app_name}}
cd {{cookiecutter.app_name}}
{%- if cookiecutter.use_pipenv == "yes" %}
pipenv install --dev
pipenv shell
{%- else %}
pip install -r requirements/dev.txt
{%- endif %}
cp .env.example .env
npm install
npm start # run the webpack dev server and flask server using concurrently
You will see a pretty welcome screen.
Once you have installed your DBMS, run the following to create your app's
database tables and perform the initial migration ::
flask db init
flask db migrate
flask db upgrade
npm start
Deployment
----------
To deploy::
export FLASK_ENV=production
export FLASK_DEBUG=0
export DATABASE_URL="<YOUR DATABASE URL>"
npm run build # build assets with webpack
flask run # start the flask server
In your production environment, make sure the ``FLASK_DEBUG`` environment
variable is unset or is set to ``0``.
Shell
-----
To open the interactive shell, run ::
flask shell
By default, you will have access to the flask ``app``.
Running Tests/Linter
--------------------
To run all tests, run ::
flask test
To run the linter, run ::
flask lint
The ``lint`` command will attempt to fix any linting/style errors in the code. If you only want to know if the code will pass CI and do not wish for the linter to make changes, add the ``--check`` argument.
Migrations
----------
Whenever a database migration needs to be made. Run the following commands ::
flask db migrate
This will generate a new migration script. Then run ::
flask db upgrade
To apply the migration.
For a full migration command reference, run ``flask db --help``.
If you will deploy your application remotely (e.g on Heroku) you should add the `migrations` folder to version control.
You can do this after ``flask db migrate`` by running the following commands ::
git add migrations/*
git commit -m "Add migrations"
Make sure folder `migrations/versions` is not empty.
Docker
------
This app can be run completely using ``Docker`` and ``docker-compose``. Before starting, make sure to create a new copy of ``.env.example`` called ``.env``.
There are three main services:
To run the development version of the app ::
docker-compose up flask-dev
To run the production version of the app ::
docker-compose up flask-prod
The list of ``environment:`` variables in the ``docker-compose.yml`` file takes precedence over any variables specified in ``.env``.
To run any commands using the ``Flask CLI`` ::
docker-compose run --rm manage <<COMMAND>>
Therefore, to initialize a database you would run ::
docker-compose run --rm manage db init
A docker volume ``node-modules`` is created to store NPM packages and is reused across the dev and prod versions of the application. For the purposes of DB testing with ``sqlite``, the file ``dev.db`` is mounted to all containers. This volume mount should be removed from ``docker-compose.yml`` if a production DB server is used.
Asset Management
----------------
Files placed inside the ``assets`` directory and its subdirectories
(excluding ``js`` and ``css``) will be copied by webpack's
``file-loader`` into the ``static/build`` directory. In production, the plugin
``Flask-Static-Digest`` zips the webpack content and tags them with a MD5 hash.
As a result, you must use the ``static_url_for`` function when including static content,
as it resolves the correct file name, including the MD5 hash.
For example::
<link rel="shortcut icon" href="{{ "{{" }}static_url_for('static', filename='build/img/favicon.ico') {{ "}}" }}">
If all of your static files are managed this way, then their filenames will change whenever their
contents do, and you can ask Flask to tell web browsers that they
should cache all your assets forever by including the following line
in ``.env``::
SEND_FILE_MAX_AGE_DEFAULT=31556926 # one year
{%- if cookiecutter.use_heroku == "yes" %}
Heroku
------
Before deploying to Heroku you should be familiar with the basic concepts of `Git <https://git-scm.com/>`_ and `Heroku <https://heroku.com/>`_.
Remember to add migrations to your repository. Please check `Migrations`_ section.
Since the filesystem on Heroku is ephemeral, non-version controlled files (like a SQLite database) will be lost at least once every 24 hours. Therefore, a persistent, standalone database like PostgreSQL is recommended. This application will work with any database backend that is compatible with SQLAlchemy, but we provide specific instructions for Postgres, (including the required library ``psycopg2-binary``).
**Note:** ``psycopg2-binary`` package is a practical choice for development and testing but in production it is advised to use the package built from sources. Read more in the `psycopg2 documentation <http://initd.org/psycopg/docs/install.html?highlight=production%20advised%20use%20package%20built%20from%20sources#binary-install-from-pypi>`_
If you keep your project on GitHub you can use 'Deploy to Heroku' button thanks to which the deployment can be done in web browser with minimal configuration required.
The configuration used by the button is stored in ``app.json`` file.
.. raw:: html
<a href="https://heroku.com/deploy" style="display: block"><img src="https://www.herokucdn.com/deploy/button.svg" title="Deploy" alt="Deploy"></a>
<br>
Deployment by using `Heroku CLI <https://devcenter.heroku.com/articles/heroku-cli>`_:
* Create Heroku App. You can leave your app name, change it, or leave it blank (random name will be generated)::
heroku create {{cookiecutter.app_name}}
* Add buildpacks::
heroku buildpacks:add --index=1 heroku/nodejs
heroku buildpacks:add --index=1 heroku/python
* Add database addon which creates a persistent PostgresSQL database. These instructions assume you're using the free `hobby-dev <https://elements.heroku.com/addons/heroku-postgresql#hobby-dev>`_ plan. This command also sets a ``DATABASE_URL`` environmental variable that your app will use to communicate with the DB.::
heroku addons:create heroku-postgresql:hobby-dev --version=11
* Set environmental variables (change ``SECRET_KEY`` value)::
heroku config:set SECRET_KEY=not-so-secret
heroku config:set FLASK_APP=autoapp.py
* Please check ``.env.example`` to see which environmental variables are used in the project and also need to be set. The exception is ``DATABASE_URL``, which Heroku sets automatically.
* Deploy on Heroku by pushing to the ``heroku`` branch::
git push heroku master
{%- endif %}
Loading…
Cancel
Save