Skip to content

Commit cf81748

Browse files
committed
add new flask example code
1 parent f26e53f commit cf81748

File tree

4 files changed

+486
-6
lines changed

4 files changed

+486
-6
lines changed

content/pages/examples/flask/flask-app-flask.markdown

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,76 @@ def get_app(
10721072
```
10731073

10741074

1075-
## Example 17 from tedivms-flask
1075+
## Example 17 from Science Flask
1076+
[Science Flask](https://github.com/danielhomola/science_flask)
1077+
is a [Flask](/flask.html)-powered web application for online
1078+
scientific research tools. The project was built as a template
1079+
for any scientist or groups of scientists to use when working
1080+
together without having to really understand how the application
1081+
is built. The application includes an academic registration
1082+
process (only valid academic email addresses can be used), an
1083+
admin panel, logging, and analysis forms.
1084+
1085+
[@danielhomola](https://github.com/danielhomola) is the
1086+
primary creator of Science Flask and the project is open
1087+
source under the
1088+
[GNU General Public License](https://github.com/danielhomola/science_flask/blob/master/LICENSE).
1089+
1090+
[**Science Flask / frontend / __init__.py**](https://github.com/danielhomola/science_flask/blob/master/./frontend/__init__.py)
1091+
1092+
```python
1093+
# __init__.py
1094+
import os
1095+
~~from flask import Flask, url_for, redirect, request, abort
1096+
from flask_mail import Mail
1097+
from flask_sqlalchemy import SQLAlchemy
1098+
from flask_login import LoginManager
1099+
from flask_security import Security, SQLAlchemyUserDatastore, signals, \
1100+
current_user
1101+
import flask_admin
1102+
from flask_admin.contrib import sqla
1103+
from flask_admin import helpers as admin_helpers
1104+
from flask_wtf.csrf import CSRFProtect
1105+
from celery import Celery
1106+
1107+
1108+
appdir = os.path.abspath(os.path.dirname(__file__))
1109+
ROOTDIR = os.path.abspath(os.path.join(appdir, os.pardir))
1110+
user_data_folder = os.path.join(ROOTDIR, 'userData')
1111+
1112+
~~app = Flask(__name__, instance_path=user_data_folder)
1113+
1114+
app.config.from_pyfile('config.py')
1115+
1116+
db = SQLAlchemy(app)
1117+
1118+
mail = Mail(app)
1119+
1120+
csrf = CSRFProtect(app)
1121+
1122+
def create_celery_app():
1123+
celery = Celery(__name__, broker=app.config['CELERY_BROKER_URL'])
1124+
celery.conf.update(app.config)
1125+
TaskBase = celery.Task
1126+
1127+
class ContextTask(TaskBase):
1128+
abstract = True
1129+
1130+
def __call__(self, *args, **kwargs):
1131+
with app.app_context():
1132+
return TaskBase.__call__(self, *args, **kwargs)
1133+
1134+
celery.Task = ContextTask
1135+
celery.app = app
1136+
return celery
1137+
1138+
1139+
## ... source file continues with no further Flask examples...
1140+
1141+
```
1142+
1143+
1144+
## Example 18 from tedivms-flask
10761145
[tedivm's flask starter app](https://github.com/tedivm/tedivms-flask) is a
10771146
base of [Flask](/flask.html) code and related projects such as
10781147
[Celery](/celery.html) which provides a template to start your own
@@ -1201,7 +1270,7 @@ def create_app(extra_config_settings={}):
12011270
```
12021271

12031272

1204-
## Example 18 from trape
1273+
## Example 19 from trape
12051274
[trape](https://github.com/jofpin/trape) is a research tool for tracking
12061275
people's activities that are logged digitally. The tool uses
12071276
[Flask](/flask.html) to create a web front end to view aggregated data

content/pages/examples/flask/flask-example-projects-code.markdown

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@ aggregator created with [Flask](/flask.html) and the
112112
the [MIT license](https://github.com/skamieniarz/newspie/blob/master/LICENSE).
113113

114114

115+
### Science Flask
116+
[Science Flask](https://github.com/danielhomola/science_flask)
117+
is a [Flask](/flask.html)-powered web application for online
118+
scientific research tools. The project was built as a template
119+
for any scientist or groups of scientists to use when working
120+
together without having to really understand how the application
121+
is built. The application includes an academic registration
122+
process (only valid academic email addresses can be used), an
123+
admin panel, logging, and analysis forms.
124+
125+
[@danielhomola](https://github.com/danielhomola) is the
126+
primary creator of Science Flask and the project is open
127+
source under the
128+
[GNU General Public License](https://github.com/danielhomola/science_flask/blob/master/LICENSE).
129+
130+
115131
### tedivm's flask starter app
116132
[tedivm's flask starter app](https://github.com/tedivm/tedivms-flask) is a
117133
base of [Flask](/flask.html) code and related projects such as

content/pages/examples/flask/flask-globals-request.markdown

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2995,7 +2995,114 @@ class Service(MethodView):
29952995
```
29962996

29972997

2998-
## Example 19 from tedivms-flask
2998+
## Example 19 from Science Flask
2999+
[Science Flask](https://github.com/danielhomola/science_flask)
3000+
is a [Flask](/flask.html)-powered web application for online
3001+
scientific research tools. The project was built as a template
3002+
for any scientist or groups of scientists to use when working
3003+
together without having to really understand how the application
3004+
is built. The application includes an academic registration
3005+
process (only valid academic email addresses can be used), an
3006+
admin panel, logging, and analysis forms.
3007+
3008+
[@danielhomola](https://github.com/danielhomola) is the
3009+
primary creator of Science Flask and the project is open
3010+
source under the
3011+
[GNU General Public License](https://github.com/danielhomola/science_flask/blob/master/LICENSE).
3012+
3013+
[**Science Flask / frontend / __init__.py**](https://github.com/danielhomola/science_flask/blob/master/./frontend/__init__.py)
3014+
3015+
```python
3016+
# __init__.py
3017+
import os
3018+
~~from flask import Flask, url_for, redirect, request, abort
3019+
from flask_mail import Mail
3020+
from flask_sqlalchemy import SQLAlchemy
3021+
from flask_login import LoginManager
3022+
from flask_security import Security, SQLAlchemyUserDatastore, signals, \
3023+
current_user
3024+
import flask_admin
3025+
from flask_admin.contrib import sqla
3026+
from flask_admin import helpers as admin_helpers
3027+
from flask_wtf.csrf import CSRFProtect
3028+
from celery import Celery
3029+
3030+
3031+
appdir = os.path.abspath(os.path.dirname(__file__))
3032+
ROOTDIR = os.path.abspath(os.path.join(appdir, os.pardir))
3033+
user_data_folder = os.path.join(ROOTDIR, 'userData')
3034+
3035+
app = Flask(__name__, instance_path=user_data_folder)
3036+
3037+
app.config.from_pyfile('config.py')
3038+
3039+
db = SQLAlchemy(app)
3040+
3041+
mail = Mail(app)
3042+
3043+
3044+
3045+
## ... source file abbreviated to get to request examples ...
3046+
3047+
3048+
3049+
class MyModelView(sqla.ModelView):
3050+
3051+
def __init__(self, model, session, name=None, category=None, endpoint=None,
3052+
url=None, **kwargs):
3053+
for k, v in kwargs.items():
3054+
setattr(self, k, v)
3055+
3056+
super(MyModelView, self).__init__(model, session, name=name,
3057+
category=category, endpoint=endpoint,
3058+
url=url)
3059+
3060+
def is_accessible(self):
3061+
if not current_user.is_active or not current_user.is_authenticated:
3062+
return False
3063+
3064+
if current_user.has_role('superuser'):
3065+
return True
3066+
return False
3067+
3068+
def _handle_view(self, name, **kwargs):
3069+
if not self.is_accessible():
3070+
if current_user.is_authenticated:
3071+
abort(403)
3072+
else:
3073+
~~ return redirect(url_for('security.login', next=request.url))
3074+
3075+
admin = flask_admin.Admin(
3076+
app,
3077+
'Admin panel',
3078+
base_template='admin_base.html',
3079+
template_mode='bootstrap3',
3080+
)
3081+
3082+
from .models import Studies, Analyses
3083+
admin.add_view(MyModelView(Role, db.session))
3084+
cols = [c for c in User.__table__.columns]
3085+
admin.add_view(MyModelView(User, db.session, column_list=cols))
3086+
cols = [c for c in Studies.__table__.columns]
3087+
admin.add_view(MyModelView(Studies, db.session, column_list=cols))
3088+
cols = [c for c in Analyses.__table__.columns]
3089+
admin.add_view(MyModelView(Analyses, db.session, column_list=cols))
3090+
3091+
@security.context_processor
3092+
def security_context_processor():
3093+
return dict(
3094+
admin_base_template=admin.base_template,
3095+
admin_view=admin.index_view,
3096+
h=admin_helpers,
3097+
get_url=url_for
3098+
3099+
3100+
## ... source file continues with no further request examples...
3101+
3102+
```
3103+
3104+
3105+
## Example 20 from tedivms-flask
29993106
[tedivm's flask starter app](https://github.com/tedivm/tedivms-flask) is a
30003107
base of [Flask](/flask.html) code and related projects such as
30013108
[Celery](/celery.html) which provides a template to start your own
@@ -3053,7 +3160,7 @@ def roles_accepted_api(*role_names):
30533160
```
30543161

30553162

3056-
## Example 20 from trape
3163+
## Example 21 from trape
30573164
[trape](https://github.com/jofpin/trape) is a research tool for tracking
30583165
people's activities that are logged digitally. The tool uses
30593166
[Flask](/flask.html) to create a web front end to view aggregated data

0 commit comments

Comments
 (0)