Code Monkey home page Code Monkey logo

flask-boilerplate's Introduction

Flask Boilerplate CircleCI Code style: black

Deploy Deploy to now

This is an minimal but opinionated boilerplate meant for building out simple REST APIs. It is primarily used at Hack4Impact UIUC. This app is written in Python 3.6 with Postgres 10 as the chosen data persistence. The default way to deploy it is with Heroku or Zeit now but you can deploy it with another service, like AWS, Google Cloud, or DigitalOcean with Gunicorn and Nginx, but instructions for that are not provided. Included are simple examples and instructions developing with or without Docker are provided. I've also written a blog post about using Docker based on this repository.

Documentation is located here. We use black for code formatting, and mypy for optional static typing.

Goal

The goal of this boilerplate is to allow developers to quickly write their API with code structured to best practices while giving them flexibility to easily add/change features. Here are the problems this is trying to solve:

  1. Flask is too flexible. With Flask, you can write your application in any structure you like, even in one file. There are also a lot of different tutorials and guides providing different instructions & application structures to set up a Flask app with a database, confusing many newcomers about best practices.

  2. Django and other Flask boilerplates are too heavy. Sometimes, I don't need a fully featured admin portal with Redis and an Email manager nor do I need templates. Many APIs and applications require the use of a database though. Thus, I've chosen Postgres because it is a battle-tested and reliable database used in many companies and we know that 99% of applications can easily be designed to use relational databases (especially the ones used at Hack4Impact).

Docs

Please Please PLEASE read the documentation if you don't understand something relevant to this boilerplate. Documentation is provided in the wiki page of this repository. I've also added comments with links to specific Flask Documentation to explain certain design choices and/or a specific Flask API (ex: test clients).

Usage

Here are some quickstart instructions, although I would look at the documentation for more details and other options of setting up your environment (e.g. full Docker setup, installed postgres instance, pipenv, etc).

First start a postgres docker container and persist the data with a volume flask-app-db:

make start_dev_db

Another option is to create a postgres instance on a cloud service like elephantsql and connect it to this app. Remember to change the postgres url and don't hard code it in!

Then, start your virtual environment

$ pip3 install virtualenv
$ virtualenv venv
$ source venv/bin/activate

Now, install the python dependencies and run the server:

(venv) $ pip install -r requirements.txt
(venv) $ pip install -r requirements-dev.txt
(venv) $ python manage.py recreate_db
(venv) $ python manage.py runserver

To exit the virtual environment:

(venv) $ deactivate
$

For ease of setup, I have hard-coded postgres URLs for development and docker configurations. If you are using a separate postgres instance as mentioned above, do not hardcode the postgres url including the credentials to your code. Instead, create a file called creds.ini in the same directory level as manage.py and write something like this:

[pg_creds]
pg_url = postgresql://testusr:[email protected]:5432/testdb

Note: you will need to call api.core.get_pg_url in the Config file.

For production, you should do something similar with the flask SECRET_KEY.

Easier setup

I've created a makefile to make this entire process easier but purposely provided verbose instructions there to show you what is necessary to start this application. To do so:

$ make setup

If you like to destroy your docker postgres database and start over, run:

$ make recreate_db

This is under the assumption that you have only set up one postgres container that's linked to the flask-app-db volume.

I would highly suggest reading the documentation for more details on setup.

Deployment

You may use Heroku or Zeit Now and the instructions are defined in the wiki page. I would recommend Heroku. The easiest way to do so is to click the Heroku Deploy button. Remember, once you fork/copy this repo, you will need to change app.json, especially the repository key. Everything else should be fine.

Repository Contents

  • api/views/ - Holds files that define your endpoints
  • api/models/ - Holds files that defines your database schema
  • api/__init__.py - What is initially ran when you start your application
  • api/utils.py - utility functions and classes - explained here
  • api/core.py - includes core functionality including error handlers and logger
  • api/wsgi.py - app reference for gunicorn
  • tests/ - Folder holding tests

Others

  • config.py - Provides Configuration for the application. There are two configurations: one for development and one for production using Heroku.
  • manage.py - Command line interface that allows you to perform common functions with a command
  • requirements.txt - A list of python package dependencies the application requires
  • runtime.txt & Procfile - configuration for Heroku
  • Dockerfile - instructions for Docker to build the Flask app
  • docker-compose.yml - config to setup this Flask app and a Database
  • migrations/ - Holds migration files – doesn't exist until you python manage.py db init if you decide to not use docker

MISC

If you're annoyed by the pycache files

find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf

Additional Documentation

Feel free to contact me for questions and contributions are welcome :)
[email protected]

flask-boilerplate's People

Contributors

pasenidis avatar tko22 avatar yoann9344 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flask-boilerplate's Issues

Questions...

Just two quick questions.

Why is line 12 of docker-compose.yml this:

  • flask-app-db:/var/lib/postgresql/data
    instead of this:
  • flask-app-db:/var/lib/docker/volumes/flask-app-db/_data
    On Ubuntu using the former means that db data does not persist when the server is restarted.

Why is line 58 of api/views/main.py
message=f"Successfully created person {new_person.name} with id: {new_person._id}" and not
message=f"Successfully created person {new_person.name} with id: {new_person.id}"
Is this just a typo? - new_person._id - new_person.id ???

tests

add in common functions and maybe add in continuous integration?

Make `to_dict` recursive

the to_dict function in the class Mixin is used as a helper function in SQLAlchemy ORM models for converting the objects into python data structures for easy json serialization. However, it isn't recursive.

For example, let's have a Person and Email table. Person has name and emails as fields while Email has total_mail, email_addr, person. The emails field is a one-to-many relationship to entries in Email table. The following happens when I run to_dict() function:

person = Person.query.all()[0] # query and get the first person
person.to_dict()
# This outputs
{
    name: "tim"
}
# but it doesn't show `emails`, which should be an array of emails even if it exists
# desired output...
{
   name: "tim",
   emails: [
       {
             "total_mail": 100,
             "email_addr": "[email protected]"
        },
        {
            "total_mail": 20,
            "email_addr": "[email protected]"
        }
   ]
}

Build currently fails on SQLAlchemy error

When running docker-compose build & docker-compose up from the zipfile on a mac, I run into the following issue:

app         | Traceback (most recent call last):
app         |   File "manage.py", line 3, in <module>
app         |     from api import create_app
app         |   File "/app/api/__init__.py", line 7, in <module>
app         |     from sqlalchemy_utils import create_database, database_exists
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/__init__.py", line 1, in <module>
app         |     from .aggregates import aggregated  # noqa
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/aggregates.py", line 372, in <module>
app         |     from .functions.orm import get_column_key
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/__init__.py", line 1, in <module>
app         |     from .database import (  # noqa
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/database.py", line 10, in <module>
app         |     from ..expressions import explain_analyze
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/expressions.py", line 4, in <module>
app         |     from sqlalchemy.sql.expression import (
app         | ImportError: cannot import name '_literal_as_text' from 'sqlalchemy.sql.expression' (/usr/local/lib/python3.7/site-packages/sqlalchemy/sql/expression.py)
app         | Traceback (most recent call last):
app         |   File "manage.py", line 3, in <module>
app         |     from api import create_app
app         |   File "/app/api/__init__.py", line 7, in <module>
app         |     from sqlalchemy_utils import create_database, database_exists
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/__init__.py", line 1, in <module>
app         |     from .aggregates import aggregated  # noqa
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/aggregates.py", line 372, in <module>
app         |     from .functions.orm import get_column_key
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/__init__.py", line 1, in <module>
app         |     from .database import (  # noqa
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/database.py", line 10, in <module>
app         |     from ..expressions import explain_analyze
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/expressions.py", line 4, in <module>
app         |     from sqlalchemy.sql.expression import (
app         | ImportError: cannot import name '_literal_as_text' from 'sqlalchemy.sql.expression' (/usr/local/lib/python3.7/site-packages/sqlalchemy/sql/expression.py)

Looks like this happens on the line:

from sqlalchemy_utils import create_database, database_exists

In api/__init__.py. The solution appears to be to update requirements.txt with the following change:

sqlalchemy<1.4.0

According to this StackOverflow discussion: https://stackoverflow.com/questions/66644975/importerror-cannot-import-name-columnentity-from-sqlalchemy-orm-query

Add creds.ini file instead of hardcoding urls

Hard coding the urls made it much easier for people to set up but that is a very bad way of storing credentials. Instead, we could use a .ini file in which we would parse before we set up the flask configs.

Method .to_dict() in core does not output the up to date object

This is my model:

class Person(Mixin, db.Model):
    """Person Table."""

    __tablename__ = "person"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    faces = db.relationship('Face')


    def __repr__(self):
        return f"<Person {self.name}>"

This is the route:

from webargs import fields
from webargs.flaskparser import use_args
@main.route("/person", methods=['POST'])
@use_args({
    "name": fields.Str(required=True)
})
def post_person(args):
    person = Person(**args)
    db.session.add(person)
    db.session.commit()
    # logger.info(f"id: ",person.id)
    logger.info(f"Person: {person.to_dict()}")
    return create_response(person.to_dict(),201)

Issue: If I execute this code, the logger and the create response will output a null id:

{
    "message": "",
    "result": {
        "_id": null
    },
    "success": true
}

Expectation: Once we perform the commit, the object properties are available. I've confirmed this by printing/logging the id of the person using person.id, and it outputs the correct value. to_dict() outputs null instead of the correct value. It doesn't seem to have the committed object.

Interestingly enough if I uncomment the logger.info line, or if I add db.session.refresh(), the to_dict() outputs the correct result.

This is the result when I execute it with the line # logger.info(f"id: ",person.id) uncommented.

{
    "message": "",
    "result": {
        "_id": 75,
        "name": "Test"
    },
    "success": true
}

Documentation not up to date?

Hello,

Following the Docker setup instructions here:
https://github.com/tko22/flask-boilerplate/wiki/Docker-Setup

docker-compose up -d
This one builds without a problem.

docker ps
shows me everything is running fine.
CONTAINER ID IMAGE COMMAND CREATED STATUS PO
RTS NAMES
c07b67320ea3 flask-boilerplate_app "/app/scripts/docker…" 40 seconds ago Restarting (1) 12 seconds ago
app
add1ae4ccdc3 postgres:10 "docker-entrypoint.s…" 42 seconds ago Up 40 seconds 0.
0.0.0:5432->5432/tcp postgres

docker-compose start
Starting postgres ... done
Starting app ... done
Starts those without any issue.

docker-compose logs app
I quickly get the following error when checking the logs:

app | standard_init_linux.go:211: exec user process caused "no such file or directory"

Any idea if the documentation is out of date? I followed the instructions carefully but it doesn't work.

Thanks!

Docker setup issue

When running the last command for docker setup docker-compose exec app python manage.py recreate_db, this error is thrown:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1122, in _do_get
    return self._pool.get(wait, self._timeout)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/queue.py", line 145, in get
    raise Empty
sqlalchemy.util.queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2147, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 387, in connect
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 766, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 516, in checkout
    rec = pool._do_get()
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1138, in _do_get
    self._dec_overflow()
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise
    raise value
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1135, in _do_get
    return self._create_connection()
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 333, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 461, in __init__
    self.__connect(first_connect_check=True)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 651, in __connect
    connection = pool._invoke_creator(self)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 105, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 393, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
	Is the server running on host "postgres" (172.18.0.2) and accepting
	TCP/IP connections on port 5432?


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 3, in <module>
    from api import app
  File "/app/api/__init__.py", line 16, in <module>
    if not database_exists(db_url):
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy_utils/functions/database.py", line 472, in database_exists
    return bool(get_scalar_result(engine, text))
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy_utils/functions/database.py", line 455, in get_scalar_result
    result_proxy = engine.execute(sql)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2063, in execute
    connection = self.contextual_connect(close_with_result=True)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2112, in contextual_connect
    self._wrap_pool_connect(self.pool.connect, None),
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2151, in _wrap_pool_connect
    e, dialect, self)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1465, in _handle_dbapi_exception_noconnection
    exc_info
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2147, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 387, in connect
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 766, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 516, in checkout
    rec = pool._do_get()
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1138, in _do_get
    self._dec_overflow()
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise
    raise value
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1135, in _do_get
    return self._create_connection()
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 333, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 461, in __init__
    self.__connect(first_connect_check=True)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 651, in __connect
    connection = pool._invoke_creator(self)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 105, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 393, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
	Is the server running on host "postgres" (172.18.0.2) and accepting
	TCP/IP connections on port 5432?

The way I bypassed this was to comment out db.drop_all() in the recreate_db method within manage.py and rerun the command. I think this error is being thrown due to the db not being initialized, but this needs confirmation.

sqlalchemy.exc.OperationalError in Docker setup

Hi, thanks for your nice work!

When I clone this repo and run docker-compose up --build, I get the following error:

app         | /usr/local/lib/python3.7/site-packages/sqlalchemy/sql/functions.py:67: SAWarning: The GenericFunction 'array_agg' is already registered and is going to be overriden.
app         |   "is going to be overriden.".format(identifier)
app         | /usr/local/lib/python3.7/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
app         |   """)
app         | Traceback (most recent call last):
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2265, in _wrap_pool_connect
app         |     return fn()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 363, in connect
app         |     return _ConnectionFairy._checkout(self)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
app         |     fairy = _ConnectionRecord.checkout(pool)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
app         |     rec = pool._do_get()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get
app         |     self._dec_overflow()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
app         |     compat.reraise(exc_type, exc_value, exc_tb)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
app         |     raise value
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
app         |     return self._create_connection()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
app         |     return _ConnectionRecord(self)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
app         |     self.__connect(first_connect_check=True)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
app         |     connection = pool._invoke_creator(self)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
app         |     return dialect.connect(*cargs, **cparams)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 453, in connect
app         |     return self.dbapi.connect(*cargs, **cparams)
app         |   File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
app         |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
app         | psycopg2.OperationalError: server closed the connection unexpectedly
app         | 	This probably means the server terminated abnormally
app         | 	before or while processing the request.
app         |
app         |
app         | The above exception was the direct cause of the following exception:
app         |
app         | Traceback (most recent call last):
app         |   File "manage.py", line 7, in <module>
app         |     app = create_app()
app         |   File "/app/api/__init__.py", line 71, in create_app
app         |     if not database_exists(db_url):
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/database.py", line 472, in database_exists
app         |     return bool(get_scalar_result(engine, text))
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/database.py", line 455, in get_scalar_result
app         |     result_proxy = engine.execute(sql)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2168, in execute
app         |     connection = self._contextual_connect(close_with_result=True)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2229, in _contextual_connect
app         |     self._wrap_pool_connect(self.pool.connect, None),
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2269, in _wrap_pool_connect
app         |     e, dialect, self
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1536, in _handle_dbapi_exception_noconnection
app         |     util.raise_from_cause(sqlalchemy_exception, exc_info)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
app         |     reraise(type(exception), exception, tb=exc_tb, cause=cause)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
app         |     raise value.with_traceback(tb)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2265, in _wrap_pool_connect
app         |     return fn()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 363, in connect
app         |     return _ConnectionFairy._checkout(self)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
app         |     fairy = _ConnectionRecord.checkout(pool)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
app         |     rec = pool._do_get()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get
app         |     self._dec_overflow()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
app         |     compat.reraise(exc_type, exc_value, exc_tb)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
app         |     raise value
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
app         |     return self._create_connection()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
app         |     return _ConnectionRecord(self)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
app         |     self.__connect(first_connect_check=True)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
app         |     connection = pool._invoke_creator(self)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
app         |     return dialect.connect(*cargs, **cparams)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 453, in connect
app         |     return self.dbapi.connect(*cargs, **cparams)
app         |   File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
app         |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
app         | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly
app         | 	This probably means the server terminated abnormally
app         | 	before or while processing the request.
app         |
app         | (Background on this error at: http://sqlalche.me/e/e3q8)
app         | /usr/local/lib/python3.7/site-packages/sqlalchemy/sql/functions.py:67: SAWarning: The GenericFunction 'array_agg' is already registered and is going to be overriden.
app         |   "is going to be overriden.".format(identifier)
app         | /usr/local/lib/python3.7/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
app         |   """)
app         | Traceback (most recent call last):
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2265, in _wrap_pool_connect
app         |     return fn()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 363, in connect
app         |     return _ConnectionFairy._checkout(self)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
app         |     fairy = _ConnectionRecord.checkout(pool)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
app         |     rec = pool._do_get()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get
app         |     self._dec_overflow()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
app         |     compat.reraise(exc_type, exc_value, exc_tb)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
app         |     raise value
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
app         |     return self._create_connection()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
app         |     return _ConnectionRecord(self)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
app         |     self.__connect(first_connect_check=True)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
app         |     connection = pool._invoke_creator(self)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
app         |     return dialect.connect(*cargs, **cparams)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 453, in connect
app         |     return self.dbapi.connect(*cargs, **cparams)
app         |   File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
app         |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
app         | psycopg2.OperationalError: server closed the connection unexpectedly
app         | 	This probably means the server terminated abnormally
app         | 	before or while processing the request.
app         |
app         |
app         | The above exception was the direct cause of the following exception:
app         |
app         | Traceback (most recent call last):
app         |   File "manage.py", line 7, in <module>
app         |     app = create_app()
app         |   File "/app/api/__init__.py", line 71, in create_app
app         |     if not database_exists(db_url):
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/database.py", line 472, in database_exists
app         |     return bool(get_scalar_result(engine, text))
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/database.py", line 455, in get_scalar_result
app         |     result_proxy = engine.execute(sql)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2168, in execute
app         |     connection = self._contextual_connect(close_with_result=True)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2229, in _contextual_connect
app         |     self._wrap_pool_connect(self.pool.connect, None),
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2269, in _wrap_pool_connect
app         |     e, dialect, self
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1536, in _handle_dbapi_exception_noconnection
app         |     util.raise_from_cause(sqlalchemy_exception, exc_info)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
app         |     reraise(type(exception), exception, tb=exc_tb, cause=cause)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
app         |     raise value.with_traceback(tb)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2265, in _wrap_pool_connect
app         |     return fn()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 363, in connect
app         |     return _ConnectionFairy._checkout(self)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
app         |     fairy = _ConnectionRecord.checkout(pool)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
app         |     rec = pool._do_get()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get
app         |     self._dec_overflow()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
app         |     compat.reraise(exc_type, exc_value, exc_tb)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
app         |     raise value
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
app         |     return self._create_connection()
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
app         |     return _ConnectionRecord(self)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
app         |     self.__connect(first_connect_check=True)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
app         |     connection = pool._invoke_creator(self)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
app         |     return dialect.connect(*cargs, **cparams)
app         |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 453, in connect
app         |     return self.dbapi.connect(*cargs, **cparams)
app         |   File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
app         |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
app         | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly
app         | 	This probably means the server terminated abnormally
app         | 	before or while processing the request.
app         |
app         | (Background on this error at: http://sqlalche.me/e/e3q8)

Any idea?

Production configuration

Thanks for this amazing boilerplate, it's simple but enough for a high-quality Flask application. I would just want to make a small suggestion.

Can you add the setting for a WSGI server? Currently, the python manage.py runserver runs the Flask development server which is not recommended on production environment.

Thanks

How to view Flask exceptions when running in docker?

First off, I'm a programmer, but have beginner level experience with Python and Docker ;-)

I've got the app running fine in Docker, but am trying to figure out the development process.

I've reviewed the docs, and blog post, but am not quite sure where to see the Flask exceptions when editing python files/running the app in docker?

I've tried tailing the api.log file, but it's only showing valid HTTP requests, and no exceptions.

I've also tried adding logger.info(error) to the global exception handler in api/core.py, but that didnt help either.

Any suggestions?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.