Skip to content

Commit 0b182c5

Browse files
authored
Merge pull request #15 from pallets-eco/examples
add examples
2 parents afe77ff + b19eada commit 0b182c5

30 files changed

+1329
-0
lines changed

examples/blog/.flaskenv

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FLASK_APP=flaskr

examples/blog/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.venv/
2+
__pycache__/
3+
.coverage
4+
htmlcov/
5+
instance/

examples/blog/LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Pallets
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

examples/blog/README.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Flask Tutorial App
2+
3+
The basic blog app built in the Flask [tutorial]. Modified to use
4+
Flask-SQLAlchemy-Lite and Flask-Alembic.
5+
6+
[tutorial]: https://flask.palletsprojects.com/tutorial/
7+
8+
9+
## Install
10+
11+
Clone the repository and move into the project folder:
12+
13+
```
14+
$ git clone https://github.com/pallets-eco/flask-sqlalchemy-lite
15+
$ cd flask-sqlalchemy-lite/examples/blog
16+
```
17+
18+
Create a virtualenv and activate it:
19+
20+
```
21+
$ python3 -m venv .venv
22+
$ . .venv/bin/activate
23+
```
24+
25+
Or on Windows:
26+
27+
```
28+
$ py -m venv .venv
29+
$ .venv\Scripts\activate
30+
```
31+
32+
Install the project and its dev dependencies:
33+
34+
```
35+
$ pip install -r requirements/dev.txt && pip install -e .
36+
```
37+
38+
## Run
39+
40+
```
41+
$ flask db upgrade
42+
$ flask run --debug
43+
```
44+
45+
Open <http://127.0.0.1:5000> in a browser.
46+
47+
48+
## Test
49+
50+
```
51+
$ coverage run -m pytest
52+
$ coverage report
53+
$ coverage html # open htmlcov/index.html in a browser
54+
```

examples/blog/pyproject.toml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[project]
2+
name = "flaskr"
3+
version = "1.0.0"
4+
description = "The basic blog app built in the Flask tutorial."
5+
readme = "README.md"
6+
license = {file = "LICENSE.txt"}
7+
maintainers = [{name = "Pallets", email = "contact@palletsprojects.com"}]
8+
classifiers = ["Private :: Do Not Upload"]
9+
dependencies = [
10+
"flask",
11+
"flask-alembic",
12+
"flask-sqlalchemy-lite",
13+
]
14+
15+
[build-system]
16+
requires = ["flit_core<4"]
17+
build-backend = "flit_core.buildapi"
18+
19+
[tool.pytest.ini_options]
20+
testpaths = ["tests"]
21+
filterwarnings = ["error"]
22+
23+
[tool.coverage.run]
24+
source = ["flaskr", "tests"]
25+
branch = true
26+
27+
[tool.mypy]
28+
files = ["src/flaskr", "tests"]
29+
show_error_codes = true
30+
pretty = true
31+
strict = true
32+
33+
[tool.pyright]
34+
include = ["src/flaskr", "tests"]
35+
typeCheckingMode = "basic"

examples/blog/requirements/base.txt

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This file was autogenerated by uv via the following command:
2+
# uv pip compile ../pyproject.toml -o base.txt
3+
alembic==1.14.0
4+
# via flask-alembic
5+
asgiref==3.8.1
6+
# via flask
7+
blinker==1.9.0
8+
# via flask
9+
click==8.1.7
10+
# via flask
11+
flask==3.0.3
12+
# via
13+
# flaskr (../pyproject.toml)
14+
# flask-alembic
15+
# flask-sqlalchemy-lite
16+
flask-alembic==3.1.1
17+
# via flaskr (../pyproject.toml)
18+
flask-sqlalchemy-lite==0.1.0
19+
# via flaskr (../pyproject.toml)
20+
greenlet==3.1.1
21+
# via sqlalchemy
22+
itsdangerous==2.2.0
23+
# via flask
24+
jinja2==3.1.4
25+
# via flask
26+
mako==1.3.6
27+
# via alembic
28+
markupsafe==3.0.2
29+
# via
30+
# jinja2
31+
# mako
32+
# werkzeug
33+
sqlalchemy==2.0.36
34+
# via
35+
# alembic
36+
# flask-alembic
37+
# flask-sqlalchemy-lite
38+
typing-extensions==4.12.2
39+
# via
40+
# alembic
41+
# sqlalchemy
42+
werkzeug==3.1.3
43+
# via flask

examples/blog/requirements/dev.in

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-r base.txt
2+
coverage
3+
mypy
4+
pytest
5+
python-dotenv
6+
sqlalchemy-utils
7+
watchdog

examples/blog/requirements/dev.txt

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# This file was autogenerated by uv via the following command:
2+
# uv pip compile dev.in -o dev.txt
3+
alembic==1.14.0
4+
# via
5+
# -r base.txt
6+
# flask-alembic
7+
asgiref==3.8.1
8+
# via
9+
# -r base.txt
10+
# flask
11+
blinker==1.9.0
12+
# via
13+
# -r base.txt
14+
# flask
15+
click==8.1.7
16+
# via
17+
# -r base.txt
18+
# flask
19+
coverage==7.6.4
20+
# via -r dev.in
21+
flask==3.0.3
22+
# via
23+
# -r base.txt
24+
# flask-alembic
25+
# flask-sqlalchemy-lite
26+
flask-alembic==3.1.1
27+
# via -r base.txt
28+
flask-sqlalchemy-lite==0.1.0
29+
# via -r base.txt
30+
greenlet==3.1.1
31+
# via
32+
# -r base.txt
33+
# sqlalchemy
34+
iniconfig==2.0.0
35+
# via pytest
36+
itsdangerous==2.2.0
37+
# via
38+
# -r base.txt
39+
# flask
40+
jinja2==3.1.4
41+
# via
42+
# -r base.txt
43+
# flask
44+
mako==1.3.6
45+
# via
46+
# -r base.txt
47+
# alembic
48+
markupsafe==3.0.2
49+
# via
50+
# -r base.txt
51+
# jinja2
52+
# mako
53+
# werkzeug
54+
mypy==1.13.0
55+
# via -r dev.in
56+
mypy-extensions==1.0.0
57+
# via mypy
58+
packaging==24.2
59+
# via pytest
60+
pluggy==1.5.0
61+
# via pytest
62+
pytest==8.3.3
63+
# via -r dev.in
64+
python-dotenv==1.0.1
65+
# via -r dev.in
66+
sqlalchemy==2.0.36
67+
# via
68+
# -r base.txt
69+
# alembic
70+
# flask-alembic
71+
# flask-sqlalchemy-lite
72+
typing-extensions==4.12.2
73+
# via
74+
# -r base.txt
75+
# alembic
76+
# mypy
77+
# sqlalchemy
78+
watchdog==6.0.0
79+
# via -r dev.in
80+
werkzeug==3.1.3
81+
# via
82+
# -r base.txt
83+
# flask

examples/blog/src/flaskr/__init__.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from __future__ import annotations
2+
3+
import typing as t
4+
5+
import sqlalchemy as sa
6+
from flask import Flask
7+
from flask_alembic import Alembic
8+
from sqlalchemy import orm
9+
10+
from flask_sqlalchemy_lite import SQLAlchemy
11+
12+
13+
class Model(orm.DeclarativeBase):
14+
metadata: t.ClassVar[sa.MetaData] = sa.MetaData(
15+
naming_convention={
16+
"ix": "ix_%(column_0_label)s",
17+
"uq": "uq_%(table_name)s_%(column_0_name)s",
18+
"ck": "ck_%(table_name)s_%(constraint_name)s",
19+
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
20+
"pk": "pk_%(table_name)s",
21+
}
22+
)
23+
24+
25+
db: SQLAlchemy = SQLAlchemy()
26+
alembic: Alembic = Alembic(metadatas=Model.metadata)
27+
28+
29+
def create_app(test_config: dict[str, t.Any] | None = None) -> Flask:
30+
"""Create and configure an instance of the Flask application."""
31+
app = Flask(__name__)
32+
app.config |= {
33+
# a default secret that should be overridden by instance config
34+
"SECRET_KEY": "dev",
35+
# store the database in the instance folder
36+
"SQLALCHEMY_ENGINES": {"default": "sqlite:///blog.sqlite"},
37+
}
38+
39+
if test_config is None: # pragma: no cover
40+
# load config from env vars when not testing
41+
app.config.from_prefixed_env()
42+
else:
43+
# load the test config if passed in
44+
app.testing = True
45+
app.config |= test_config
46+
47+
# apply the extensions to the app
48+
db.init_app(app)
49+
alembic.init_app(app)
50+
51+
# apply the blueprints to the app
52+
from flaskr import auth
53+
from flaskr import blog
54+
55+
app.register_blueprint(auth.bp)
56+
app.register_blueprint(blog.bp)
57+
58+
# make url_for('index') == url_for('blog.index')
59+
# in another app, you might define a separate main index here with
60+
# app.route, while giving the blog blueprint a url_prefix, but for
61+
# the tutorial the blog will be the main index
62+
app.add_url_rule("/", endpoint="index")
63+
64+
return app

0 commit comments

Comments
 (0)