Skip to content

Commit 65e1b35

Browse files
committed
add new examples from flaskex project
1 parent 334b206 commit 65e1b35

File tree

5 files changed

+446
-21
lines changed

5 files changed

+446
-21
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ FlaskBB is provided as open source
2424
[under this license](https://github.com/flaskbb/flaskbb/blob/master/LICENSE).
2525

2626

27+
### Flaskex
28+
[Flaskex](https://github.com/anfederico/Flaskex) is a working example
29+
[Flask](/flask.html) web application intended as a base to build your
30+
own applications upon. The application comes with pre-built sign up, log in
31+
and related screens, as well as a database backend. Flaskex is provided
32+
as open source under the
33+
[MIT license](https://github.com/anfederico/Flaskex/blob/master/LICENSE.txt).
34+
35+
2736
### Flask JSONDash
2837
[Flask JSONDash](https://github.com/christabor/flask_jsondash) is a
2938
configurable web application built in Flask that creates charts and

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

Lines changed: 111 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,111 @@ def configure_migrations(app):
251251
```
252252

253253

254-
## Example 3 from flask-login
254+
## Example 3 from flaskex
255+
[Flaskex](https://github.com/anfederico/Flaskex) is a working example
256+
[Flask](/flask.html) web application intended as a base to build your
257+
own applications upon. The application comes with pre-built sign up, log in
258+
and related screens, as well as a database backend. Flaskex is provided
259+
as open source under the
260+
[MIT license](https://github.com/anfederico/Flaskex/blob/master/LICENSE.txt).
261+
262+
[**flaskex / app.py**](https://github.com/anfederico/Flaskex/blob/master/././app.py)
263+
264+
```python
265+
# app.py
266+
# -*- coding: utf-8 -*-
267+
268+
from scripts import tabledef
269+
from scripts import forms
270+
from scripts import helpers
271+
~~from flask import Flask, redirect, url_for, render_template, request, session
272+
import json
273+
import sys
274+
import os
275+
276+
app = Flask(__name__)
277+
app.secret_key = os.urandom(12) # Generic key for dev purposes only
278+
279+
# Heroku
280+
#from flask_heroku import Heroku
281+
#heroku = Heroku(app)
282+
283+
# ======== Routing =========================================================== #
284+
# -------- Login ------------------------------------------------------------- #
285+
@app.route('/', methods=['GET', 'POST'])
286+
def login():
287+
if not session.get('logged_in'):
288+
~~ form = forms.LoginForm(request.form)
289+
~~ if request.method == 'POST':
290+
~~ username = request.form['username'].lower()
291+
~~ password = request.form['password']
292+
if form.validate():
293+
if helpers.credentials_valid(username, password):
294+
session['logged_in'] = True
295+
session['username'] = username
296+
return json.dumps({'status': 'Login successful'})
297+
return json.dumps({'status': 'Invalid user/pass'})
298+
return json.dumps({'status': 'Both fields required'})
299+
return render_template('login.html', form=form)
300+
user = helpers.get_user()
301+
return render_template('home.html', user=user)
302+
303+
304+
@app.route("/logout")
305+
def logout():
306+
session['logged_in'] = False
307+
return redirect(url_for('login'))
308+
309+
310+
# -------- Signup ---------------------------------------------------------- #
311+
@app.route('/signup', methods=['GET', 'POST'])
312+
def signup():
313+
if not session.get('logged_in'):
314+
~~ form = forms.LoginForm(request.form)
315+
~~ if request.method == 'POST':
316+
~~ username = request.form['username'].lower()
317+
~~ password = helpers.hash_password(request.form['password'])
318+
~~ email = request.form['email']
319+
if form.validate():
320+
if not helpers.username_taken(username):
321+
helpers.add_user(username, password, email)
322+
session['logged_in'] = True
323+
session['username'] = username
324+
return json.dumps({'status': 'Signup successful'})
325+
return json.dumps({'status': 'Username taken'})
326+
return json.dumps({'status': 'User/Pass required'})
327+
return render_template('login.html', form=form)
328+
return redirect(url_for('login'))
329+
330+
331+
# -------- Settings ---------------------------------------------------------- #
332+
@app.route('/settings', methods=['GET', 'POST'])
333+
def settings():
334+
if session.get('logged_in'):
335+
~~ if request.method == 'POST':
336+
~~ password = request.form['password']
337+
if password != "":
338+
password = helpers.hash_password(password)
339+
~~ email = request.form['email']
340+
helpers.change_user(password=password, email=email)
341+
return json.dumps({'status': 'Saved'})
342+
user = helpers.get_user()
343+
return render_template('settings.html', user=user)
344+
return redirect(url_for('login'))
345+
346+
347+
# ======== Main ============================================================== #
348+
if __name__ == "__main__":
349+
app.run(debug=True, use_reloader=True, host="0.0.0.0")
350+
351+
352+
## ... source file continues with no further request examples...
353+
354+
355+
```
356+
357+
358+
## Example 4 from flask-login
255359
[Flask-Login](https://github.com/maxcountryman/flask-login)
256360
([project documentation](https://flask-login.readthedocs.io/en/latest/)
257361
and [PyPI package](https://pypi.org/project/Flask-Login/))
@@ -560,7 +664,7 @@ def _secret_key(key=None):
560664
```
561665

562666

563-
## Example 4 from flask-restx
667+
## Example 5 from flask-restx
564668
[Flask RESTX](https://github.com/python-restx/flask-restx) is an
565669
extension that makes it easier to build
566670
[RESTful APIs](/application-programming-interfaces.html) into
@@ -665,7 +769,7 @@ class marshal_with_field(object):
665769
```
666770

667771

668-
## Example 5 from flask-sqlalchemy
772+
## Example 6 from flask-sqlalchemy
669773
[flask-sqlalchemy](https://github.com/pallets/flask-sqlalchemy)
670774
([project documentation](https://flask-sqlalchemy.palletsprojects.com/en/2.x/)
671775
and
@@ -791,7 +895,7 @@ def _make_table(db):
791895
```
792896
793897
794-
## Example 6 from Flask-WTF
898+
## Example 7 from Flask-WTF
795899
[Flask-WTF](https://github.com/lepture/flask-wtf)
796900
([project documentation](https://flask-wtf.readthedocs.io/en/stable/)
797901
and
@@ -953,7 +1057,7 @@ def generate_csrf(secret_key=None, token_key=None):
9531057
```
9541058
9551059
956-
## Example 7 from flaskSaaS
1060+
## Example 8 from flaskSaaS
9571061
[flaskSaas](https://github.com/alectrocute/flaskSaaS) is a boilerplate
9581062
starter project to build a software-as-a-service (SaaS) web application
9591063
in [Flask](/flask.html), with [Stripe](/stripe.html) for billing. The
@@ -1006,7 +1110,7 @@ admin.add_view(FileAdmin(path, '/static/', name='Static'))
10061110
```
10071111
10081112
1009-
## Example 8 from newspie
1113+
## Example 9 from newspie
10101114
[NewsPie](https://github.com/skamieniarz/newspie) is a minimalistic news
10111115
aggregator created with [Flask](/flask.html) and the
10121116
[News API](https://newsapi.org/).
@@ -1215,7 +1319,7 @@ if __name__ == '__main__':
12151319
```
12161320
12171321
1218-
## Example 9 from sandman2
1322+
## Example 10 from sandman2
12191323
[sandman2](https://github.com/jeffknupp/sandman2)
12201324
([project documentation](https://sandman2.readthedocs.io/en/latest/)
12211325
and

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

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,111 @@ def do_topic_action(topics, user, action, reverse): # noqa: C901
231231
```
232232
233233
234-
## Example 3 from flask-login
234+
## Example 3 from flaskex
235+
[Flaskex](https://github.com/anfederico/Flaskex) is a working example
236+
[Flask](/flask.html) web application intended as a base to build your
237+
own applications upon. The application comes with pre-built sign up, log in
238+
and related screens, as well as a database backend. Flaskex is provided
239+
as open source under the
240+
[MIT license](https://github.com/anfederico/Flaskex/blob/master/LICENSE.txt).
241+
242+
[**flaskex / app.py**](https://github.com/anfederico/Flaskex/blob/master/././app.py)
243+
244+
```python
245+
# app.py
246+
# -*- coding: utf-8 -*-
247+
248+
from scripts import tabledef
249+
from scripts import forms
250+
from scripts import helpers
251+
~~from flask import Flask, redirect, url_for, render_template, request, session
252+
import json
253+
import sys
254+
import os
255+
256+
app = Flask(__name__)
257+
app.secret_key = os.urandom(12) # Generic key for dev purposes only
258+
259+
# Heroku
260+
#from flask_heroku import Heroku
261+
#heroku = Heroku(app)
262+
263+
# ======== Routing =========================================================== #
264+
# -------- Login ------------------------------------------------------------- #
265+
@app.route('/', methods=['GET', 'POST'])
266+
def login():
267+
~~ if not session.get('logged_in'):
268+
form = forms.LoginForm(request.form)
269+
if request.method == 'POST':
270+
username = request.form['username'].lower()
271+
password = request.form['password']
272+
if form.validate():
273+
if helpers.credentials_valid(username, password):
274+
session['logged_in'] = True
275+
session['username'] = username
276+
return json.dumps({'status': 'Login successful'})
277+
return json.dumps({'status': 'Invalid user/pass'})
278+
return json.dumps({'status': 'Both fields required'})
279+
return render_template('login.html', form=form)
280+
user = helpers.get_user()
281+
return render_template('home.html', user=user)
282+
283+
284+
@app.route("/logout")
285+
def logout():
286+
session['logged_in'] = False
287+
return redirect(url_for('login'))
288+
289+
290+
# -------- Signup ---------------------------------------------------------- #
291+
@app.route('/signup', methods=['GET', 'POST'])
292+
def signup():
293+
~~ if not session.get('logged_in'):
294+
form = forms.LoginForm(request.form)
295+
if request.method == 'POST':
296+
username = request.form['username'].lower()
297+
password = helpers.hash_password(request.form['password'])
298+
email = request.form['email']
299+
if form.validate():
300+
if not helpers.username_taken(username):
301+
helpers.add_user(username, password, email)
302+
session['logged_in'] = True
303+
session['username'] = username
304+
return json.dumps({'status': 'Signup successful'})
305+
return json.dumps({'status': 'Username taken'})
306+
return json.dumps({'status': 'User/Pass required'})
307+
return render_template('login.html', form=form)
308+
return redirect(url_for('login'))
309+
310+
311+
# -------- Settings ---------------------------------------------------------- #
312+
@app.route('/settings', methods=['GET', 'POST'])
313+
def settings():
314+
~~ if session.get('logged_in'):
315+
if request.method == 'POST':
316+
password = request.form['password']
317+
if password != "":
318+
password = helpers.hash_password(password)
319+
email = request.form['email']
320+
helpers.change_user(password=password, email=email)
321+
return json.dumps({'status': 'Saved'})
322+
user = helpers.get_user()
323+
return render_template('settings.html', user=user)
324+
return redirect(url_for('login'))
325+
326+
327+
# ======== Main ============================================================== #
328+
if __name__ == "__main__":
329+
app.run(debug=True, use_reloader=True, host="0.0.0.0")
330+
331+
332+
## ... source file continues with no further session examples...
333+
334+
335+
```
336+
337+
338+
## Example 4 from flask-login
235339
[Flask-Login](https://github.com/maxcountryman/flask-login)
236340
([project documentation](https://flask-login.readthedocs.io/en/latest/)
237341
and [PyPI package](https://pypi.org/project/Flask-Login/))
@@ -407,7 +511,7 @@ def login_required(func):
407511
```
408512
409513
410-
## Example 4 from Flask-WTF
514+
## Example 5 from Flask-WTF
411515
[Flask-WTF](https://github.com/lepture/flask-wtf)
412516
([project documentation](https://flask-wtf.readthedocs.io/en/stable/)
413517
and
@@ -549,7 +653,7 @@ def _get_config(
549653
```
550654

551655

552-
## Example 5 from flaskSaaS
656+
## Example 6 from flaskSaaS
553657
[flaskSaas](https://github.com/alectrocute/flaskSaaS) is a boilerplate
554658
starter project to build a software-as-a-service (SaaS) web application
555659
in [Flask](/flask.html), with [Stripe](/stripe.html) for billing. The

0 commit comments

Comments
 (0)