Skip to content

Commit 25a7e5e

Browse files
committed
adding more examples for flask applications
1 parent 2e5ab26 commit 25a7e5e

25 files changed

+1672
-22
lines changed

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

Lines changed: 769 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
title: flask.app Headers code examples
2+
category: page
3+
slug: flask-app-headers-examples
4+
sortorder: 500021001
5+
toc: False
6+
sidebartitle: flask.app Headers
7+
meta: Python example code for the Headers class from the flask.app module of the Flask project.
8+
9+
10+
Headers is a class within the flask.app module of the Flask project.
11+
12+
13+
## Example 1 from flask-restx
14+
[Flask RESTX](https://github.com/python-restx/flask-restx) is an
15+
extension that makes it easier to build
16+
[RESTful APIs](/application-programming-interfaces.html) into
17+
your applications. Flask RESTX aims for minimal configuration to
18+
get basic APIs running for existing applications and it exposes
19+
endpoint documentation using [Swagger](https://swagger.io/).
20+
21+
Flask RESTX is provided as open source under the
22+
[BSD 3-Clause license](https://github.com/python-restx/flask-restx/blob/master/LICENSE).
23+
24+
[**flask-restx / flask_restx / api.py**](https://github.com/python-restx/flask-restx/blob/master/flask_restx/./api.py)
25+
26+
```python
27+
# api.py
28+
from __future__ import unicode_literals
29+
30+
import difflib
31+
import inspect
32+
from itertools import chain
33+
import logging
34+
import operator
35+
import re
36+
import six
37+
import sys
38+
39+
from collections import OrderedDict
40+
from functools import wraps, partial
41+
from types import MethodType
42+
43+
from flask import url_for, request, current_app
44+
from flask import make_response as original_flask_make_response
45+
from flask.helpers import _endpoint_from_view_func
46+
from flask.signals import got_request_exception
47+
48+
from jsonschema import RefResolver
49+
50+
from werkzeug.utils import cached_property
51+
~~from werkzeug.datastructures import Headers
52+
from werkzeug.exceptions import (
53+
HTTPException,
54+
MethodNotAllowed,
55+
NotFound,
56+
NotAcceptable,
57+
InternalServerError,
58+
)
59+
from werkzeug.wrappers import BaseResponse
60+
61+
from . import apidoc
62+
from .mask import ParseError, MaskError
63+
from .namespace import Namespace
64+
from .postman import PostmanCollectionV1
65+
from .resource import Resource
66+
from .swagger import Swagger
67+
from .utils import default_id, camel_to_dash, unpack
68+
from .representations import output_json
69+
from ._http import HTTPStatus
70+
71+
RE_RULES = re.compile("(<.*>)")
72+
73+
HEADERS_BLACKLIST = ("Content-Length",)
74+
75+
DEFAULT_REPRESENTATIONS = [("application/json", output_json)]
76+
77+
78+
## ... source file abbreviated to get to Headers examples ...
79+
80+
81+
return self.handle_error(e)
82+
except Exception as f:
83+
return original_handler(f)
84+
return original_handler(e)
85+
86+
def handle_error(self, e):
87+
got_request_exception.send(current_app._get_current_object(), exception=e)
88+
89+
if (
90+
not isinstance(e, HTTPException)
91+
and current_app.propagate_exceptions
92+
and not isinstance(e, tuple(self.error_handlers.keys()))
93+
):
94+
95+
exc_type, exc_value, tb = sys.exc_info()
96+
if exc_value is e:
97+
raise
98+
else:
99+
raise e
100+
101+
include_message_in_response = current_app.config.get(
102+
"ERROR_INCLUDE_MESSAGE", True
103+
)
104+
default_data = {}
105+
106+
~~ headers = Headers()
107+
108+
for typecheck, handler in six.iteritems(self._own_and_child_error_handlers):
109+
if isinstance(e, typecheck):
110+
result = handler(e)
111+
default_data, code, headers = unpack(
112+
result, HTTPStatus.INTERNAL_SERVER_ERROR
113+
)
114+
break
115+
else:
116+
if isinstance(e, HTTPException):
117+
code = HTTPStatus(e.code)
118+
if include_message_in_response:
119+
default_data = {"message": getattr(e, "description", code.phrase)}
120+
headers = e.get_response().headers
121+
elif self._default_error_handler:
122+
result = self._default_error_handler(e)
123+
default_data, code, headers = unpack(
124+
result, HTTPStatus.INTERNAL_SERVER_ERROR
125+
)
126+
else:
127+
code = HTTPStatus.INTERNAL_SERVER_ERROR
128+
if include_message_in_response:
129+
default_data = {
130+
"message": code.phrase,
131+
132+
133+
## ... source file continues with no further Headers examples...
134+
135+
```
136+

content/pages/examples/flask/flask-blueprints-blueprint.markdown

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
title: flask.blueprints Blueprint code examples
22
category: page
33
slug: flask-blueprints-blueprint-examples
4-
sortorder: 500021000
4+
sortorder: 500021002
55
toc: False
66
sidebartitle: flask.blueprints Blueprint
77
meta: Python example code for the Blueprint class from the flask.blueprints module of the Flask project.
88

99

10-
Blueprint is a class within the flask.blueprints module of the Flask project.
10+
A Blueprint in [Flask](/flask.html) is
11+
[a "mold" or template for creating parts of web applications](https://stackoverflow.com/questions/24420857/what-are-flask-blueprints-exactly).
1112

1213

1314
## Example 1 from Flask AppBuilder

content/pages/examples/flask/flask-cli-flaskgroup.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
title: flask.cli FlaskGroup code examples
22
category: page
33
slug: flask-cli-flaskgroup-examples
4-
sortorder: 500021001
4+
sortorder: 500021003
55
toc: False
66
sidebartitle: flask.cli FlaskGroup
77
meta: Python example code for the FlaskGroup class from the flask.cli module of the Flask project.

content/pages/examples/flask/flask-cli-scriptinfo.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
title: flask.cli ScriptInfo code examples
22
category: page
33
slug: flask-cli-scriptinfo-examples
4-
sortorder: 500021002
4+
sortorder: 500021004
55
toc: False
66
sidebartitle: flask.cli ScriptInfo
77
meta: Python example code for the ScriptInfo class from the flask.cli module of the Flask project.

content/pages/examples/flask/flask-cli-with-appcontext.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
title: flask.cli with_appcontext code examples
22
category: page
33
slug: flask-cli-with-appcontext-examples
4-
sortorder: 500021003
4+
sortorder: 500021005
55
toc: False
66
sidebartitle: flask.cli with_appcontext
77
meta: Python example code for the with_appcontext function from the flask.cli module of the Flask project.

content/pages/examples/flask/flask-ctx-has-app-context.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
title: flask.ctx has_app_context code examples
22
category: page
33
slug: flask-ctx-has-app-context-examples
4-
sortorder: 500021004
4+
sortorder: 500021006
55
toc: False
66
sidebartitle: flask.ctx has_app_context
77
meta: Python example code for the has_app_context function from the flask.ctx module of the Flask project.

content/pages/examples/flask/flask-ctx-has-request-context.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
title: flask.ctx has_request_context code examples
22
category: page
33
slug: flask-ctx-has-request-context-examples
4-
sortorder: 500021005
4+
sortorder: 500021007
55
toc: False
66
sidebartitle: flask.ctx has_request_context
77
meta: Python example code for the has_request_context function from the flask.ctx module of the Flask project.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
title: flask.globals current_app code examples
22
category: page
33
slug: flask-globals-current-app-examples
4-
sortorder: 500021006
4+
sortorder: 500021008
55
toc: False
66
sidebartitle: flask.globals current_app
77
meta: Python example code for the current_app function from the flask.globals module of the Flask project.

0 commit comments

Comments
 (0)