Skip to content

Commit 8b9a521

Browse files
committed
adding new flask example code
1 parent 286ff19 commit 8b9a521

16 files changed

+3179
-401
lines changed

content/pages/04-web-development/32-pelican.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Pelican's
1818
under the
1919
[AGPL 3 license](https://www.gnu.org/licenses/why-affero-gpl.html).
2020

21-
<a href="http://docs.getpelican.com/en/3.6.3/" style="border: none;"><img src="/img/logos/pelican.png" width="100%" alt="Pelican static website generator logo." class="technical-diagram" /></a>
21+
<a href="http://docs.getpelican.com/en/3.6.3/" style="border: none;"><img src="/img/logos/pelican.png" width="100%" alt="Pelican static website generator logo." class="shot" /></a>
2222

2323
<div class="well see-also">Pelican is an implementation of the <a href="/static-site-generator.html">static site generators</a> concept. Learn how the parts fit together in the <a href="/web-development.html">web development</a> chapter or view <a href="/table-of-contents.html">all topics</a>.</div>
2424

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
title: flask.cli FlaskGroup Python code examples
2+
category: page
3+
slug: flask-cli-flaskgroup-examples
4+
sortorder: 500021000
5+
toc: False
6+
sidebartitle: flask.cli FlaskGroup
7+
meta: Python example code for the FlaskGroup class from the flask.cli module of the Flask project.
8+
9+
10+
FlaskGroup is a class within the flask.cli module of the Flask project.
11+
12+
13+
## Example 1 from FlaskBB
14+
[FlaskBB](https://github.com/flaskbb/flaskbb)
15+
([project website](https://flaskbb.org/)) is a [Flask](/flask.html)-based
16+
forum web application. The web app allows users to chat in an open
17+
message board or send private messages in plain text or
18+
[Markdown](/markdown.html).
19+
20+
FlaskBB is provided as open source
21+
[under this license](https://github.com/flaskbb/flaskbb/blob/master/LICENSE).
22+
23+
[**FlaskBB / flaskbb / cli / main.py**](https://github.com/flaskbb/flaskbb/blob/master/flaskbb/cli/main.py)
24+
25+
```python
26+
# main.py
27+
flaskbb.cli.commands
28+
--------------------
29+
30+
This module contains the main commands.
31+
32+
:copyright: (c) 2016 by the FlaskBB Team.
33+
:license: BSD, see LICENSE for more details.
34+
"""
35+
import binascii
36+
import logging
37+
import os
38+
import sys
39+
import time
40+
import traceback
41+
from datetime import datetime
42+
43+
import click
44+
import click_log
45+
from celery.bin.celery import CeleryCommand
46+
from flask import current_app
47+
~~from flask.cli import FlaskGroup, ScriptInfo, with_appcontext
48+
from flask_alembic import alembic_click
49+
from jinja2 import Environment, FileSystemLoader
50+
from sqlalchemy_utils.functions import database_exists
51+
from werkzeug.utils import import_string
52+
53+
from flaskbb import create_app
54+
from flaskbb.cli.utils import (EmailType, FlaskBBCLIError, get_version,
55+
prompt_config_path, prompt_save_user,
56+
write_config)
57+
from flaskbb.extensions import alembic, celery, db, whooshee
58+
from flaskbb.utils.populate import (create_default_groups,
59+
create_default_settings, create_latest_db,
60+
create_test_data, create_welcome_forum,
61+
insert_bulk_data, run_plugin_migrations,
62+
update_settings_from_fixture)
63+
from flaskbb.utils.translations import compile_translations
64+
65+
66+
logger = logging.getLogger(__name__)
67+
click_log.basic_config(logger)
68+
69+
70+
~~class FlaskBBGroup(FlaskGroup):
71+
def __init__(self, *args, **kwargs):
72+
super(FlaskBBGroup, self).__init__(*args, **kwargs)
73+
self._loaded_flaskbb_plugins = False
74+
75+
def _load_flaskbb_plugins(self, ctx):
76+
if self._loaded_flaskbb_plugins:
77+
return
78+
79+
try:
80+
app = ctx.ensure_object(ScriptInfo).load_app()
81+
app.pluggy.hook.flaskbb_cli(cli=self, app=app)
82+
self._loaded_flaskbb_plugins = True
83+
except Exception:
84+
logger.error(
85+
"Error while loading CLI Plugins",
86+
exc_info=traceback.format_exc()
87+
)
88+
else:
89+
shell_context_processors = app.pluggy.hook.flaskbb_shell_context()
90+
for p in shell_context_processors:
91+
app.shell_context_processor(p)
92+
93+
def get_command(self, ctx, name):
94+
self._load_flaskbb_plugins(ctx)
95+
96+
97+
## ... source file continues with no further FlaskGroup examples...
98+
99+
100+
```
101+
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
title: flask.cli ScriptInfo Python code examples
2+
category: page
3+
slug: flask-cli-scriptinfo-examples
4+
sortorder: 500021001
5+
toc: False
6+
sidebartitle: flask.cli ScriptInfo
7+
meta: Python example code for the ScriptInfo class from the flask.cli module of the Flask project.
8+
9+
10+
ScriptInfo is a class within the flask.cli module of the Flask project.
11+
12+
13+
## Example 1 from FlaskBB
14+
[FlaskBB](https://github.com/flaskbb/flaskbb)
15+
([project website](https://flaskbb.org/)) is a [Flask](/flask.html)-based
16+
forum web application. The web app allows users to chat in an open
17+
message board or send private messages in plain text or
18+
[Markdown](/markdown.html).
19+
20+
FlaskBB is provided as open source
21+
[under this license](https://github.com/flaskbb/flaskbb/blob/master/LICENSE).
22+
23+
[**FlaskBB / flaskbb / cli / main.py**](https://github.com/flaskbb/flaskbb/blob/master/flaskbb/cli/main.py)
24+
25+
```python
26+
# main.py
27+
flaskbb.cli.commands
28+
--------------------
29+
30+
This module contains the main commands.
31+
32+
:copyright: (c) 2016 by the FlaskBB Team.
33+
:license: BSD, see LICENSE for more details.
34+
"""
35+
import binascii
36+
import logging
37+
import os
38+
import sys
39+
import time
40+
import traceback
41+
from datetime import datetime
42+
43+
import click
44+
import click_log
45+
from celery.bin.celery import CeleryCommand
46+
from flask import current_app
47+
~~from flask.cli import FlaskGroup, ScriptInfo, with_appcontext
48+
from flask_alembic import alembic_click
49+
from jinja2 import Environment, FileSystemLoader
50+
from sqlalchemy_utils.functions import database_exists
51+
from werkzeug.utils import import_string
52+
53+
from flaskbb import create_app
54+
from flaskbb.cli.utils import (EmailType, FlaskBBCLIError, get_version,
55+
prompt_config_path, prompt_save_user,
56+
write_config)
57+
from flaskbb.extensions import alembic, celery, db, whooshee
58+
from flaskbb.utils.populate import (create_default_groups,
59+
create_default_settings, create_latest_db,
60+
create_test_data, create_welcome_forum,
61+
insert_bulk_data, run_plugin_migrations,
62+
update_settings_from_fixture)
63+
from flaskbb.utils.translations import compile_translations
64+
65+
66+
logger = logging.getLogger(__name__)
67+
click_log.basic_config(logger)
68+
69+
70+
class FlaskBBGroup(FlaskGroup):
71+
def __init__(self, *args, **kwargs):
72+
super(FlaskBBGroup, self).__init__(*args, **kwargs)
73+
self._loaded_flaskbb_plugins = False
74+
75+
def _load_flaskbb_plugins(self, ctx):
76+
if self._loaded_flaskbb_plugins:
77+
return
78+
79+
try:
80+
~~ app = ctx.ensure_object(ScriptInfo).load_app()
81+
app.pluggy.hook.flaskbb_cli(cli=self, app=app)
82+
self._loaded_flaskbb_plugins = True
83+
except Exception:
84+
logger.error(
85+
"Error while loading CLI Plugins",
86+
exc_info=traceback.format_exc()
87+
)
88+
else:
89+
shell_context_processors = app.pluggy.hook.flaskbb_shell_context()
90+
for p in shell_context_processors:
91+
app.shell_context_processor(p)
92+
93+
def get_command(self, ctx, name):
94+
self._load_flaskbb_plugins(ctx)
95+
return super(FlaskBBGroup, self).get_command(ctx, name)
96+
97+
def list_commands(self, ctx):
98+
self._load_flaskbb_plugins(ctx)
99+
return super(FlaskBBGroup, self).list_commands(ctx)
100+
101+
102+
def make_app(script_info):
103+
config_file = getattr(script_info, "config_file", None)
104+
instance_path = getattr(script_info, "instance_path", None)
105+
return create_app(config_file, instance_path)
106+
107+
108+
def set_config(ctx, param, value):
109+
"""This will pass the config file to the create_app function."""
110+
~~ ctx.ensure_object(ScriptInfo).config_file = value
111+
112+
113+
def set_instance(ctx, param, value):
114+
"""This will pass the instance path on the script info which can then
115+
be used in 'make_app'."""
116+
ctx.ensure_object(ScriptInfo).instance_path = value
117+
118+
119+
@click.group(cls=FlaskBBGroup, create_app=make_app, add_version_option=False,
120+
invoke_without_command=True)
121+
@click.option("--config", expose_value=False, callback=set_config,
122+
required=False, is_flag=False, is_eager=True, metavar="CONFIG",
123+
help="Specify the config to use either in dotted module "
124+
"notation e.g. 'flaskbb.configs.default.DefaultConfig' "
125+
"or by using a path like '/path/to/flaskbb.cfg'")
126+
@click.option("--instance", expose_value=False, callback=set_instance,
127+
required=False, is_flag=False, is_eager=True, metavar="PATH",
128+
help="Specify the instance path to use. By default the folder "
129+
"'instance' next to the package or module is assumed to "
130+
"be the instance path.")
131+
@click.option("--version", expose_value=False, callback=get_version,
132+
is_flag=True, is_eager=True, help="Show the FlaskBB version.")
133+
@click.pass_context
134+
@click_log.simple_verbosity_option(logger)
135+
136+
137+
## ... source file continues with no further ScriptInfo examples...
138+
139+
140+
```
141+

0 commit comments

Comments
 (0)