Skip to content

Commit

Permalink
Merge pull request #87 from tecladocode/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jslvtr authored Oct 20, 2022
2 parents a408f34 + 3ada848 commit bd7d743
Show file tree
Hide file tree
Showing 498 changed files with 12,905 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ Next, let's create the `.flaskenv` file:

```txt title=".flaskenv"
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
```

If we have the `python-dotenv` library installed, when we run the `flask run` command, Flask will read the variables inside `.flaskenv` and use them to configure the Flask app.

The configuration that we'll do is to define the Flask app file (here, `app.py`). Then we'll also set the Flask environment to `development`, which does a couple things:
The configuration that we'll do is to define the Flask app file (here, `app.py`). Then we'll also set the `FLASK_DEBUG` flag to `True`, which does a couple things:

- Sets debug mode to true, which makes the app give us better error messages
- Makes the app give us better error messages and return a traceback when we make requests if there's an error.
- Sets the app reloading to true, so the app restarts when we make code changes

We don't want debug mode to be enabled in production (when we deploy our app), but while we're doing development it's definitely a time-saving tool!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ def create_app(db_url=None):
api = Api(app)

# highlight-start
@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()
# highlight-end

Expand All @@ -104,7 +103,7 @@ We've done three things:

1. Added the `db_url` parameter. This lets us create an app with a certain database URL, or alternatively try to fetch the database URL from the environment variables. The default value will be a local SQLite file, if we don't pass a value ourselves and it isn't in the environment.
2. Added two SQLAlchemy values to `app.config`. One is the database URL (or URI), the other is a [configuration option](https://flask-sqlalchemy.palletsprojects.com/en/2.x/config/) which improves performance.
3. Registered a function to run before our Flask app handles its first request. The function will tell SQLAlchemy to use what it knows in order to create all the database tables we need.
3. When the app is created, tell SQLAlchemy to create all the database tables we need.

:::tip How does SQLAlchemy know what tables to create?
The line `import models` lets SQLAlchemy know what models exist in our application. Because they are `db.Model` instances, SQLAlchemy will look at their `__tablename__` and defined `db.Column` attributes to create the tables.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ class TagSchema(PlainTagSchema):

Let's add the Tag endpoints that aren't related to Items:


| Method | Endpoint | Description |
| ---------- | --------------------- | ------------------------------------------------------- |
|`GET` | `/store/{id}/tag` | Get a list of tags in a store. |
Expand Down Expand Up @@ -177,8 +176,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand All @@ -188,4 +186,4 @@ def create_app(db_url=None):
# highlight-end

return app
```
```
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=True
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def create_app(db_url=None):
db.init_app(app)
api = Api(app)

@app.before_first_request
def create_tables():
with app.app_context():
db.create_all()

api.register_blueprint(ItemBlueprint)
Expand Down
Loading

0 comments on commit bd7d743

Please sign in to comment.