Skip to content

Commit 83ba24d

Browse files
committed
new flask code examples and cross linking
1 parent 76dfd97 commit 83ba24d

38 files changed

+1486
-141
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ at runtime when an
1515
[invalid POST request is sent to a URL route](https://stackoverflow.com/questions/14105452/what-is-the-cause-of-the-bad-request-error-when-submitting-form-in-flask-applica)
1616
that accepts POSTs.
1717

18+
<a href="/flask-app-flask-examples.html">Flask</a>,
19+
<a href="/flask-app-headers-examples.html">Headers</a>,
20+
and <a href="/flask-app-immutabledict-examples.html">ImmutableDict</a>
21+
are several other callables with code examples from the same `flask.app` package.
1822

19-
These subjects go along with the `BadRequest` code examples:
23+
These topics are also useful while reading the `BadRequest` examples:
2024

2125
* [web development](/web-development.html) and [web design](/web-design.html)
2226
* [Flask](/flask.html) and [web framework](/web-frameworks.html) concepts

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

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ This class acts as a central registry for a significant amount of a Flask
1414
application's functionality, including URL rounting,
1515
[template configurations](/template-engines.html), and handling view functions.
1616

17+
<a href="/flask-app-badrequest-examples.html">BadRequest</a>,
18+
<a href="/flask-app-headers-examples.html">Headers</a>,
19+
and <a href="/flask-app-immutabledict-examples.html">ImmutableDict</a>
20+
are several other callables with code examples from the same `flask.app` package.
1721

18-
These subjects go along with the `Flask` code examples:
22+
You should read up on these subjects along with these `Flask` examples:
1923

2024
* [web development](/web-development.html) and [web design](/web-design.html)
2125
* [Flask](/flask.html) and [web framework](/web-frameworks.html) concepts
@@ -75,7 +79,52 @@ def new_checkout():
7579
```
7680

7781

78-
## Example 2 from Flask AppBuilder
82+
## Example 2 from CTFd
83+
[CTFd](https://github.com/CTFd/CTFd)
84+
([homepage](https://ctfd.io/)) is a
85+
[capture the flag (CTF) hacking web app](https://cybersecurity.att.com/blogs/security-essentials/capture-the-flag-ctf-what-is-it-for-a-newbie)
86+
built with [Flask](/flask.html). The application can be used
87+
as-is to run CTF events, or modified for custom rules for related
88+
scenarios. CTFd is open sourced under the
89+
[Apache License 2.0](https://github.com/CTFd/CTFd/blob/master/LICENSE).
90+
91+
[**CTFd / manage.py**](https://github.com/CTFd/CTFd/blob/master/././manage.py)
92+
93+
```python
94+
# manage.py
95+
~~from flask import Flask
96+
from flask_sqlalchemy import SQLAlchemy
97+
from flask_script import Manager
98+
from flask_migrate import Migrate, MigrateCommand
99+
from CTFd import create_app
100+
from CTFd.utils import get_config as get_config_util, set_config as set_config_util
101+
from CTFd.models import *
102+
103+
app = create_app()
104+
105+
manager = Manager(app)
106+
manager.add_command("db", MigrateCommand)
107+
108+
109+
def jsenums():
110+
from CTFd.constants import JS_ENUMS
111+
import json
112+
import os
113+
114+
path = os.path.join(app.root_path, "themes/core/assets/js/constants.js")
115+
116+
with open(path, "w+") as f:
117+
for k, v in JS_ENUMS.items():
118+
f.write("const {} = Object.freeze({});".format(k, json.dumps(v)))
119+
120+
121+
122+
## ... source file continues with no further Flask examples...
123+
124+
```
125+
126+
127+
## Example 3 from Flask AppBuilder
79128
[Flask-AppBuilder](https://github.com/dpgaspar/Flask-AppBuilder)
80129
([documentation](https://flask-appbuilder.readthedocs.io/en/latest/)
81130
and
@@ -138,7 +187,7 @@ class OAuthRegistrationRoleTestCase(unittest.TestCase):
138187
```
139188

140189

141-
## Example 3 from FlaskBB
190+
## Example 4 from FlaskBB
142191
[FlaskBB](https://github.com/flaskbb/flaskbb)
143192
([project website](https://flaskbb.org/)) is a [Flask](/flask.html)-based
144193
forum web application. The web app allows users to chat in an open
@@ -237,7 +286,7 @@ def configure_app(app, config):
237286
```
238287

239288

240-
## Example 4 from flask-base
289+
## Example 5 from flask-base
241290
[flask-base](https://github.com/hack4impact/flask-base)
242291
([project documentation](http://hack4impact.github.io/flask-base/))
243292
provides boilerplate code for new [Flask](/flask.html) web apps.
@@ -316,7 +365,7 @@ def create_app(config):
316365
```
317366

318367

319-
## Example 5 from flask-bones
368+
## Example 6 from flask-bones
320369
[flask-bones](https://github.com/cburmeister/flask-bones)
321370
([demo](http://flask-bones.herokuapp.com/))
322371
is large scale [Flask](/flask.html) example application built
@@ -378,7 +427,7 @@ def create_app(config=config.base_config):
378427
```
379428

380429

381-
## Example 6 from flask-bookshelf
430+
## Example 7 from flask-bookshelf
382431
[flask-bookshelf](https://github.com/damyanbogoev/flask-bookshelf) is the
383432
example [Flask](/flask.html) application that developers create when
384433
going through
@@ -430,7 +479,7 @@ def get_lang_code(endpoint, values):
430479
```
431480

432481

433-
## Example 7 from flaskex
482+
## Example 8 from flaskex
434483
[Flaskex](https://github.com/anfederico/Flaskex) is a working example
435484
[Flask](/flask.html) web application intended as a base to build your
436485
own applications upon. The application comes with pre-built sign up, log in
@@ -483,7 +532,7 @@ def logout():
483532
```
484533

485534

486-
## Example 8 from Flask-HTTPAuth
535+
## Example 9 from Flask-HTTPAuth
487536
[Flask-HTTPAuth](https://github.com/miguelgrinberg/Flask-HTTPAuth)
488537
([documentation](https://flask-httpauth.readthedocs.io/en/latest/)
489538
and
@@ -539,7 +588,7 @@ class HTTPAuthTestCase(unittest.TestCase):
539588
```
540589

541590

542-
## Example 9 from flask-phone-input
591+
## Example 10 from flask-phone-input
543592
[flask-phone-input](https://github.com/miguelgrinberg/flask-phone-input)
544593
is an example application that ties together the
545594
[intTellInput.js](https://github.com/jackocnr/intl-tel-input)
@@ -591,7 +640,7 @@ def index():
591640
```
592641

593642

594-
## Example 10 from flaskSaaS
643+
## Example 11 from flaskSaaS
595644
[flaskSaas](https://github.com/alectrocute/flaskSaaS) is a boilerplate
596645
starter project to build a software-as-a-service (SaaS) web application
597646
in [Flask](/flask.html), with [Stripe](/stripe.html) for billing. The
@@ -640,7 +689,7 @@ from app.models import User
640689
```
641690

642691

643-
## Example 11 from Flask-SocketIO
692+
## Example 12 from Flask-SocketIO
644693
[Flask-SocketIO](https://github.com/miguelgrinberg/Flask-SocketIO)
645694
([PyPI package information](https://pypi.org/project/Flask-SocketIO/),
646695
[official tutorial](https://blog.miguelgrinberg.com/post/easy-websockets-with-flask-and-gevent)
@@ -751,7 +800,7 @@ if __name__ == '__main__':
751800
```
752801

753802

754-
## Example 12 from Flask-User
803+
## Example 13 from Flask-User
755804
[Flask-User](https://github.com/lingthio/Flask-User)
756805
([PyPI information](https://pypi.org/project/Flask-User/)
757806
and
@@ -834,7 +883,7 @@ class UserManager(UserManager__Settings, UserManager__Utils, UserManager__Views)
834883
```
835884

836885

837-
## Example 13 from Flask-VueJs-Template
886+
## Example 14 from Flask-VueJs-Template
838887
[Flask-VueJs-Template](https://github.com/gtalarico/flask-vuejs-template)
839888
([demo site](https://flask-vuejs-template.herokuapp.com/))
840889
is a minimal [Flask](/flask.html) boilerplate starter project that
@@ -875,7 +924,7 @@ def index_client():
875924
```
876925

877926

878-
## Example 14 from Flasky
927+
## Example 15 from Flasky
879928
[Flasky](https://github.com/miguelgrinberg/flasky) is a wonderful
880929
example application by
881930
[Miguel Grinberg](https://github.com/miguelgrinberg) that he builds
@@ -939,7 +988,7 @@ def create_app(config_name):
939988
```
940989

941990

942-
## Example 15 from Datadog Flask Example App
991+
## Example 16 from Datadog Flask Example App
943992
The [Datadog Flask example app](https://github.com/DataDog/trace-examples/tree/master/python/flask)
944993
contains many examples of the [Flask](/flask.html) core functions
945994
available to a developer using the [web framework](/web-frameworks.html).
@@ -1000,7 +1049,7 @@ def before_request():
10001049
```
10011050

10021051

1003-
## Example 16 from keras-flask-deploy-webapp
1052+
## Example 17 from keras-flask-deploy-webapp
10041053
The
10051054
[keras-flask-deploy-webapp](https://github.com/mtobeiyf/keras-flask-deploy-webapp)
10061055
project combines the [Flask](/flask.html) [web framework](/web-frameworks.html)
@@ -1064,7 +1113,7 @@ def model_predict(img, model):
10641113
```
10651114

10661115

1067-
## Example 17 from sandman2
1116+
## Example 18 from sandman2
10681117
[sandman2](https://github.com/jeffknupp/sandman2)
10691118
([project documentation](https://sandman2.readthedocs.io/en/latest/)
10701119
and
@@ -1145,7 +1194,7 @@ def get_app(
11451194
```
11461195

11471196

1148-
## Example 18 from Science Flask
1197+
## Example 19 from Science Flask
11491198
[Science Flask](https://github.com/danielhomola/science_flask)
11501199
is a [Flask](/flask.html)-powered web application for online
11511200
scientific research tools. The project was built as a template
@@ -1214,7 +1263,7 @@ def create_celery_app():
12141263
```
12151264

12161265

1217-
## Example 19 from tedivms-flask
1266+
## Example 20 from tedivms-flask
12181267
[tedivm's flask starter app](https://github.com/tedivm/tedivms-flask) is a
12191268
base of [Flask](/flask.html) code and related projects such as
12201269
[Celery](/celery.html) which provides a template to start your own
@@ -1343,7 +1392,7 @@ def create_app(extra_config_settings={}):
13431392
```
13441393

13451394

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

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

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,114 @@ Headers handles the
1717
from [requests](/flask-globals-request-examples.html) and responses for
1818
Flask web applications.
1919

20+
<a href="/flask-app-badrequest-examples.html">BadRequest</a>,
21+
<a href="/flask-app-flask-examples.html">Flask</a>,
22+
and <a href="/flask-app-immutabledict-examples.html">ImmutableDict</a>
23+
are several other callables with code examples from the same `flask.app` package.
2024

2125
You should read up on these subjects along with these `Headers` examples:
2226

2327
* [web development](/web-development.html) and [web design](/web-design.html)
2428
* [Flask](/flask.html) and [web framework](/web-frameworks.html) concepts
2529

2630

27-
## Example 1 from flask-restx
31+
## Example 1 from CTFd
32+
[CTFd](https://github.com/CTFd/CTFd)
33+
([homepage](https://ctfd.io/)) is a
34+
[capture the flag (CTF) hacking web app](https://cybersecurity.att.com/blogs/security-essentials/capture-the-flag-ctf-what-is-it-for-a-newbie)
35+
built with [Flask](/flask.html). The application can be used
36+
as-is to run CTF events, or modified for custom rules for related
37+
scenarios. CTFd is open sourced under the
38+
[Apache License 2.0](https://github.com/CTFd/CTFd/blob/master/LICENSE).
39+
40+
[**CTFd / tests / helpers.py**](https://github.com/CTFd/CTFd/blob/master/./tests/helpers.py)
41+
42+
```python
43+
# helpers.py
44+
import datetime
45+
import gc
46+
import random
47+
import string
48+
import uuid
49+
from collections import namedtuple
50+
from unittest.mock import Mock, patch
51+
52+
import requests
53+
from flask.testing import FlaskClient
54+
from sqlalchemy.engine.url import make_url
55+
from sqlalchemy_utils import drop_database
56+
~~from werkzeug.datastructures import Headers
57+
58+
from CTFd import create_app
59+
from CTFd.cache import cache, clear_standings
60+
from CTFd.config import TestingConfig
61+
from CTFd.models import (
62+
Awards,
63+
ChallengeFiles,
64+
Challenges,
65+
Fails,
66+
Files,
67+
Flags,
68+
Hints,
69+
Notifications,
70+
PageFiles,
71+
Pages,
72+
Solves,
73+
Tags,
74+
Teams,
75+
Tokens,
76+
Tracking,
77+
Unlocks,
78+
Users,
79+
)
80+
81+
text_type = str
82+
binary_type = bytes
83+
84+
85+
FakeRequest = namedtuple("FakeRequest", ["form"])
86+
87+
88+
class CTFdTestClient(FlaskClient):
89+
def open(self, *args, **kwargs):
90+
if kwargs.get("json") is not None:
91+
with self.session_transaction() as sess:
92+
~~ api_key_headers = Headers({"CSRF-Token": sess.get("nonce")})
93+
~~ headers = kwargs.pop("headers", Headers())
94+
if isinstance(headers, dict):
95+
~~ headers = Headers(headers)
96+
headers.extend(api_key_headers)
97+
kwargs["headers"] = headers
98+
return super(CTFdTestClient, self).open(*args, **kwargs)
99+
100+
101+
def create_ctfd(
102+
ctf_name="CTFd",
103+
ctf_description="CTF description",
104+
name="admin",
105+
email="admin@ctfd.io",
106+
password="password",
107+
user_mode="users",
108+
setup=True,
109+
enable_plugins=False,
110+
application_root="/",
111+
config=TestingConfig,
112+
):
113+
if enable_plugins:
114+
config.SAFE_MODE = False
115+
else:
116+
config.SAFE_MODE = True
117+
118+
config.APPLICATION_ROOT = application_root
119+
url = make_url(config.SQLALCHEMY_DATABASE_URI)
120+
121+
122+
## ... source file continues with no further Headers examples...
123+
124+
```
125+
126+
127+
## Example 2 from flask-restx
28128
[Flask RESTX](https://github.com/python-restx/flask-restx) is an
29129
extension that makes it easier to build
30130
[RESTful APIs](/application-programming-interfaces.html) into

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ The `ImmutableDict` class wraps a
1515
[standard Python dictionary](https://docs.python.org/3/tutorial/datastructures.html#dictionaries)
1616
so that values cannot be modified after initially being set.
1717

18+
<a href="/flask-app-badrequest-examples.html">BadRequest</a>,
19+
<a href="/flask-app-flask-examples.html">Flask</a>,
20+
and <a href="/flask-app-headers-examples.html">Headers</a>
21+
are several other callables with code examples from the same `flask.app` package.
1822

19-
You should read up on these subjects along with these `ImmutableDict` examples:
23+
These topics are also useful while reading the `ImmutableDict` examples:
2024

2125
* [web development](/web-development.html) and [web design](/web-design.html)
2226
* [Flask](/flask.html) and [web framework](/web-frameworks.html) concepts

0 commit comments

Comments
 (0)